|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Metadata\Driver\AttributeDriver; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use ReflectionMethod; |
|
10
|
|
|
use ReflectionProperty; |
|
11
|
|
|
|
|
12
|
|
|
class AttributeReader implements Reader |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Reader |
|
16
|
|
|
*/ |
|
17
|
|
|
private $reader; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Reader $reader) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->reader = $reader; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getClassAnnotations(ReflectionClass $class) |
|
25
|
|
|
{ |
|
26
|
|
|
$attributes = $class->getAttributes(); |
|
27
|
|
|
|
|
28
|
|
|
return array_merge($this->reader->getClassAnnotations($class), $this->buildAnnotations($attributes)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getClassAnnotation(ReflectionClass $class, $annotationName) |
|
32
|
|
|
{ |
|
33
|
|
|
$attributes = $class->getAttributes($annotationName); |
|
34
|
|
|
|
|
35
|
|
|
return $this->reader->getClassAnnotation($class, $annotationName) ?? $this->buildAnnotation($attributes); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getMethodAnnotations(ReflectionMethod $method) |
|
39
|
|
|
{ |
|
40
|
|
|
$attributes = $method->getAttributes(); |
|
41
|
|
|
|
|
42
|
|
|
return array_merge($this->reader->getMethodAnnotations($method), $this->buildAnnotations($attributes)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getMethodAnnotation(ReflectionMethod $method, $annotationName) |
|
46
|
|
|
{ |
|
47
|
|
|
$attributes = $method->getAttributes($annotationName); |
|
48
|
|
|
|
|
49
|
|
|
return $this->reader->getClassAnnotation($method, $annotationName) ?? $this->buildAnnotation($attributes); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getPropertyAnnotations(ReflectionProperty $property) |
|
53
|
|
|
{ |
|
54
|
|
|
$attributes = $property->getAttributes(); |
|
55
|
|
|
|
|
56
|
|
|
return array_merge($this->reader->getPropertyAnnotations($property), $this->buildAnnotations($attributes)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) |
|
60
|
|
|
{ |
|
61
|
|
|
$attributes = $property->getAttributes($annotationName); |
|
62
|
|
|
|
|
63
|
|
|
return $this->reader->getClassAnnotation($property, $annotationName) ?? $this->buildAnnotation($attributes); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function buildAnnotation(array $attributes): ?object |
|
67
|
|
|
{ |
|
68
|
|
|
if (!isset($attributes[0])) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $attributes[0]->newInstance(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function buildAnnotations(array $attributes): array |
|
76
|
|
|
{ |
|
77
|
|
|
$result = []; |
|
78
|
|
|
foreach ($attributes as $attribute) { |
|
79
|
|
|
if (0 === strpos($attribute->getName(), 'JMS\Serializer\Annotation\\')) { |
|
80
|
|
|
$result[] = $attribute->newInstance(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|