-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTokenCollection.php
More file actions
381 lines (348 loc) · 9.75 KB
/
Copy pathTokenCollection.php
File metadata and controls
381 lines (348 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
namespace ConfigToken;
use ConfigToken\TokenFilter\Exception\UnknownFilterException;
use ConfigToken\TokenResolver\Exception\OutOfScopeException;
use ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException;
use ConfigToken\TokenResolver\Exception\TokenFormatException;
use ConfigToken\TokenResolver\Exception\UnknownTokenException;
use ConfigToken\TokenResolver\TokenResolverInterface;
/**
* Generic token collection class used in conjunction with a given token resolver instance to resolve
* tokens to values and apply the given filters.
*/
class TokenCollection implements \IteratorAggregate
{
/** @var Token[] */
protected $tokens =array ();
/** @var string */
protected $sourceHash;
/** @var boolean */
protected $ignoreUnknownFilters = True;
/**
* @param Token[] $tokens Array of Token with tokenString as keys.
*/
public function __construct(array $tokens = null)
{
if (isset($tokens)) {
$this->tokens = $tokens;
}
}
/**
* Get the ignore unknown filters flag.
*
* @return boolean
*/
public function getIgnoreUnknownFilters()
{
return $this->ignoreUnknownFilters;
}
/**
* Set the ignore unknown filters flag.
*
* @param boolean $ignoreUnknownFilters The new value for the ignore unknown filters flag.
* @return $this
*/
public function setIgnoreUnknownFilters($ignoreUnknownFilters)
{
$this->ignoreUnknownFilters = $ignoreUnknownFilters;
return $this;
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->tokens);
}
/**
* Check if the source hash was set.
*
* @return boolean
*/
public function hasSourceHash()
{
return isset($this->sourceHash);
}
/**
* Get the source hash.
*
* @return string|null
*/
public function getSourceHash()
{
if (!$this->hasSourceHash()) {
return null;
}
return $this->sourceHash;
}
/**
* Set the source hash.
*
* @param string $value The new value.
* @return $this
*/
public function setSourceHash($value)
{
$this->sourceHash = $value;
return $this;
}
/**
* Check if the list of tokens is empty.
*
* @return boolean
*/
public function isEmpty()
{
return count($this->tokens) == 0;
}
/**
* Get the number of tokens in the list
*
* @return integer
*/
public function getCount()
{
return count($this->tokens);
}
/**
* Add a new token to the list.
* If another token already exists with the same tokenString then it will be overwritten.
*
* @param Token $token
* @return $this
*/
public function add(Token $token)
{
$this->tokens[$token->getTokenString()] = $token;
return $this;
}
/**
* Import add all tokens from another list.
* Existing tokens will be overwritten. The hash will be unset.
*
* @param TokenCollection $tokens
* @return $this
*/
public function import(TokenCollection $tokens)
{
foreach ($tokens->tokens as $tokenString => $token) {
$this->tokens[$tokenString] = $token;
}
$this->sourceHash = null;
return $this;
}
/**
* Remove the given token from the list.
*
* @param Token $token
* @return $this
*/
public function remove(Token $token)
{
unset($this->tokens[$token->getTokenString()]);
return $this;
}
/**
* Clear the list of tokens.
*
* @return $this
*/
public function clear()
{
$this->tokens = array();
return $this;
}
/**
* Check if a token with the given token string is in this list.
*
* @param string $tokenString
* @return boolean
*/
public function has($tokenString)
{
return isset($this->tokens[$tokenString]);
}
/**
* Get a token from this list having the given token string.
*
* @param string $tokenString
* @return Token
*/
public function get($tokenString)
{
return $this->tokens[$tokenString];
}
/**
* Get the list of tokens as an associative array of Token with tokenString as keys.
*
* @return Token[]
*/
public function getArray()
{
return $this->tokens;
}
/**
* Get all token names.
*
* @return string[]
*/
public function getNames()
{
$result = array();
foreach ($this->tokens as $token) {
$tokenName = $token->getTokenName();
$result[$tokenName] = $tokenName;
}
return $result;
}
/**
* Find tokens having the given token name.
*
* @param string|string[]|null $tokenNameToFind Null to find all. String to find a certain token. Array of strings
* To find multiple tokens.
* @return Token[]|array Array of Token if $tokenNameToFind is string, Array of arrays of Token with token names
* as keys if $tokenNameToFind is string[] or null.
*/
public function findByName($tokenNameToFind = null)
{
$result = array();
foreach ($this->tokens as $tokenString => $token)
{
$tokenName = $token->getTokenName();
if ((!isset($tokenNameToFind)) || (is_array($tokenNameToFind) && in_array($tokenName, $tokenNameToFind)) ||
($tokenName == $tokenNameToFind)) {
if (!isset($result[$tokenName])) {
$result[$tokenName] = array();
}
$result[$tokenName][$tokenString] = $token;
}
}
if (isset($tokenNameToFind) && isset($result[$tokenNameToFind])) {
return $result[$tokenNameToFind];
}
return $result;
}
/**
* Find all resolved tokens.
*
* @return Token[]
*/
public function findResolved()
{
$result = array();
foreach ($this->tokens as $tokenString => $token)
{
if ($token->getIsResolved()) {
$result[$tokenString] = $token;
}
}
return $result;
}
/**
* Find all tokens resolved but not yet injected.
*
* @return Token[]
*/
public function findResolvedAndNotInjected()
{
$result = array();
foreach ($this->tokens as $tokenString => $token)
{
if ($token->getIsResolved() && (!$token->getIsInjected())) {
$result[$tokenString] = $token;
}
}
return $result;
}
/**
* Find all unresolved tokens.
*
* @return Token[]
*/
public function findUnresolved()
{
$result = array();
foreach ($this->tokens as $tokenString => $token)
{
if (!$token->getIsResolved()) {
$result[$tokenString] = $token;
}
}
return $result;
}
/**
* Check if at least one unresolved token exists in the list.
*
* @return boolean
*/
public function hasUnresolved()
{
foreach ($this->tokens as $tokenString => $token)
{
if (!$token->getIsResolved()) {
return True;
}
}
return False;
}
/**
* Find all tokens with unresolved filters.
*
* @return Token[]
*/
public function findWithUnresolvedFilters()
{
$result = array();
foreach ($this->tokens as $tokenString => $token)
{
if ($token->hasUnresolvedFilters()) {
$result[$tokenString] = $token;
}
}
return $result;
}
/**
* Attempt to resolve the values for all tokens in the list.
*
* @param TokenResolverInterface $tokenResolver
* @param boolean|null $ignoreUnknownTokens Null to use token resolver option.
* @param boolean|null $ignoreUnknownFilters Null to use collection option.
* @param LoggerInterface|null $logger
* @throws UnknownFilterException
*
* If using RegisteredTokenResolver:
* @throws UnknownTokenException If the token name was not registered and set not to ignore unknown tokens.
*
* If using ScopeTokenResolver:
* @throws UnknownTokenException
* @throws OutOfScopeException
* @throws ScopeTokenValueSerializationException
* @throws TokenFormatException
* @return $this
*/
public function resolve(TokenResolverInterface $tokenResolver,
$ignoreUnknownTokens = null, $ignoreUnknownFilters = null, LoggerInterface $logger=null)
{
if (is_null($ignoreUnknownTokens)) {
$ignoreUnknownTokens = $tokenResolver->getIgnoreUnknownTokens();
}
if (is_null($ignoreUnknownFilters)) {
$ignoreUnknownFilters = $this->ignoreUnknownFilters;
}
foreach ($this->tokens as $tokenString => $token) {
if ($token->getIsResolved() && (!$token->hasUnresolvedFilters())) {
continue;
}
if (!$token->getIsResolved()) {
$token->resolveUnfilteredValue($tokenResolver, $ignoreUnknownTokens);
}
if ($token->hasFilters() && $token->hasUnfilteredTokenValue()) {
$token->applyFilters($ignoreUnknownFilters);
}
}
return $this;
}
}