Version 1
[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\PropertyAccess\PropertyAccessorInterface;
19 use Symfony\Component\Translation\IdentityTranslator;
20 use Symfony\Component\Translation\TranslatorInterface;
21 use Symfony\Component\Validator\Context\ExecutionContextFactory;
22 use Symfony\Component\Validator\Exception\InvalidArgumentException;
23 use Symfony\Component\Validator\Exception\ValidatorException;
24 use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
25 use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
26 use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
27 use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
28 use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
29 use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
30 use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
31 use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
32 use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
33 use Symfony\Component\Validator\Validator\RecursiveValidator;
34
35 /**
36  * The default implementation of {@link ValidatorBuilderInterface}.
37  *
38  * @author Bernhard Schussek <bschussek@gmail.com>
39  */
40 class ValidatorBuilder implements ValidatorBuilderInterface
41 {
42     /**
43      * @var array
44      */
45     private $initializers = array();
46
47     /**
48      * @var array
49      */
50     private $xmlMappings = array();
51
52     /**
53      * @var array
54      */
55     private $yamlMappings = array();
56
57     /**
58      * @var array
59      */
60     private $methodMappings = array();
61
62     /**
63      * @var Reader|null
64      */
65     private $annotationReader;
66
67     /**
68      * @var MetadataFactoryInterface|null
69      */
70     private $metadataFactory;
71
72     /**
73      * @var ConstraintValidatorFactoryInterface|null
74      */
75     private $validatorFactory;
76
77     /**
78      * @var CacheInterface|null
79      */
80     private $metadataCache;
81
82     /**
83      * @var TranslatorInterface|null
84      */
85     private $translator;
86
87     /**
88      * @var null|string
89      */
90     private $translationDomain;
91
92     /**
93      * @var PropertyAccessorInterface|null
94      */
95     private $propertyAccessor;
96
97     /**
98      * {@inheritdoc}
99      */
100     public function addObjectInitializer(ObjectInitializerInterface $initializer)
101     {
102         $this->initializers[] = $initializer;
103
104         return $this;
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function addObjectInitializers(array $initializers)
111     {
112         $this->initializers = array_merge($this->initializers, $initializers);
113
114         return $this;
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function addXmlMapping($path)
121     {
122         if (null !== $this->metadataFactory) {
123             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
124         }
125
126         $this->xmlMappings[] = $path;
127
128         return $this;
129     }
130
131     /**
132      * {@inheritdoc}
133      */
134     public function addXmlMappings(array $paths)
135     {
136         if (null !== $this->metadataFactory) {
137             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
138         }
139
140         $this->xmlMappings = array_merge($this->xmlMappings, $paths);
141
142         return $this;
143     }
144
145     /**
146      * {@inheritdoc}
147      */
148     public function addYamlMapping($path)
149     {
150         if (null !== $this->metadataFactory) {
151             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
152         }
153
154         $this->yamlMappings[] = $path;
155
156         return $this;
157     }
158
159     /**
160      * {@inheritdoc}
161      */
162     public function addYamlMappings(array $paths)
163     {
164         if (null !== $this->metadataFactory) {
165             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
166         }
167
168         $this->yamlMappings = array_merge($this->yamlMappings, $paths);
169
170         return $this;
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function addMethodMapping($methodName)
177     {
178         if (null !== $this->metadataFactory) {
179             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
180         }
181
182         $this->methodMappings[] = $methodName;
183
184         return $this;
185     }
186
187     /**
188      * {@inheritdoc}
189      */
190     public function addMethodMappings(array $methodNames)
191     {
192         if (null !== $this->metadataFactory) {
193             throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
194         }
195
196         $this->methodMappings = array_merge($this->methodMappings, $methodNames);
197
198         return $this;
199     }
200
201     /**
202      * {@inheritdoc}
203      */
204     public function enableAnnotationMapping(Reader $annotationReader = null)
205     {
206         if (null !== $this->metadataFactory) {
207             throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
208         }
209
210         if (null === $annotationReader) {
211             if (!class_exists('Doctrine\Common\Annotations\AnnotationReader') || !class_exists('Doctrine\Common\Cache\ArrayCache')) {
212                 throw new \RuntimeException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and doctrine/cache to be installed.');
213             }
214
215             $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
216         }
217
218         $this->annotationReader = $annotationReader;
219
220         return $this;
221     }
222
223     /**
224      * {@inheritdoc}
225      */
226     public function disableAnnotationMapping()
227     {
228         $this->annotationReader = null;
229
230         return $this;
231     }
232
233     /**
234      * {@inheritdoc}
235      */
236     public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
237     {
238         if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || null !== $this->annotationReader) {
239             throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
240         }
241
242         $this->metadataFactory = $metadataFactory;
243
244         return $this;
245     }
246
247     /**
248      * {@inheritdoc}
249      */
250     public function setMetadataCache(CacheInterface $cache)
251     {
252         if (null !== $this->metadataFactory) {
253             throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
254         }
255
256         $this->metadataCache = $cache;
257
258         return $this;
259     }
260
261     /**
262      * {@inheritdoc}
263      */
264     public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
265     {
266         if (null !== $this->propertyAccessor) {
267             throw new ValidatorException('You cannot set a validator factory after setting a custom property accessor. Remove the call to setPropertyAccessor() if you want to call setConstraintValidatorFactory().');
268         }
269
270         $this->validatorFactory = $validatorFactory;
271
272         return $this;
273     }
274
275     /**
276      * {@inheritdoc}
277      */
278     public function setTranslator(TranslatorInterface $translator)
279     {
280         $this->translator = $translator;
281
282         return $this;
283     }
284
285     /**
286      * {@inheritdoc}
287      */
288     public function setTranslationDomain($translationDomain)
289     {
290         $this->translationDomain = $translationDomain;
291
292         return $this;
293     }
294
295     /**
296      * {@inheritdoc}
297      *
298      * @deprecated since version 2.5, to be removed in 3.0.
299      *             The validator will function without a property accessor.
300      */
301     public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
302     {
303         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. The validator will function without a property accessor.', E_USER_DEPRECATED);
304
305         if (null !== $this->validatorFactory) {
306             throw new ValidatorException('You cannot set a property accessor after setting a custom validator factory. Configure your validator factory instead.');
307         }
308
309         $this->propertyAccessor = $propertyAccessor;
310
311         return $this;
312     }
313
314     /**
315      * {@inheritdoc}
316      *
317      * @deprecated since version 2.7, to be removed in 3.0.
318      */
319     public function setApiVersion($apiVersion)
320     {
321         @trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
322
323         if (!in_array($apiVersion, array(Validation::API_VERSION_2_4, Validation::API_VERSION_2_5, Validation::API_VERSION_2_5_BC))) {
324             throw new InvalidArgumentException(sprintf('The requested API version is invalid: "%s"', $apiVersion));
325         }
326
327         return $this;
328     }
329
330     /**
331      * {@inheritdoc}
332      */
333     public function getValidator()
334     {
335         $metadataFactory = $this->metadataFactory;
336
337         if (!$metadataFactory) {
338             $loaders = array();
339
340             if (count($this->xmlMappings) > 1) {
341                 $loaders[] = new XmlFilesLoader($this->xmlMappings);
342             } elseif (1 === count($this->xmlMappings)) {
343                 $loaders[] = new XmlFileLoader($this->xmlMappings[0]);
344             }
345
346             if (count($this->yamlMappings) > 1) {
347                 $loaders[] = new YamlFilesLoader($this->yamlMappings);
348             } elseif (1 === count($this->yamlMappings)) {
349                 $loaders[] = new YamlFileLoader($this->yamlMappings[0]);
350             }
351
352             foreach ($this->methodMappings as $methodName) {
353                 $loaders[] = new StaticMethodLoader($methodName);
354             }
355
356             if ($this->annotationReader) {
357                 $loaders[] = new AnnotationLoader($this->annotationReader);
358             }
359
360             $loader = null;
361
362             if (count($loaders) > 1) {
363                 $loader = new LoaderChain($loaders);
364             } elseif (1 === count($loaders)) {
365                 $loader = $loaders[0];
366             }
367
368             $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
369         }
370
371         $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
372         $translator = $this->translator;
373
374         if (null === $translator) {
375             $translator = new IdentityTranslator();
376             // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale
377             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
378             // validation messages are pluralized properly even when the default locale gets changed because they are in
379             // English.
380             $translator->setLocale('en');
381         }
382
383         $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
384
385         return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
386     }
387 }