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\Since; |
26
|
|
|
use Ivory\Serializer\Mapping\Annotation\Type; |
27
|
|
|
use Ivory\Serializer\Mapping\Annotation\Until; |
28
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author GeLo <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class AnnotationClassMetadataLoader extends AbstractReflectionClassMetadataLoader |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Reader |
37
|
|
|
*/ |
38
|
|
|
private $reader; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Reader|null $reader |
42
|
|
|
* @param TypeParserInterface|null $typeParser |
43
|
|
|
*/ |
44
|
630 |
|
public function __construct(Reader $reader = null, TypeParserInterface $typeParser = null) |
45
|
|
|
{ |
46
|
630 |
|
parent::__construct($typeParser); |
47
|
|
|
|
48
|
630 |
|
$this->reader = $reader ?: new AnnotationReader(); |
49
|
630 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
465 |
|
protected function loadClass(\ReflectionClass $class) |
55
|
|
|
{ |
56
|
465 |
|
$definition = parent::loadClass($class); |
57
|
|
|
|
58
|
465 |
|
foreach ($this->reader->getClassAnnotations($class) as $annotation) { |
59
|
105 |
|
if ($annotation instanceof ExclusionPolicy) { |
60
|
42 |
|
$definition['exclusion_policy'] = $annotation->getPolicy(); |
61
|
70 |
|
} elseif ($annotation instanceof Order) { |
62
|
77 |
|
$definition['order'] = $annotation->getOrder(); |
63
|
42 |
|
} |
64
|
310 |
|
} |
65
|
|
|
|
66
|
465 |
|
return $definition; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
462 |
|
protected function loadProperty(\ReflectionProperty $property) |
73
|
|
|
{ |
74
|
462 |
|
return $this->loadAnnotations($this->reader->getPropertyAnnotations($property)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
261 |
|
protected function loadMethod(\ReflectionMethod $method) |
81
|
|
|
{ |
82
|
261 |
|
$result = $this->loadAnnotations($this->reader->getMethodAnnotations($method)); |
83
|
|
|
|
84
|
261 |
|
if (!empty($result)) { |
85
|
39 |
|
return $result; |
86
|
|
|
} |
87
|
261 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param object[] $annotations |
91
|
|
|
* |
92
|
|
|
* @return mixed[] |
93
|
|
|
*/ |
94
|
462 |
|
private function loadAnnotations(array $annotations) |
95
|
|
|
{ |
96
|
462 |
|
$definition = []; |
97
|
|
|
|
98
|
462 |
|
foreach ($annotations as $annotation) { |
99
|
399 |
|
if ($annotation instanceof Alias) { |
100
|
75 |
|
$definition['alias'] = $annotation->getAlias(); |
101
|
266 |
|
} elseif ($annotation instanceof Type) { |
102
|
192 |
|
$definition['type'] = $annotation->getType(); |
103
|
267 |
|
} elseif ($annotation instanceof Expose) { |
104
|
21 |
|
$definition['expose'] = true; |
105
|
164 |
|
} elseif ($annotation instanceof Exclude) { |
106
|
21 |
|
$definition['exclude'] = true; |
107
|
150 |
|
} elseif ($annotation instanceof Accessor) { |
108
|
21 |
|
$definition['accessor'] = $annotation->getAccessor(); |
109
|
136 |
|
} elseif ($annotation instanceof Mutator) { |
110
|
21 |
|
$definition['mutator'] = $annotation->getMutator(); |
111
|
124 |
|
} elseif ($annotation instanceof Since) { |
112
|
57 |
|
$definition['since'] = $annotation->getVersion(); |
113
|
110 |
|
} elseif ($annotation instanceof Until) { |
114
|
57 |
|
$definition['until'] = $annotation->getVersion(); |
115
|
108 |
|
} elseif ($annotation instanceof MaxDepth) { |
116
|
39 |
|
$definition['max_depth'] = $annotation->getMaxDepth(); |
117
|
70 |
|
} elseif ($annotation instanceof Groups) { |
118
|
177 |
|
$definition['groups'] = $annotation->getGroups(); |
119
|
44 |
|
} |
120
|
308 |
|
} |
121
|
|
|
|
122
|
462 |
|
return $definition; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|