|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Validator\Mapping\Loader;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Validator\Constraint; |
| 15 | +use Symfony\Component\Validator\Constraints\Callback; |
| 16 | +use Symfony\Component\Validator\Constraints\GroupSequence; |
| 17 | +use Symfony\Component\Validator\Constraints\GroupSequenceProvider; |
| 18 | +use Symfony\Component\Validator\Exception\MappingException; |
| 19 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 20 | + |
14 | 21 | /**
|
15 | 22 | * Loads validation metadata using PHP attributes.
|
16 | 23 | *
|
17 | 24 | * @author Bernhard Schussek <[email protected]>
|
18 | 25 | * @author Alexander M. Turek <[email protected]>
|
19 | 26 | * @author Alexandre Daubois <[email protected]>
|
20 | 27 | */
|
21 |
| -class AttributeLoader extends AnnotationLoader |
| 28 | +class AttributeLoader implements LoaderInterface |
22 | 29 | {
|
23 |
| - public function __construct() |
| 30 | + public function loadClassMetadata(ClassMetadata $metadata): bool |
| 31 | + { |
| 32 | + $reflClass = $metadata->getReflectionClass(); |
| 33 | + $className = $reflClass->name; |
| 34 | + $success = false; |
| 35 | + |
| 36 | + foreach ($this->getAttributes($reflClass) as $constraint) { |
| 37 | + if ($constraint instanceof GroupSequence) { |
| 38 | + $metadata->setGroupSequence($constraint->groups); |
| 39 | + } elseif ($constraint instanceof GroupSequenceProvider) { |
| 40 | + $metadata->setGroupSequenceProvider(true); |
| 41 | + } elseif ($constraint instanceof Constraint) { |
| 42 | + $metadata->addConstraint($constraint); |
| 43 | + } |
| 44 | + |
| 45 | + $success = true; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($reflClass->getProperties() as $property) { |
| 49 | + if ($property->getDeclaringClass()->name === $className) { |
| 50 | + foreach ($this->getAttributes($property) as $constraint) { |
| 51 | + if ($constraint instanceof Constraint) { |
| 52 | + $metadata->addPropertyConstraint($property->name, $constraint); |
| 53 | + } |
| 54 | + |
| 55 | + $success = true; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + foreach ($reflClass->getMethods() as $method) { |
| 61 | + if ($method->getDeclaringClass()->name === $className) { |
| 62 | + foreach ($this->getAttributes($method) as $constraint) { |
| 63 | + if ($constraint instanceof Callback) { |
| 64 | + $constraint->callback = $method->getName(); |
| 65 | + |
| 66 | + $metadata->addConstraint($constraint); |
| 67 | + } elseif ($constraint instanceof Constraint) { |
| 68 | + if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { |
| 69 | + $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint); |
| 70 | + } else { |
| 71 | + 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)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $success = true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $success; |
| 81 | + } |
| 82 | + |
| 83 | + private function getAttributes(\ReflectionMethod|\ReflectionClass|\ReflectionProperty $reflection): iterable |
24 | 84 | {
|
25 |
| - parent::__construct(null); |
| 85 | + foreach ($reflection->getAttributes(GroupSequence::class) as $attribute) { |
| 86 | + yield $attribute->newInstance(); |
| 87 | + } |
| 88 | + foreach ($reflection->getAttributes(GroupSequenceProvider::class) as $attribute) { |
| 89 | + yield $attribute->newInstance(); |
| 90 | + } |
| 91 | + foreach ($reflection->getAttributes(Constraint::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 92 | + yield $attribute->newInstance(); |
| 93 | + } |
26 | 94 | }
|
27 | 95 | }
|
0 commit comments