Version 1
[yaffs-website] / vendor / symfony / serializer / 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\Serializer\Mapping\Loader;
13
14 use Doctrine\Common\Annotations\Reader;
15 use Symfony\Component\Serializer\Annotation\Groups;
16 use Symfony\Component\Serializer\Exception\MappingException;
17 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
18 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
19
20 /**
21  * Annotation loader.
22  *
23  * @author Kévin Dunglas <dunglas@gmail.com>
24  */
25 class AnnotationLoader implements LoaderInterface
26 {
27     /**
28      * @var Reader
29      */
30     private $reader;
31
32     /**
33      * @param Reader $reader
34      */
35     public function __construct(Reader $reader)
36     {
37         $this->reader = $reader;
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     public function loadClassMetadata(ClassMetadataInterface $classMetadata)
44     {
45         $reflectionClass = $classMetadata->getReflectionClass();
46         $className = $reflectionClass->name;
47         $loaded = false;
48
49         $attributesMetadata = $classMetadata->getAttributesMetadata();
50
51         foreach ($reflectionClass->getProperties() as $property) {
52             if (!isset($attributesMetadata[$property->name])) {
53                 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
54                 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
55             }
56
57             if ($property->getDeclaringClass()->name === $className) {
58                 foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
59                     if ($groups instanceof Groups) {
60                         foreach ($groups->getGroups() as $group) {
61                             $attributesMetadata[$property->name]->addGroup($group);
62                         }
63                     }
64
65                     $loaded = true;
66                 }
67             }
68         }
69
70         foreach ($reflectionClass->getMethods() as $method) {
71             if ($method->getDeclaringClass()->name === $className) {
72                 foreach ($this->reader->getMethodAnnotations($method) as $groups) {
73                     if ($groups instanceof Groups) {
74                         if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
75                             $attributeName = lcfirst($matches[2]);
76
77                             if (isset($attributesMetadata[$attributeName])) {
78                                 $attributeMetadata = $attributesMetadata[$attributeName];
79                             } else {
80                                 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
81                                 $classMetadata->addAttributeMetadata($attributeMetadata);
82                             }
83
84                             foreach ($groups->getGroups() as $group) {
85                                 $attributeMetadata->addGroup($group);
86                             }
87                         } else {
88                             throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
89                         }
90                     }
91
92                     $loaded = true;
93                 }
94             }
95         }
96
97         return $loaded;
98     }
99 }