1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Mapping\Loader; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
16
|
|
|
use Ivory\Serializer\Mapping\Annotation\Accessor; |
17
|
|
|
use Ivory\Serializer\Mapping\Annotation\Alias; |
18
|
|
|
use Ivory\Serializer\Mapping\Annotation\Exclude; |
19
|
|
|
use Ivory\Serializer\Mapping\Annotation\ExclusionPolicy; |
20
|
|
|
use Ivory\Serializer\Mapping\Annotation\Expose; |
21
|
|
|
use Ivory\Serializer\Mapping\Annotation\Groups; |
22
|
|
|
use Ivory\Serializer\Mapping\Annotation\MaxDepth; |
23
|
|
|
use Ivory\Serializer\Mapping\Annotation\Mutator; |
24
|
|
|
use Ivory\Serializer\Mapping\Annotation\Order; |
25
|
|
|
use Ivory\Serializer\Mapping\Annotation\Readable; |
26
|
|
|
use Ivory\Serializer\Mapping\Annotation\Since; |
27
|
|
|
use Ivory\Serializer\Mapping\Annotation\Type; |
28
|
|
|
use Ivory\Serializer\Mapping\Annotation\Until; |
29
|
|
|
use Ivory\Serializer\Mapping\Annotation\Writable; |
30
|
|
|
use Ivory\Serializer\Mapping\Annotation\XmlAttribute; |
31
|
|
|
use Ivory\Serializer\Mapping\Annotation\XmlCollection; |
32
|
|
|
use Ivory\Serializer\Mapping\Annotation\XmlRoot; |
33
|
|
|
use Ivory\Serializer\Mapping\Annotation\XmlValue; |
34
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @author GeLo <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var Reader |
43
|
|
|
*/ |
44
|
|
|
private $reader; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Reader|null $reader |
48
|
|
|
* @param TypeParserInterface|null $typeParser |
49
|
|
|
*/ |
50
|
1576 |
|
public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null) |
51
|
|
|
{ |
52
|
1576 |
|
parent::__construct($typeParser); |
53
|
|
|
|
54
|
1576 |
|
$this->reader = $reader ?: new AnnotationReader(); |
55
|
1576 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1168 |
|
protected function loadClass(\ReflectionClass $class) |
61
|
|
|
{ |
62
|
1168 |
|
$definition = parent::loadClass($class); |
63
|
|
|
|
64
|
1168 |
|
foreach ($this->reader->getClassAnnotations($class) as $annotation) { |
65
|
324 |
|
if ($annotation instanceof ExclusionPolicy) { |
66
|
72 |
|
$definition['exclusion_policy'] = $annotation->policy; |
67
|
252 |
|
} elseif ($annotation instanceof Order) { |
68
|
108 |
|
$definition['order'] = $annotation->order; |
69
|
144 |
|
} elseif ($annotation instanceof Readable) { |
70
|
36 |
|
$definition['readable'] = $annotation->readable; |
71
|
108 |
|
} elseif ($annotation instanceof Writable) { |
72
|
36 |
|
$definition['writable'] = $annotation->writable; |
73
|
72 |
|
} elseif ($annotation instanceof XmlRoot) { |
74
|
198 |
|
$definition['xml_root'] = $annotation->name; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
1168 |
|
return $definition; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
1164 |
|
protected function loadProperty(\ReflectionProperty $property) |
85
|
|
|
{ |
86
|
1164 |
|
return $this->loadAnnotations($this->reader->getPropertyAnnotations($property)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
456 |
|
protected function loadMethod(\ReflectionMethod $method) |
93
|
|
|
{ |
94
|
456 |
|
$result = $this->loadAnnotations($this->reader->getMethodAnnotations($method)); |
95
|
|
|
|
96
|
456 |
|
if (!empty($result)) { |
97
|
68 |
|
return $result; |
98
|
|
|
} |
99
|
456 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param object[] $annotations |
103
|
|
|
* |
104
|
|
|
* @return mixed[] |
105
|
|
|
*/ |
106
|
1164 |
|
private function loadAnnotations(array $annotations) |
107
|
|
|
{ |
108
|
1164 |
|
$definition = []; |
109
|
|
|
|
110
|
1164 |
|
foreach ($annotations as $annotation) { |
111
|
912 |
|
if ($annotation instanceof Alias) { |
112
|
132 |
|
$definition['alias'] = $annotation->alias; |
113
|
912 |
|
} elseif ($annotation instanceof Type) { |
114
|
336 |
|
$definition['type'] = $annotation->type; |
115
|
644 |
|
} elseif ($annotation instanceof Expose) { |
116
|
36 |
|
$definition['expose'] = true; |
117
|
608 |
|
} elseif ($annotation instanceof Exclude) { |
118
|
36 |
|
$definition['exclude'] = true; |
119
|
572 |
|
} elseif ($annotation instanceof Readable) { |
120
|
72 |
|
$definition['readable'] = $annotation->readable; |
121
|
500 |
|
} elseif ($annotation instanceof Writable) { |
122
|
72 |
|
$definition['writable'] = $annotation->writable; |
123
|
428 |
|
} elseif ($annotation instanceof Accessor) { |
124
|
36 |
|
$definition['accessor'] = $annotation->accessor; |
125
|
392 |
|
} elseif ($annotation instanceof Mutator) { |
126
|
36 |
|
$definition['mutator'] = $annotation->mutator; |
127
|
356 |
|
} elseif ($annotation instanceof Since) { |
128
|
100 |
|
$definition['since'] = $annotation->version; |
129
|
356 |
|
} elseif ($annotation instanceof Until) { |
130
|
100 |
|
$definition['until'] = $annotation->version; |
131
|
256 |
|
} elseif ($annotation instanceof MaxDepth) { |
132
|
68 |
|
$definition['max_depth'] = $annotation->maxDepth; |
133
|
188 |
|
} elseif ($annotation instanceof Groups) { |
134
|
116 |
|
$definition['groups'] = $annotation->groups; |
135
|
72 |
|
} elseif ($annotation instanceof XmlAttribute) { |
136
|
72 |
|
$definition['xml_attribute'] = true; |
137
|
72 |
|
} elseif ($annotation instanceof XmlValue) { |
138
|
36 |
|
$definition['xml_value'] = true; |
139
|
36 |
|
} elseif ($annotation instanceof XmlCollection) { |
140
|
36 |
|
if ($annotation->entry !== null) { |
141
|
36 |
|
$definition['xml_entry'] = $annotation->entry; |
142
|
|
|
} |
143
|
|
|
|
144
|
36 |
|
if ($annotation->entryAttribute !== null) { |
145
|
36 |
|
$definition['xml_entry_attribute'] = $annotation->entryAttribute; |
146
|
|
|
} |
147
|
|
|
|
148
|
36 |
|
if ($annotation->keyAsAttribute !== null) { |
149
|
36 |
|
$definition['xml_key_as_attribute'] = $annotation->keyAsAttribute; |
150
|
|
|
} |
151
|
|
|
|
152
|
36 |
|
if ($annotation->keyAsNode !== null) { |
153
|
36 |
|
$definition['xml_key_as_node'] = $annotation->keyAsNode; |
154
|
|
|
} |
155
|
|
|
|
156
|
36 |
|
if ($annotation->inline !== null) { |
157
|
474 |
|
$definition['xml_inline'] = $annotation->inline; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
1164 |
|
return $definition; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|