Mercurial > hg > cmmr2012-drupal-site
comparison vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\Validator\Mapping\Loader; | |
13 | |
14 use Doctrine\Common\Annotations\Reader; | |
15 use Symfony\Component\Validator\Constraint; | |
16 use Symfony\Component\Validator\Constraints\Callback; | |
17 use Symfony\Component\Validator\Constraints\GroupSequence; | |
18 use Symfony\Component\Validator\Constraints\GroupSequenceProvider; | |
19 use Symfony\Component\Validator\Exception\MappingException; | |
20 use Symfony\Component\Validator\Mapping\ClassMetadata; | |
21 | |
22 /** | |
23 * Loads validation metadata using a Doctrine annotation {@link Reader}. | |
24 * | |
25 * @author Bernhard Schussek <bschussek@gmail.com> | |
26 */ | |
27 class AnnotationLoader implements LoaderInterface | |
28 { | |
29 protected $reader; | |
30 | |
31 public function __construct(Reader $reader) | |
32 { | |
33 $this->reader = $reader; | |
34 } | |
35 | |
36 /** | |
37 * {@inheritdoc} | |
38 */ | |
39 public function loadClassMetadata(ClassMetadata $metadata) | |
40 { | |
41 $reflClass = $metadata->getReflectionClass(); | |
42 $className = $reflClass->name; | |
43 $success = false; | |
44 | |
45 foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { | |
46 if ($constraint instanceof GroupSequence) { | |
47 $metadata->setGroupSequence($constraint->groups); | |
48 } elseif ($constraint instanceof GroupSequenceProvider) { | |
49 $metadata->setGroupSequenceProvider(true); | |
50 } elseif ($constraint instanceof Constraint) { | |
51 $metadata->addConstraint($constraint); | |
52 } | |
53 | |
54 $success = true; | |
55 } | |
56 | |
57 foreach ($reflClass->getProperties() as $property) { | |
58 if ($property->getDeclaringClass()->name === $className) { | |
59 foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { | |
60 if ($constraint instanceof Constraint) { | |
61 $metadata->addPropertyConstraint($property->name, $constraint); | |
62 } | |
63 | |
64 $success = true; | |
65 } | |
66 } | |
67 } | |
68 | |
69 foreach ($reflClass->getMethods() as $method) { | |
70 if ($method->getDeclaringClass()->name === $className) { | |
71 foreach ($this->reader->getMethodAnnotations($method) as $constraint) { | |
72 if ($constraint instanceof Callback) { | |
73 $constraint->callback = $method->getName(); | |
74 | |
75 $metadata->addConstraint($constraint); | |
76 } elseif ($constraint instanceof Constraint) { | |
77 if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { | |
78 $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint); | |
79 } else { | |
80 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)); | |
81 } | |
82 } | |
83 | |
84 $success = true; | |
85 } | |
86 } | |
87 } | |
88 | |
89 return $success; | |
90 } | |
91 } |