1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Minime\Annotations; |
4
|
|
|
|
5
|
|
|
use Minime\Annotations\Interfaces\AnnotationsBagInterface; |
6
|
|
|
use RegexGuard\Factory as RegexGuard; |
7
|
|
|
use ArrayIterator; |
8
|
|
|
use RegexIterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An annotation collection class |
12
|
|
|
* |
13
|
|
|
* @package Annotations |
14
|
|
|
* @author Márcio Almada and the Minime Community |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class AnnotationsBag implements AnnotationsBagInterface |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Associative arrays of annotations |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $attributes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Constructor |
30
|
|
|
* |
31
|
|
|
* @param array $attributes |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $attributes) |
34
|
|
|
{ |
35
|
|
|
$this->attributes = $attributes; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Unbox all annotations in the form of an associative array |
40
|
|
|
* |
41
|
|
|
* @return array associative array of annotations |
42
|
|
|
*/ |
43
|
|
|
public function toArray() |
44
|
|
|
{ |
45
|
|
|
return $this->attributes; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Checks if a given annotation is declared |
50
|
|
|
* |
51
|
|
|
* @param string $key A valid annotation tag, should match parser rules |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
|
|
public function has($key) |
55
|
|
|
{ |
56
|
|
|
return array_key_exists($key, $this->attributes); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set a single annotation value |
61
|
|
|
* |
62
|
|
|
* @param string $key a valid annotation tag, should match parser rules |
63
|
|
|
* @param mixed $value the param value |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public function set($key, $value) |
67
|
|
|
{ |
68
|
|
|
$this->attributes[$key] = $value; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves a single annotation value |
75
|
|
|
* |
76
|
|
|
* @param string $key A valid annotation tag, should match parser rules |
77
|
|
|
* @param mixed $defaut Default value in case $key is not set |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
public function get($key, $defaut = null) |
81
|
|
|
{ |
82
|
|
|
if ($this->has($key)) { |
83
|
|
|
return $this->attributes[$key]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $defaut; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieve annotation values as an array even if there's only one single value |
91
|
|
|
* |
92
|
|
|
* @param string $key A valid annotation tag, should match parser rules |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getAsArray($key) |
96
|
|
|
{ |
97
|
|
|
if (! $this->has($key)) { |
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
$res = $this->attributes[$key]; |
101
|
|
|
if (is_null($res)) { |
102
|
|
|
return [null]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return (array) $res; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Filters annotations based on a regexp |
110
|
|
|
* |
111
|
|
|
* @param string $pattern valid regexp |
112
|
|
|
* @return \Minime\Annotations\AnnotationsBag Annotations collection with filtered results |
113
|
|
|
*/ |
114
|
|
|
public function grep($pattern) |
115
|
|
|
{ |
116
|
|
|
$results = array_intersect_key($this->attributes, array_flip( |
117
|
|
|
RegexGuard::getGuard()->grep($pattern, array_keys($this->attributes)) |
118
|
|
|
)); |
119
|
|
|
|
120
|
|
|
return new static($results); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Isolates a given namespace of annotations. |
125
|
|
|
* |
126
|
|
|
* @param string $pattern namespace |
127
|
|
|
* @param array $delimiters possible namespace delimiters to mask |
128
|
|
|
* @return \Minime\Annotations\AnnotationsBag |
129
|
|
|
*/ |
130
|
|
|
public function useNamespace($pattern, array $delimiters = ['.', '\\']) |
131
|
|
|
{ |
132
|
|
|
$mask = implode('', $delimiters); |
133
|
|
|
$consumer = '(' . implode('|', array_map('preg_quote', $delimiters)) .')'; |
134
|
|
|
$namespace_pattern = '/^' . preg_quote(rtrim($pattern, $mask)) . $consumer . '/'; |
135
|
|
|
$iterator = new RegexIterator( |
136
|
|
|
$this->getIterator(), $namespace_pattern, RegexIterator::REPLACE, RegexIterator::USE_KEY); |
137
|
|
|
$iterator->replacement = ''; |
138
|
|
|
|
139
|
|
|
return new static(iterator_to_array($iterator)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Performs union operations against a given AnnotationsBag |
144
|
|
|
* |
145
|
|
|
* @param AnnotationsBag $bag The annotation bag to be united |
146
|
|
|
* @return \Minime\Annotations\AnnotationsBag Annotations collection with union results |
147
|
|
|
*/ |
148
|
|
|
public function union(AnnotationsBagInterface $bag) |
149
|
|
|
{ |
150
|
|
|
return new static($this->attributes + $bag->toArray()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Countable |
155
|
|
|
*/ |
156
|
|
|
public function count() |
157
|
|
|
{ |
158
|
|
|
return count($this->attributes); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* JsonSerializable |
163
|
|
|
*/ |
164
|
|
|
public function jsonSerialize() |
165
|
|
|
{ |
166
|
|
|
return $this->toArray(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* IteratorAggregate |
171
|
|
|
*/ |
172
|
|
|
public function getIterator() |
173
|
|
|
{ |
174
|
|
|
return new ArrayIterator($this->attributes); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* ArrayAccess - Whether or not an offset exists. |
179
|
|
|
*/ |
180
|
|
|
public function offsetExists($key) |
181
|
|
|
{ |
182
|
|
|
return $this->has($key); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* ArrayAccess - Returns the value at specified offset. |
187
|
|
|
*/ |
188
|
|
|
public function offsetGet($key) |
189
|
|
|
{ |
190
|
|
|
return $this->get($key); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* ArrayAccess - Assigns a value to the specified offset. |
195
|
|
|
*/ |
196
|
|
|
public function offsetSet($key, $value) |
197
|
|
|
{ |
198
|
|
|
$this->set($key, $value); |
199
|
|
|
|
200
|
|
|
return true; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* ArrayAccess - Unsets an offset. |
205
|
|
|
*/ |
206
|
|
|
public function offsetUnset($key) |
207
|
|
|
{ |
208
|
|
|
unset($this->attributes[$key]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|