/wp-content/plugins/mailpoet/vendor-prefixed/symfony/validator/ValidatorBuilder.php

https://gitlab.com/remyvianne/krowkaramel · PHP · 241 lines · 230 code · 0 blank · 11 comment · 31 complexity · 3a74352f314e5151715f98274ba6cea3 MD5 · raw file

  1. <?php
  2. namespace MailPoetVendor\Symfony\Component\Validator;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\Common\Annotations\AnnotationReader;
  5. use MailPoetVendor\Doctrine\Common\Annotations\CachedReader;
  6. use MailPoetVendor\Doctrine\Common\Annotations\PsrCachedReader;
  7. use MailPoetVendor\Doctrine\Common\Annotations\Reader;
  8. use MailPoetVendor\Doctrine\Common\Cache\ArrayCache;
  9. use MailPoetVendor\Doctrine\Common\Cache\Psr6\DoctrineProvider;
  10. use MailPoetVendor\Psr\Cache\CacheItemPoolInterface;
  11. use MailPoetVendor\Symfony\Component\Cache\Adapter\ArrayAdapter;
  12. use MailPoetVendor\Symfony\Component\Cache\DoctrineProvider as SymfonyDoctrineProvider;
  13. use MailPoetVendor\Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  14. use MailPoetVendor\Symfony\Component\Validator\Context\ExecutionContextFactory;
  15. use MailPoetVendor\Symfony\Component\Validator\Exception\LogicException;
  16. use MailPoetVendor\Symfony\Component\Validator\Exception\ValidatorException;
  17. use MailPoetVendor\Symfony\Component\Validator\Mapping\Cache\CacheInterface;
  18. use MailPoetVendor\Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
  19. use MailPoetVendor\Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
  20. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
  21. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\LoaderChain;
  22. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  23. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
  24. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  25. use MailPoetVendor\Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
  26. use MailPoetVendor\Symfony\Component\Validator\Util\LegacyTranslatorProxy;
  27. use MailPoetVendor\Symfony\Component\Validator\Validator\RecursiveValidator;
  28. use MailPoetVendor\Symfony\Contracts\Translation\LocaleAwareInterface;
  29. use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface;
  30. use MailPoetVendor\Symfony\Contracts\Translation\TranslatorTrait;
  31. // Help opcache.preload discover always-needed symbols
  32. \class_exists(TranslatorInterface::class);
  33. \class_exists(LocaleAwareInterface::class);
  34. \class_exists(TranslatorTrait::class);
  35. class ValidatorBuilder implements ValidatorBuilderInterface
  36. {
  37. private $initializers = [];
  38. private $loaders = [];
  39. private $xmlMappings = [];
  40. private $yamlMappings = [];
  41. private $methodMappings = [];
  42. private $annotationReader;
  43. private $metadataFactory;
  44. private $validatorFactory;
  45. private $mappingCache;
  46. private $translator;
  47. private $translationDomain;
  48. public function addObjectInitializer(ObjectInitializerInterface $initializer)
  49. {
  50. $this->initializers[] = $initializer;
  51. return $this;
  52. }
  53. public function addObjectInitializers(array $initializers)
  54. {
  55. $this->initializers = \array_merge($this->initializers, $initializers);
  56. return $this;
  57. }
  58. public function addXmlMapping($path)
  59. {
  60. if (null !== $this->metadataFactory) {
  61. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  62. }
  63. $this->xmlMappings[] = $path;
  64. return $this;
  65. }
  66. public function addXmlMappings(array $paths)
  67. {
  68. if (null !== $this->metadataFactory) {
  69. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  70. }
  71. $this->xmlMappings = \array_merge($this->xmlMappings, $paths);
  72. return $this;
  73. }
  74. public function addYamlMapping($path)
  75. {
  76. if (null !== $this->metadataFactory) {
  77. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  78. }
  79. $this->yamlMappings[] = $path;
  80. return $this;
  81. }
  82. public function addYamlMappings(array $paths)
  83. {
  84. if (null !== $this->metadataFactory) {
  85. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  86. }
  87. $this->yamlMappings = \array_merge($this->yamlMappings, $paths);
  88. return $this;
  89. }
  90. public function addMethodMapping($methodName)
  91. {
  92. if (null !== $this->metadataFactory) {
  93. throw new ValidatorException('You cannot add custom mappings after setting a custom metadata factory. Configure your metadata factory instead.');
  94. }
  95. $this->methodMappings[] = $methodName;
  96. return $this;
  97. }
  98. public function addMethodMappings(array $methodNames)
  99. {
  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.');
  102. }
  103. $this->methodMappings = \array_merge($this->methodMappings, $methodNames);
  104. return $this;
  105. }
  106. public function enableAnnotationMapping(Reader $annotationReader = null)
  107. {
  108. if (null !== $this->metadataFactory) {
  109. throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
  110. }
  111. $this->annotationReader = $annotationReader ?? $this->createAnnotationReader();
  112. return $this;
  113. }
  114. public function disableAnnotationMapping()
  115. {
  116. $this->annotationReader = null;
  117. return $this;
  118. }
  119. public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
  120. {
  121. if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || null !== $this->annotationReader) {
  122. throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
  123. }
  124. $this->metadataFactory = $metadataFactory;
  125. return $this;
  126. }
  127. public function setMetadataCache(CacheInterface $cache)
  128. {
  129. @\trigger_error(\sprintf('%s is deprecated since Symfony 4.4. Use setMappingCache() instead.', __METHOD__), \E_USER_DEPRECATED);
  130. if (null !== $this->metadataFactory) {
  131. throw new ValidatorException('You cannot set a custom metadata cache after setting a custom metadata factory. Configure your metadata factory instead.');
  132. }
  133. $this->mappingCache = $cache;
  134. return $this;
  135. }
  136. public function setMappingCache(CacheItemPoolInterface $cache)
  137. {
  138. if (null !== $this->metadataFactory) {
  139. throw new ValidatorException('You cannot set a custom mapping cache after setting a custom metadata factory. Configure your metadata factory instead.');
  140. }
  141. $this->mappingCache = $cache;
  142. return $this;
  143. }
  144. public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
  145. {
  146. $this->validatorFactory = $validatorFactory;
  147. return $this;
  148. }
  149. public function setTranslator(LegacyTranslatorInterface $translator)
  150. {
  151. $this->translator = $translator;
  152. while ($this->translator instanceof LegacyTranslatorProxy) {
  153. $this->translator = $this->translator->getTranslator();
  154. }
  155. return $this;
  156. }
  157. public function setTranslationDomain($translationDomain)
  158. {
  159. $this->translationDomain = $translationDomain;
  160. return $this;
  161. }
  162. public function addLoader(LoaderInterface $loader)
  163. {
  164. $this->loaders[] = $loader;
  165. return $this;
  166. }
  167. public function getLoaders()
  168. {
  169. $loaders = [];
  170. foreach ($this->xmlMappings as $xmlMapping) {
  171. $loaders[] = new XmlFileLoader($xmlMapping);
  172. }
  173. foreach ($this->yamlMappings as $yamlMappings) {
  174. $loaders[] = new YamlFileLoader($yamlMappings);
  175. }
  176. foreach ($this->methodMappings as $methodName) {
  177. $loaders[] = new StaticMethodLoader($methodName);
  178. }
  179. if ($this->annotationReader) {
  180. $loaders[] = new AnnotationLoader($this->annotationReader);
  181. }
  182. return \array_merge($loaders, $this->loaders);
  183. }
  184. public function getValidator()
  185. {
  186. $metadataFactory = $this->metadataFactory;
  187. if (!$metadataFactory) {
  188. $loaders = $this->getLoaders();
  189. $loader = null;
  190. if (\count($loaders) > 1) {
  191. $loader = new LoaderChain($loaders);
  192. } elseif (1 === \count($loaders)) {
  193. $loader = $loaders[0];
  194. }
  195. $metadataFactory = new LazyLoadingMetadataFactory($loader, $this->mappingCache);
  196. }
  197. $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
  198. $translator = $this->translator;
  199. if (null === $translator) {
  200. $translator = new class implements TranslatorInterface, LocaleAwareInterface
  201. {
  202. use TranslatorTrait;
  203. };
  204. // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale
  205. // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony
  206. // validation messages are pluralized properly even when the default locale gets changed because they are in
  207. // English.
  208. $translator->setLocale('en');
  209. }
  210. $contextFactory = new ExecutionContextFactory($translator, $this->translationDomain);
  211. return new RecursiveValidator($contextFactory, $metadataFactory, $validatorFactory, $this->initializers);
  212. }
  213. private function createAnnotationReader() : Reader
  214. {
  215. if (!\class_exists(AnnotationReader::class)) {
  216. throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.');
  217. }
  218. // Doctrine Annotation >= 1.13, Symfony Cache
  219. if (\class_exists(PsrCachedReader::class) && \class_exists(ArrayAdapter::class)) {
  220. return new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
  221. }
  222. // Doctrine Annotations < 1.13, Doctrine Cache >= 1.11, Symfony Cache
  223. if (\class_exists(CachedReader::class) && \class_exists(DoctrineProvider::class) && \class_exists(ArrayAdapter::class)) {
  224. return new CachedReader(new AnnotationReader(), DoctrineProvider::wrap(new ArrayAdapter()));
  225. }
  226. // Doctrine Annotations < 1.13, Doctrine Cache < 1.11, Symfony Cache
  227. if (\class_exists(CachedReader::class) && !\class_exists(DoctrineProvider::class) && \class_exists(ArrayAdapter::class)) {
  228. return new CachedReader(new AnnotationReader(), new SymfonyDoctrineProvider(new ArrayAdapter()));
  229. }
  230. // Doctrine Annotations < 1.13, Doctrine Cache < 1.11
  231. if (\class_exists(CachedReader::class) && \class_exists(ArrayCache::class)) {
  232. return new CachedReader(new AnnotationReader(), new ArrayCache());
  233. }
  234. // Doctrine Annotation >= 1.13, Doctrine Cache >= 2, no Symfony Cache
  235. if (\class_exists(PsrCachedReader::class)) {
  236. throw new LogicException('Enabling annotation based constraint mapping requires the package symfony/cache to be installed.');
  237. }
  238. // Doctrine Annotation (<1.13 || >2), no Doctrine Cache, no Symfony Cache
  239. throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations (>=1.13) and symfony/cache to be installed.');
  240. }
  241. }