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 private $reader;
|
Chris@0
|
29
|
Chris@0
|
30 public function __construct(Reader $reader)
|
Chris@0
|
31 {
|
Chris@0
|
32 $this->reader = $reader;
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
Chris@0
|
39 {
|
Chris@0
|
40 $reflectionClass = $classMetadata->getReflectionClass();
|
Chris@0
|
41 $className = $reflectionClass->name;
|
Chris@0
|
42 $loaded = false;
|
Chris@0
|
43
|
Chris@0
|
44 $attributesMetadata = $classMetadata->getAttributesMetadata();
|
Chris@0
|
45
|
Chris@0
|
46 foreach ($reflectionClass->getProperties() as $property) {
|
Chris@0
|
47 if (!isset($attributesMetadata[$property->name])) {
|
Chris@0
|
48 $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
|
Chris@0
|
49 $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 if ($property->getDeclaringClass()->name === $className) {
|
Chris@0
|
53 foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
|
Chris@0
|
54 if ($annotation instanceof Groups) {
|
Chris@0
|
55 foreach ($annotation->getGroups() as $group) {
|
Chris@0
|
56 $attributesMetadata[$property->name]->addGroup($group);
|
Chris@0
|
57 }
|
Chris@0
|
58 } elseif ($annotation instanceof MaxDepth) {
|
Chris@0
|
59 $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 $loaded = true;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 foreach ($reflectionClass->getMethods() as $method) {
|
Chris@0
|
68 if ($method->getDeclaringClass()->name !== $className) {
|
Chris@0
|
69 continue;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
|
Chris@0
|
73 if ($accessorOrMutator) {
|
Chris@0
|
74 $attributeName = lcfirst($matches[2]);
|
Chris@0
|
75
|
Chris@0
|
76 if (isset($attributesMetadata[$attributeName])) {
|
Chris@0
|
77 $attributeMetadata = $attributesMetadata[$attributeName];
|
Chris@0
|
78 } else {
|
Chris@0
|
79 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
|
Chris@0
|
80 $classMetadata->addAttributeMetadata($attributeMetadata);
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
|
Chris@0
|
85 if ($annotation instanceof Groups) {
|
Chris@0
|
86 if (!$accessorOrMutator) {
|
Chris@0
|
87 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
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 foreach ($annotation->getGroups() as $group) {
|
Chris@0
|
91 $attributeMetadata->addGroup($group);
|
Chris@0
|
92 }
|
Chris@0
|
93 } elseif ($annotation instanceof MaxDepth) {
|
Chris@0
|
94 if (!$accessorOrMutator) {
|
Chris@0
|
95 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
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 $attributeMetadata->setMaxDepth($annotation->getMaxDepth());
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 $loaded = true;
|
Chris@0
|
102 }
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 return $loaded;
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|