b09f5a669bac07a8ede309810f2aea459582d8da
[yaffs-website] / vendor / symfony / validator / Mapping / Loader / AnnotationLoader.php
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     /**
30      * @var Reader
31      */
32     protected $reader;
33
34     public function __construct(Reader $reader)
35     {
36         $this->reader = $reader;
37     }
38
39     /**
40      * {@inheritdoc}
41      */
42     public function loadClassMetadata(ClassMetadata $metadata)
43     {
44         $reflClass = $metadata->getReflectionClass();
45         $className = $reflClass->name;
46         $success = false;
47
48         foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
49             if ($constraint instanceof GroupSequence) {
50                 $metadata->setGroupSequence($constraint->groups);
51             } elseif ($constraint instanceof GroupSequenceProvider) {
52                 $metadata->setGroupSequenceProvider(true);
53             } elseif ($constraint instanceof Constraint) {
54                 $metadata->addConstraint($constraint);
55             }
56
57             $success = true;
58         }
59
60         foreach ($reflClass->getProperties() as $property) {
61             if ($property->getDeclaringClass()->name === $className) {
62                 foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
63                     if ($constraint instanceof Constraint) {
64                         $metadata->addPropertyConstraint($property->name, $constraint);
65                     }
66
67                     $success = true;
68                 }
69             }
70         }
71
72         foreach ($reflClass->getMethods() as $method) {
73             if ($method->getDeclaringClass()->name === $className) {
74                 foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
75                     if ($constraint instanceof Callback) {
76                         $constraint->callback = $method->getName();
77
78                         $metadata->addConstraint($constraint);
79                     } elseif ($constraint instanceof Constraint) {
80                         if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
81                             $metadata->addGetterMethodConstraint(lcfirst($matches[2]), $matches[0], $constraint);
82                         } else {
83                             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));
84                         }
85                     }
86
87                     $success = true;
88                 }
89             }
90         }
91
92         return $success;
93     }
94 }