schema
3 years ago
AbstractLoader.php
4 years ago
AnnotationLoader.php
1 year ago
AutoMappingTrait.php
1 year ago
FileLoader.php
4 years ago
FilesLoader.php
4 years ago
LoaderChain.php
4 years ago
LoaderInterface.php
4 years ago
PropertyInfoLoader.php
1 year ago
StaticMethodLoader.php
4 years ago
XmlFileLoader.php
4 years ago
XmlFilesLoader.php
4 years ago
YamlFileLoader.php
4 years ago
YamlFilesLoader.php
4 years ago
index.php
3 years ago
AnnotationLoader.php
109 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Mapping\Loader; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\Common\Annotations\Reader; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\Constraints\Callback; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Constraints\GroupSequence; |
| 8 | use MailPoetVendor\Symfony\Component\Validator\Constraints\GroupSequenceProvider; |
| 9 | use MailPoetVendor\Symfony\Component\Validator\Exception\MappingException; |
| 10 | use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadata; |
| 11 | class AnnotationLoader implements LoaderInterface |
| 12 | { |
| 13 | protected $reader; |
| 14 | public function __construct(?Reader $reader = null) |
| 15 | { |
| 16 | $this->reader = $reader; |
| 17 | } |
| 18 | public function loadClassMetadata(ClassMetadata $metadata) |
| 19 | { |
| 20 | $reflClass = $metadata->getReflectionClass(); |
| 21 | $className = $reflClass->name; |
| 22 | $success = \false; |
| 23 | foreach ($this->getAnnotations($reflClass) as $constraint) { |
| 24 | if ($constraint instanceof GroupSequence) { |
| 25 | $metadata->setGroupSequence($constraint->groups); |
| 26 | } elseif ($constraint instanceof GroupSequenceProvider) { |
| 27 | $metadata->setGroupSequenceProvider(\true); |
| 28 | } elseif ($constraint instanceof Constraint) { |
| 29 | $metadata->addConstraint($constraint); |
| 30 | } |
| 31 | $success = \true; |
| 32 | } |
| 33 | foreach ($reflClass->getProperties() as $property) { |
| 34 | if ($property->getDeclaringClass()->name === $className) { |
| 35 | foreach ($this->getAnnotations($property) as $constraint) { |
| 36 | if ($constraint instanceof Constraint) { |
| 37 | $metadata->addPropertyConstraint($property->name, $constraint); |
| 38 | } |
| 39 | $success = \true; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | foreach ($reflClass->getMethods() as $method) { |
| 44 | if ($method->getDeclaringClass()->name === $className) { |
| 45 | foreach ($this->getAnnotations($method) as $constraint) { |
| 46 | if ($constraint instanceof Callback) { |
| 47 | $constraint->callback = $method->getName(); |
| 48 | $metadata->addConstraint($constraint); |
| 49 | } elseif ($constraint instanceof Constraint) { |
| 50 | if (\preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { |
| 51 | $metadata->addGetterMethodConstraint(\lcfirst($matches[2]), $matches[0], $constraint); |
| 52 | } else { |
| 53 | throw new MappingException(\sprintf('The constraint on "%s::%s()" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name)); |
| 54 | } |
| 55 | } |
| 56 | $success = \true; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return $success; |
| 61 | } |
| 62 | private function getAnnotations(object $reflection) : iterable |
| 63 | { |
| 64 | $dedup = []; |
| 65 | if (\PHP_VERSION_ID >= 80000) { |
| 66 | foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) { |
| 67 | $dedup[] = $attribute->newInstance(); |
| 68 | (yield $attribute->newInstance()); |
| 69 | } |
| 70 | foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) { |
| 71 | $dedup[] = $attribute->newInstance(); |
| 72 | (yield $attribute->newInstance()); |
| 73 | } |
| 74 | foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 75 | $dedup[] = $attribute->newInstance(); |
| 76 | (yield $attribute->newInstance()); |
| 77 | } |
| 78 | } |
| 79 | if (!$this->reader) { |
| 80 | return; |
| 81 | } |
| 82 | $annotations = []; |
| 83 | if ($reflection instanceof \ReflectionClass) { |
| 84 | $annotations = $this->reader->getClassAnnotations($reflection); |
| 85 | } |
| 86 | if ($reflection instanceof \ReflectionMethod) { |
| 87 | $annotations = $this->reader->getMethodAnnotations($reflection); |
| 88 | } |
| 89 | if ($reflection instanceof \ReflectionProperty) { |
| 90 | $annotations = $this->reader->getPropertyAnnotations($reflection); |
| 91 | } |
| 92 | foreach ($dedup as $annotation) { |
| 93 | if ($annotation instanceof Constraint) { |
| 94 | $annotation->groups; |
| 95 | // trigger initialization of the "groups" property |
| 96 | } |
| 97 | } |
| 98 | foreach ($annotations as $annotation) { |
| 99 | if ($annotation instanceof Constraint) { |
| 100 | $annotation->groups; |
| 101 | // trigger initialization of the "groups" property |
| 102 | } |
| 103 | if (!\in_array($annotation, $dedup, \false)) { |
| 104 | (yield $annotation); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 |