3562b945922af3ac34bea7bca8efaf16cfff99e5
[yaffs-website] / vendor / symfony / validator / ValidatorBuilder.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator;
13
14 use Doctrine\Common\Annotations\AnnotationReader;
15 use Doctrine\Common\Annotations\CachedReader;
16 use Doctrine\Common\Annotations\Reader;
17 use Doctrine\Common\Cache\ArrayCache;
18 use Symfony\Component\Translation\IdentityTranslator;
19 use Symfony\Component\Translation\TranslatorInterface;
20 use Symfony\Component\Validator\Context\ExecutionContextFactory;
21 use Symfony\Component\Validator\Exception\ValidatorException;
22 use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
23 use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
24 use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
25 use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
26 use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
27 use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
28 use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
29 use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
30 use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
31 use Symfony\Component\Validator\Validator\RecursiveValidator;
32
33 /**
34  * The default implementation of {@link ValidatorBuilderInterface}.
35  *
36  * @author Bernhard Schussek <bschussek@gmail.com>
37  */
38 class ValidatorBuilder implements ValidatorBuilderInterface
39 {
40     private $initializers = array();
41     private $xmlMappings = array();
42     private $yamlMappings = array();
43     private $methodMappings = array();
44
45     /**
46      * @var Reader|null
47      */
48     private $annotationReader;
49
50     /**
51      * @var MetadataFactoryInterface|null
52      */
53     private $metadataFactory;
54
55     /**
56      * @var ConstraintValidatorFactoryInterface|null
57      */
58     private $validatorFactory;
59
60     /**
61      * @var CacheInterface|null
62      */
63     private $metadataCache;
64
65     /**
66      * @var TranslatorInterface|null
67      */
68     private $translator;
69
70     /**
71      * @var string|null
72      */
73     private $translationDomain;
74
75     /**
76      * {@inheritdoc}
77      */
78     public function addObjectInitializer(ObjectInitializerInterface $initializer)
79     {
80         $this->initializers[] = $initializer;
81
82         return $this;
83     }
84
85     /**
86      * {@inheritdoc}
87      */
88     public function addObjectInitializers(array $initializers)
89     {
90         $this->initializers = array_merge($this->initializers, $initializers);
91
92         return $this;
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function addXmlMapping($path)
99     {
100         if (null !== $this->metadataFactory) {
101             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
102         }
103
104         $this->xmlMappings[] = $path;
105
106         return $this;
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     public function addXmlMappings(array $paths)
113     {
114         if (null !== $this->metadataFactory) {
115             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
116         }
117
118         $this->xmlMappings = array_merge($this->xmlMappings, $paths);
119
120         return $this;
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function addYamlMapping($path)
127     {
128         if (null !== $this->metadataFactory) {
129             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
130         }
131
132         $this->yamlMappings[] = $path;
133
134         return $this;
135     }
136
137     /**
138      * {@inheritdoc}
139      */
140     public function addYamlMappings(array $paths)
141     {
142         if (null !== $this->metadataFactory) {
143             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
144         }
145
146         $this->yamlMappings = array_merge($this->yamlMappings, $paths);
147
148         return $this;
149     }
150
151     /**
152      * {@inheritdoc}
153      */
154     public function addMethodMapping($methodName)
155     {
156         if (null !== $this->metadataFactory) {
157             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
158         }
159
160         $this->methodMappings[] = $methodName;
161
162         return $this;
163     }
164
165     /**
166      * {@inheritdoc}
167      */
168     public function addMethodMappings(array $methodNames)
169     {
170         if (null !== $this->metadataFactory) {
171             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
172         }
173
174         $this->methodMappings = array_merge($this->methodMappings, $methodNames);
175
176         return $this;
177     }
178
179     /**
180      * {@inheritdoc}
181      */
182     public function enableAnnotationMapping(Reader $annotationReader = null)
183     {
184         if (null !== $this->metadataFactory) {
185             throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
186         }
187
188         if (null === $annotationReader) {
189             if (!class_exists('Doctrine\Common\Annotations\AnnotationReader') || !class_exists('Doctrine\Common\Cache\ArrayCache')) {
190                 throw new \RuntimeException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.');
191             }
192
193             $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
194         }
195
196         $this->annotationReader = $annotationReader;
197
198         return $this;
199     }
200
201     /**
202      * {@inheritdoc}
203      */
204     public function disableAnnotationMapping()
205     {
206         $this->annotationReader = null;
207
208         return $this;
209     }
210
211     /**
212      * {@inheritdoc}
213      */
214     public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
215     {
216         if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || null !== $this->annotationReader) {
217             throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
218         }
219
220         $this->metadataFactory = $metadataFactory;
221
222         return $this;
223     }
224
225     /**
226      * {@inheritdoc}
227      */
228     public function setMetadataCache(CacheInterface $cache)
229     {
230         if (null !== $this->metadataFactory) {
231             throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
232         }
233
234         $this->metadataCache = $cache;
235
236         return $this;
237     }
238
239     /**
240      * {@inheritdoc}
241      */
242     public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
243     {
244         $this->validatorFactory = $validatorFactory;
245
246         return $this;
247     }
248
249     /**
250      * {@inheritdoc}
251      */
252     public function setTranslator(TranslatorInterface $translator)
253     {
254         $this->translator = $translator;
255
256         return $this;
257     }
258
259     /**
260      * {@inheritdoc}
261      */
262     public function setTranslationDomain($translationDomain)
263     {
264         $this->translationDomain = $translationDomain;
265
266         return $this;
267     }
268
269     /**
270      * @return LoaderInterface[]
271      */
272     public function getLoaders()
273     {
274         $loaders = array();
275
276         foreach ($this->xmlMappings as $xmlMapping) {
277             $loaders[] = new XmlFileLoader($xmlMapping);
278         }
279
280         foreach ($this->yamlMappings as $yamlMappings) {
281             $loaders[] = new YamlFileLoader($yamlMappings);
282         }
283
284         foreach ($this->methodMappings as $methodName) {
285             $loaders[] = new StaticMethodLoader($methodName);
286         }
287
288         if ($this->annotationReader) {
289             $loaders[] = new AnnotationLoader($this->annotationReader);
290         }
291
292         return $loaders;
293     }
294
295     /**
296      * {@inheritdoc}
297      */
298     public function getValidator()
299     {
300         $metadataFactory = $this->metadataFactory;
301
302         if (!$metadataFactory) {
303             $loaders = $this->getLoaders();
304             $loader = null;
305
306             if (\count($loaders) > 1) {
307                 $loader = new LoaderChain($loaders);
308             } elseif (1 === \count($loaders)) {
309                 $loader = $loaders[0];
310             }
311
312             $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
313         }
314
315         $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
316         $translator = $this->translator;
317
318         if (null === $translator) {
319             $translator = new IdentityTranslator();
320             // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale
321             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
322             // validation messages are pluralized properly even when the default locale gets changed because they are in
323             // English.
324             $translator->setLocale('en');
325         }
326
327         $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
328
329         return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
330     }
331 }