PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/ValidatorBuilder.php

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