Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Serializer\Mapping\Loader;
|
Chris@0
|
13
|
Chris@0
|
14 use Doctrine\Common\Annotations\Reader;
|
Chris@0
|
15 use Symfony\Component\Serializer\Annotation\Groups;
|
Chris@0
|
16 use Symfony\Component\Serializer\Annotation\MaxDepth;
|
Chris@0
|
17 use Symfony\Component\Serializer\Exception\MappingException;
|
Chris@0
|
18 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
Chris@0
|
19 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Annotation loader.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @author Kévin Dunglas <dunglas@gmail.com>
|
Chris@0
|
25 */
|
Chris@0
|
26 class AnnotationLoader implements LoaderInterface
|
Chris@0
|
27 {
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @var Reader
|
Chris@0
|
30 */
|
Chris@0
|
31 private $reader;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @param Reader $reader
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(Reader $reader)
|
Chris@0
|
37 {
|
Chris@0
|
38 $this->reader = $reader;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * {@inheritdoc}
|
Chris@0
|
43 */
|
Chris@0
|
44 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
Chris@0
|
45 {
|
Chris@0
|
46 $reflectionClass = $classMetadata->getReflectionClass();
|
Chris@0
|
47 $className = $reflectionClass->name;
|
Chris@0
|
48 $loaded = false;
|
Chris@0
|
49
|
Chris@0
|
50 $attributesMetadata = $classMetadata->getAttributesMetadata();
|
Chris@0
|
51
|
Chris@0
|
52 foreach ($reflectionClass->getProperties() as $property) {
|
Chris@0
|
53 if (!isset($attributesMetadata[$property->name])) {
|
Chris@0
|
54 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
|
Chris@0
|
55 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if ($property->getDeclaringClass()->name === $className) {
|
Chris@0
|
59 foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
|
Chris@0
|
60 if ($annotation instanceof Groups) {
|
Chris@0
|
61 foreach ($annotation->getGroups() as $group) {
|
Chris@0
|
62 $attributesMetadata[$property->name]->addGroup($group);
|
Chris@0
|
63 }
|
Chris@0
|
64 } elseif ($annotation instanceof MaxDepth) {
|
Chris@0
|
65 $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 $loaded = true;
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 foreach ($reflectionClass->getMethods() as $method) {
|
Chris@0
|
74 if ($method->getDeclaringClass()->name !== $className) {
|
Chris@0
|
75 continue;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
|
Chris@0
|
79 if ($accessorOrMutator) {
|
Chris@0
|
80 $attributeName = lcfirst($matches[2]);
|
Chris@0
|
81
|
Chris@0
|
82 if (isset($attributesMetadata[$attributeName])) {
|
Chris@0
|
83 $attributeMetadata = $attributesMetadata[$attributeName];
|
Chris@0
|
84 } else {
|
Chris@0
|
85 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
|
Chris@0
|
86 $classMetadata->addAttributeMetadata($attributeMetadata);
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
|
Chris@0
|
91 if ($annotation instanceof Groups) {
|
Chris@0
|
92 if (!$accessorOrMutator) {
|
Chris@0
|
93 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));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 foreach ($annotation->getGroups() as $group) {
|
Chris@0
|
97 $attributeMetadata->addGroup($group);
|
Chris@0
|
98 }
|
Chris@0
|
99 } elseif ($annotation instanceof MaxDepth) {
|
Chris@0
|
100 if (!$accessorOrMutator) {
|
Chris@0
|
101 throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 $attributeMetadata->setMaxDepth($annotation->getMaxDepth());
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 $loaded = true;
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 return $loaded;
|
Chris@0
|
112 }
|
Chris@0
|
113 }
|