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