PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/prauscher/att
PHP | 296 lines | 158 code | 59 blank | 79 comment | 19 complexity | 67b1050dc1d796fe07a1e7bc6faa20bd 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 Symfony\Component\Validator\Mapping\ClassMetadataFactory;
  12. use Symfony\Component\Validator\Exception\ValidatorException;
  13. use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
  14. use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
  15. use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
  16. use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
  17. use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
  18. use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
  19. use Symfony\Component\Validator\Mapping\Loader\YamlFilesLoader;
  20. use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  21. use Symfony\Component\Validator\Mapping\Loader\XmlFilesLoader;
  22. use Doctrine\Common\Annotations\Reader;
  23. use Doctrine\Common\Annotations\AnnotationReader;
  24. use Doctrine\Common\Annotations\CachedReader;
  25. use Doctrine\Common\Cache\ArrayCache;
  26. /**
  27. * The default implementation of {@link ValidatorBuilderInterface}.
  28. *
  29. * @author Bernhard Schussek <bschussek@gmail.com>
  30. */
  31. class ValidatorBuilder implements ValidatorBuilderInterface
  32. {
  33. /**
  34. * @var array
  35. */
  36. private $initializers = array();
  37. /**
  38. * @var array
  39. */
  40. private $xmlMappings = array();
  41. /**
  42. * @var array
  43. */
  44. private $yamlMappings = array();
  45. /**
  46. * @var array
  47. */
  48. private $methodMappings = array();
  49. /**
  50. * @var Reader
  51. */
  52. private $annotationReader = null;
  53. /**
  54. * @var ClassMetadataFactoryInterface
  55. */
  56. private $metadataFactory;
  57. /**
  58. * @var ConstraintValidatorFactoryInterface
  59. */
  60. private $validatorFactory;
  61. /**
  62. * @var CacheInterface
  63. */
  64. private $metadataCache;
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function addObjectInitializer(ObjectInitializerInterface $initializer)
  69. {
  70. $this->initializers[] = $initializer;
  71. return $this;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function addObjectInitializers(array $initializers)
  77. {
  78. $this->initializers = array_merge($this->initializers, $initializers);
  79. return $this;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function addXmlMapping($path)
  85. {
  86. if (null !== $this->metadataFactory) {
  87. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  88. }
  89. $this->xmlMappings[] = $path;
  90. return $this;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function addXmlMappings(array $paths)
  96. {
  97. if (null !== $this->metadataFactory) {
  98. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  99. }
  100. $this->xmlMappings = array_merge($this->xmlMappings, $paths);
  101. return $this;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function addYamlMapping($path)
  107. {
  108. if (null !== $this->metadataFactory) {
  109. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  110. }
  111. $this->yamlMappings[] = $path;
  112. return $this;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function addYamlMappings(array $paths)
  118. {
  119. if (null !== $this->metadataFactory) {
  120. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  121. }
  122. $this->yamlMappings = array_merge($this->yamlMappings, $paths);
  123. return $this;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function addMethodMapping($methodName)
  129. {
  130. if (null !== $this->metadataFactory) {
  131. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  132. }
  133. $this->methodMappings[] = $methodName;
  134. return $this;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function addMethodMappings(array $methodNames)
  140. {
  141. if (null !== $this->metadataFactory) {
  142. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  143. }
  144. $this->methodMappings = array_merge($this->methodMappings, $methodNames);
  145. return $this;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function enableAnnotationMapping(Reader $annotationReader = null)
  151. {
  152. if (null !== $this->metadataFactory) {
  153. throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
  154. }
  155. if (null === $annotationReader) {
  156. if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
  157. throw new \RuntimeException('Requested a ValidatorFactory with an AnnotationLoader, but the AnnotationReader was not found. You should add Doctrine Common to your project.');
  158. }
  159. $annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache());
  160. }
  161. $this->annotationReader = $annotationReader;
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function disableAnnotationMapping()
  168. {
  169. $this->annotationReader = null;
  170. return $this;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory)
  176. {
  177. if (count($this->xmlMappings) > 0 || count($this->yamlMappings) > 0 || count($this->methodMappings) > 0 || null !== $this->annotationReader) {
  178. throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
  179. }
  180. $this->metadataFactory = $metadataFactory;
  181. return $this;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function setMetadataCache(CacheInterface $cache)
  187. {
  188. if (null !== $this->metadataFactory) {
  189. throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
  190. }
  191. $this->metadataCache = $cache;
  192. return $this;
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
  198. {
  199. $this->validatorFactory = $validatorFactory;
  200. return $this;
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getValidator()
  206. {
  207. $metadataFactory = $this->metadataFactory;
  208. if (!$metadataFactory) {
  209. $loaders = array();
  210. if (count($this->xmlMappings) > 1) {
  211. $loaders[] = new XmlFilesLoader($this->xmlMappings);
  212. } elseif (1 === count($this->xmlMappings)) {
  213. $loaders[] = new XmlFileLoader($this->xmlMappings[0]);
  214. }
  215. if (count($this->yamlMappings) > 1) {
  216. $loaders[] = new YamlFilesLoader($this->yamlMappings);
  217. } elseif (1 === count($this->yamlMappings)) {
  218. $loaders[] = new YamlFileLoader($this->yamlMappings[0]);
  219. }
  220. foreach ($this->methodMappings as $methodName) {
  221. $loaders[] = new StaticMethodLoader($methodName);
  222. }
  223. if ($this->annotationReader) {
  224. $loaders[] = new AnnotationLoader($this->annotationReader);
  225. }
  226. $loader = null;
  227. if (count($loaders) > 1) {
  228. $loader = new LoaderChain($loaders);
  229. } elseif (1 === count($loaders)) {
  230. $loader = $loaders[0];
  231. }
  232. $metadataFactory = new ClassMetadataFactory($loader, $this->metadataCache);
  233. }
  234. $validatorFactory = $this->validatorFactory ?: new ConstraintValidatorFactory();
  235. return new Validator($metadataFactory, $validatorFactory, $this->initializers);
  236. }
  237. }