ValidatorBuilder.php 11 KB

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