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\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;
34 * The default implementation of {@link ValidatorBuilderInterface}.
36 * @author Bernhard Schussek <bschussek@gmail.com>
38 class ValidatorBuilder implements ValidatorBuilderInterface
40 private $initializers = array();
41 private $xmlMappings = array();
42 private $yamlMappings = array();
43 private $methodMappings = array();
48 private $annotationReader;
51 * @var MetadataFactoryInterface|null
53 private $metadataFactory;
56 * @var ConstraintValidatorFactoryInterface|null
58 private $validatorFactory;
61 * @var CacheInterface|null
63 private $metadataCache;
66 * @var TranslatorInterface|null
73 private $translationDomain;
78 public function addObjectInitializer(ObjectInitializerInterface $initializer)
80 $this->initializers[] = $initializer;
88 public function addObjectInitializers(array $initializers)
90 $this->initializers = array_merge($this->initializers, $initializers);
98 public function addXmlMapping($path)
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.');
104 $this->xmlMappings[] = $path;
112 public function addXmlMappings(array $paths)
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.');
118 $this->xmlMappings = array_merge($this->xmlMappings, $paths);
126 public function addYamlMapping($path)
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.');
132 $this->yamlMappings[] = $path;
140 public function addYamlMappings(array $paths)
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.');
146 $this->yamlMappings = array_merge($this->yamlMappings, $paths);
154 public function addMethodMapping($methodName)
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.');
160 $this->methodMappings[] = $methodName;
168 public function addMethodMappings(array $methodNames)
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.');
174 $this->methodMappings = array_merge($this->methodMappings, $methodNames);
182 public function enableAnnotationMapping(Reader $annotationReader = null)
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.');
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.');
193 $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
196 $this->annotationReader = $annotationReader;
204 public function disableAnnotationMapping()
206 $this->annotationReader = null;
214 public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
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.');
220 $this->metadataFactory = $metadataFactory;
228 public function setMetadataCache(CacheInterface $cache)
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.');
234 $this->metadataCache = $cache;
242 public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
244 $this->validatorFactory = $validatorFactory;
252 public function setTranslator(TranslatorInterface $translator)
254 $this->translator = $translator;
262 public function setTranslationDomain($translationDomain)
264 $this->translationDomain = $translationDomain;
270 * @return LoaderInterface[]
272 public function getLoaders()
276 foreach ($this->xmlMappings as $xmlMapping) {
277 $loaders[] = new XmlFileLoader($xmlMapping);
280 foreach ($this->yamlMappings as $yamlMappings) {
281 $loaders[] = new YamlFileLoader($yamlMappings);
284 foreach ($this->methodMappings as $methodName) {
285 $loaders[] = new StaticMethodLoader($methodName);
288 if ($this->annotationReader) {
289 $loaders[] = new AnnotationLoader($this->annotationReader);
298 public function getValidator()
300 $metadataFactory = $this->metadataFactory;
302 if (!$metadataFactory) {
303 $loaders = $this->getLoaders();
306 if (\count($loaders) > 1) {
307 $loader = new LoaderChain($loaders);
308 } elseif (1 === \count($loaders)) {
309 $loader = $loaders[0];
312 $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->metadataCache);
315 $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
316 $translator = $this->translator;
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
324 $translator->setLocale('en');
327 $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
329 return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);