4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
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;
36 * The default implementation of {@link ValidatorBuilderInterface}.
38 * @author Bernhard Schussek <bschussek@gmail.com>
40 class ValidatorBuilder implements ValidatorBuilderInterface
45 private $initializers = array();
50 private $xmlMappings = array();
55 private $yamlMappings = array();
60 private $methodMappings = array();
65 private $annotationReader;
68 * @var MetadataFactoryInterface|null
70 private $metadataFactory;
73 * @var ConstraintValidatorFactoryInterface|null
75 private $validatorFactory;
78 * @var CacheInterface|null
80 private $metadataCache;
83 * @var TranslatorInterface|null
90 private $translationDomain;
93 * @var PropertyAccessorInterface|null
95 private $propertyAccessor;
100 public function addObjectInitializer(ObjectInitializerInterface $initializer)
102 $this->initializers[] = $initializer;
110 public function addObjectInitializers(array $initializers)
112 $this->initializers = array_merge($this->initializers, $initializers);
120 public function addXmlMapping($path)
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.');
126 $this->xmlMappings[] = $path;
134 public function addXmlMappings(array $paths)
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.');
140 $this->xmlMappings = array_merge($this->xmlMappings, $paths);
148 public function addYamlMapping($path)
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.');
154 $this->yamlMappings[] = $path;
162 public function addYamlMappings(array $paths)
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.');
168 $this->yamlMappings = array_merge($this->yamlMappings, $paths);
176 public function addMethodMapping($methodName)
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.');
182 $this->methodMappings[] = $methodName;
190 public function addMethodMappings(array $methodNames)
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.');
196 $this->methodMappings = array_merge($this->methodMappings, $methodNames);
204 public function enableAnnotationMapping(Reader $annotationReader = null)
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.');
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.');
215 $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
218 $this->annotationReader = $annotationReader;
226 public function disableAnnotationMapping()
228 $this->annotationReader = null;
236 public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
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.');
242 $this->metadataFactory = $metadataFactory;
250 public function setMetadataCache(CacheInterface $cache)
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.');
256 $this->metadataCache = $cache;
264 public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
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().');
270 $this->validatorFactory = $validatorFactory;
278 public function setTranslator(TranslatorInterface $translator)
280 $this->translator = $translator;
288 public function setTranslationDomain($translationDomain)
290 $this->translationDomain = $translationDomain;
298 * @deprecated since version 2.5, to be removed in 3.0.
299 * The validator will function without a property accessor.
301 public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
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);
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.');
309 $this->propertyAccessor = $propertyAccessor;
317 * @deprecated since version 2.7, to be removed in 3.0.
319 public function setApiVersion($apiVersion)
321 @trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
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));
333 public function getValidator()
335 $metadataFactory = $this->metadataFactory;
337 if (!$metadataFactory) {
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]);
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]);
352 foreach ($this->methodMappings as $methodName) {
353 $loaders[] = new StaticMethodLoader($methodName);
356 if ($this->annotationReader) {
357 $loaders[] = new AnnotationLoader($this->annotationReader);
362 if (count($loaders) > 1) {
363 $loader = new LoaderChain($loaders);
364 } elseif (1 === count($loaders)) {
365 $loader = $loaders[0];
368 $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
371 $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory($this->propertyAccessor);
372 $translator = $this->translator;
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
380 $translator->setLocale('en');
383 $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
385 return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);