1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection\Annotation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A collection of parsed types. |
7
|
|
|
*/ |
8
|
|
|
class AnnotationsCollection |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The parsed annotations definitions. |
13
|
|
|
* |
14
|
|
|
* @var \Wingu\OctopusCore\Reflection\Annotation\AnnotationDefinition[] |
15
|
|
|
*/ |
16
|
|
|
protected $annotationsDefinitions = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The annotation tags mapper. |
20
|
|
|
* |
21
|
|
|
* @var \Wingu\OctopusCore\Reflection\Annotation\TagMapper |
22
|
|
|
*/ |
23
|
|
|
protected $tagsMapper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param string $comment The comment string from which to extract the annotations. |
29
|
|
|
*/ |
30
|
129 |
|
public function __construct($comment) |
31
|
|
|
{ |
32
|
129 |
|
$parser = new Parser($comment); |
33
|
129 |
|
$this->annotationsDefinitions = $parser->getFoundAnnotationDefinitions(); |
34
|
129 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the annotation tag mapper. |
38
|
|
|
* |
39
|
|
|
* @param \Wingu\OctopusCore\Reflection\Annotation\TagMapper $tm The tag mapper to set. |
40
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\AnnotationsCollection |
41
|
|
|
*/ |
42
|
15 |
|
public function setTagMapper(TagMapper $tm) |
43
|
|
|
{ |
44
|
15 |
|
$this->tagsMapper = $tm; |
45
|
|
|
|
46
|
15 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the annotation mapper. |
51
|
|
|
* |
52
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\TagMapper |
53
|
|
|
*/ |
54
|
3 |
|
public function getTagMapper() |
55
|
|
|
{ |
56
|
3 |
|
return $this->tagsMapper; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check if the collection contains an annotation tag. |
61
|
|
|
* |
62
|
|
|
* @param string $tag The annotation tag name to check. |
63
|
|
|
* @return boolean |
64
|
|
|
*/ |
65
|
36 |
|
public function hasAnnotationTag($tag) |
66
|
|
|
{ |
67
|
36 |
|
return isset($this->annotationsDefinitions[$tag]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get all annotations. |
72
|
|
|
* |
73
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\Tags\TagInterface[] |
74
|
|
|
*/ |
75
|
42 |
|
public function getAnnotations() |
76
|
|
|
{ |
77
|
42 |
|
$result = array(); |
78
|
42 |
|
foreach ($this->annotationsDefinitions as $annotationDefinition) { |
79
|
12 |
|
$result = array_merge($result, $this->buildAnnotationsInstances($annotationDefinition)); |
|
|
|
|
80
|
14 |
|
} |
81
|
|
|
|
82
|
42 |
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get an annotation. |
87
|
|
|
* |
88
|
|
|
* @param string $tag The annotation tag name. |
89
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\Tags\TagInterface[] |
90
|
|
|
* @throws \Wingu\OctopusCore\Reflection\Annotation\Exceptions\OutOfBoundsException If there are no annotations with the specified tag. |
91
|
|
|
*/ |
92
|
21 |
|
public function getAnnotation($tag) |
93
|
|
|
{ |
94
|
21 |
|
if ($this->hasAnnotationTag($tag) === true) { |
95
|
18 |
|
$result = array(); |
96
|
18 |
|
foreach ($this->annotationsDefinitions[$tag] as $annotationDefinition) { |
|
|
|
|
97
|
18 |
|
$result[] = $this->buildAnnotationInstance($annotationDefinition); |
98
|
6 |
|
} |
99
|
|
|
|
100
|
18 |
|
return $result; |
101
|
|
|
} else { |
102
|
3 |
|
throw new Exceptions\OutOfBoundsException('No annotations with the tag "' . $tag . '" were found.'); |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the annotation class to use for an annotation tag. |
108
|
|
|
* |
109
|
|
|
* @param string $tag The annotation tag name. |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
54 |
|
protected function getAnnotationClass($tag) |
113
|
|
|
{ |
114
|
54 |
|
if ($this->tagsMapper !== null && $this->tagsMapper->hasMappedTag($tag) === true) { |
115
|
12 |
|
return $this->tagsMapper->getMappedTag($tag); |
116
|
42 |
|
} elseif (class_exists(__NAMESPACE__ . '\Tags\\' . ucfirst($tag) . 'Tag') === true) { |
117
|
9 |
|
return __NAMESPACE__ . '\Tags\\' . ucfirst($tag) . 'Tag'; |
118
|
|
|
} else { |
119
|
33 |
|
return __NAMESPACE__ . '\Tags\BaseTag'; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Build all the annotation instances for an annotation tag. |
125
|
|
|
* |
126
|
|
|
* @param \Wingu\OctopusCore\Reflection\Annotation\AnnotationDefinition[] $annotationDefinitions The annotation tag name. |
127
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\Tags\TagInterface[] |
128
|
|
|
*/ |
129
|
12 |
|
protected function buildAnnotationsInstances(array $annotationDefinitions) |
130
|
|
|
{ |
131
|
12 |
|
$result = array(); |
132
|
12 |
|
foreach ($annotationDefinitions as $annotationDefinition) { |
133
|
12 |
|
$result[] = $this->buildAnnotationInstance($annotationDefinition); |
134
|
4 |
|
} |
135
|
|
|
|
136
|
12 |
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Build an annotation. |
141
|
|
|
* |
142
|
|
|
* @param \Wingu\OctopusCore\Reflection\Annotation\AnnotationDefinition $annotationDefinition The annotation definition. |
143
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\Tags\TagInterface |
144
|
|
|
*/ |
145
|
30 |
|
protected function buildAnnotationInstance(AnnotationDefinition $annotationDefinition) |
146
|
|
|
{ |
147
|
30 |
|
$class = $this->getAnnotationClass($annotationDefinition->getTag()); |
148
|
30 |
|
$annotation = new $class($annotationDefinition); |
149
|
|
|
|
150
|
30 |
|
return $annotation; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: