1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Annotations\Metadata; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Parser\Scope; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use ReflectionMethod; |
10
|
|
|
use ReflectionProperty; |
11
|
|
|
use function array_combine; |
12
|
|
|
use function array_map; |
13
|
|
|
use function reset; |
14
|
|
|
|
15
|
|
|
final class AnnotationMetadata |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private $name; |
19
|
|
|
|
20
|
|
|
/** @var AnnotationTarget */ |
21
|
|
|
private $target; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $hasConstructor; |
25
|
|
|
|
26
|
|
|
/** @var PropertyMetadata[] */ |
27
|
|
|
private $properties; |
28
|
|
|
|
29
|
|
|
/** @var PropertyMetadata|null */ |
30
|
|
|
private $defaultProperty; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* TODO: Validate input |
34
|
|
|
* |
35
|
|
|
* @param PropertyMetadata[] $properties |
36
|
|
|
*/ |
37
|
26 |
|
public function __construct( |
38
|
|
|
string $name, |
39
|
|
|
AnnotationTarget $target, |
40
|
|
|
bool $hasConstructor, |
41
|
|
|
array $properties = [] |
42
|
|
|
) { |
43
|
26 |
|
$this->name = $name; |
44
|
26 |
|
$this->target = $target; |
45
|
26 |
|
$this->hasConstructor = $hasConstructor; |
46
|
26 |
|
$this->properties = array_combine( |
47
|
|
|
array_map(static function (PropertyMetadata $property) : string { |
48
|
7 |
|
return $property->getName(); |
49
|
26 |
|
}, $properties), |
50
|
26 |
|
$properties |
51
|
|
|
); |
52
|
|
|
|
53
|
26 |
|
$firstProperty = reset($this->properties); |
54
|
26 |
|
$this->defaultProperty = $firstProperty ?: null; |
55
|
26 |
|
} |
56
|
|
|
|
57
|
19 |
|
public function getName() : string |
58
|
|
|
{ |
59
|
19 |
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
28 |
|
public function getTarget() : AnnotationTarget |
63
|
|
|
{ |
64
|
28 |
|
return $this->target; |
65
|
|
|
} |
66
|
|
|
|
67
|
11 |
|
public function hasConstructor() : bool |
68
|
|
|
{ |
69
|
11 |
|
return $this->hasConstructor; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return PropertyMetadata[] |
74
|
|
|
*/ |
75
|
6 |
|
public function getProperties() : array |
76
|
|
|
{ |
77
|
6 |
|
return $this->properties; |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
public function getDefaultProperty() : ?PropertyMetadata |
81
|
|
|
{ |
82
|
7 |
|
return $this->defaultProperty; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return true |
87
|
|
|
* |
88
|
|
|
* @throws InvalidTarget |
89
|
|
|
*/ |
90
|
28 |
|
public function validateTarget(Scope $scope) : void |
91
|
|
|
{ |
92
|
28 |
|
$target = $this->getTarget(); |
93
|
|
|
|
94
|
28 |
|
if ($target->all()) { |
95
|
18 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
if ($scope->isNested()) { |
99
|
4 |
|
if (! $target->annotation()) { |
100
|
3 |
|
throw InvalidTarget::annotation($this); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
$subject = $scope->getSubject(); |
107
|
|
|
|
108
|
7 |
|
if ($subject instanceof ReflectionClass && ! $target->class()) { |
109
|
1 |
|
throw InvalidTarget::class($this); |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
if ($subject instanceof ReflectionProperty && ! $target->property()) { |
113
|
1 |
|
throw InvalidTarget::property($this); |
114
|
|
|
} |
115
|
|
|
|
116
|
5 |
|
if ($subject instanceof ReflectionMethod && ! $target->method()) { |
117
|
1 |
|
throw InvalidTarget::method($this); |
118
|
|
|
} |
119
|
4 |
|
} |
120
|
|
|
} |
121
|
|
|
|