Token::hasFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ConfigToken;
4
5
use ConfigToken\TokenFilter\TokenFilterFactory;
6
use ConfigToken\TokenFilter\Exception\UnknownFilterException;
7
use ConfigToken\TokenResolver\Exception\OutOfScopeException;
8
use ConfigToken\TokenResolver\Exception\ScopeTokenValueSerializationException;
9
use ConfigToken\TokenResolver\Exception\TokenFormatException;
10
use ConfigToken\TokenResolver\Exception\UnknownTokenException;
11
use ConfigToken\TokenResolver\TokenResolverInterface;
12
13
14
class Token
15
{
16
    /** @var integer[] */
17
    protected $offsets;
18
    /** @var string eg. [[tokenName|filter]] */
19
    protected $tokenString;
20
    /** @var string eg. tokenName */
21
    protected $tokenName;
22
    /** @var mixed */
23
    protected $tokenValue = null;
24
    /** @var mixed */
25
    protected $unfilteredTokenValue = null;
26
    /** @var boolean */
27
    protected $isResolved = False;
28
    /** @var boolean */
29
    protected $isFiltered = False;
30
    /** @var boolean */
31
    protected $isInjected = False;
32
    /** @var string[] */
33
    protected $filters;
34
    /** @var string[] */
35
    protected $unresolvedFilters;
36
37
    /**
38
     * @param integer $offset The offset where the token string is positioned in the string to be injected.
39
     * @param string $tokenString The token string. (eg. [[tokenName|filter1]])
40
     * @param string $tokenName The name part of the token string.
41
     * @param string|null $tokenValue The resolved value of the token. Null if not resolved.
42
     * @param array $filters Array of filter names applied to the value.
43
     * @param array $unresolvedFilters Array of filter names that could not be applied to the value.
44
     */
45 14
    public function __construct($offset, $tokenString, $tokenName, $tokenValue = null, array $filters = array(),
46
                         array $unresolvedFilters = array())
47
    {
48 14
        $this->tokenString = $tokenString;
49 14
        $this->setTokenName($tokenName);
50 14
        $this->setUnfilteredTokenValue(null);
51 14
        $this->setTokenValue($tokenValue);
52 14
        $this->setOffsets(array($offset => $offset));
53 14
        $this->setFilters($filters);
54 14
        $this->setUnresolvedFilters($unresolvedFilters);
55 14
    }
56
57
    /**
58
     * Get the token string.
59
     *
60
     * @return string
61
     */
62 14
    public function getTokenString()
63
    {
64 14
        return $this->tokenString;
65
    }
66
67
    /**
68
     * Get the token name.
69
     *
70
     * @return string
71
     */
72 1
    public function getTokenName()
73
    {
74 1
        return $this->tokenName;
75
    }
76
77
    /**
78
     * Set token name.
79
     *
80
     * @param string $value The new value.
81
     * @return $this
82
     */
83 14
    public function setTokenName($value)
84
    {
85 14
        $this->tokenName = $value;
86 14
        return $this;
87
    }
88
89
    /**
90
     * Check if the token value was set.
91
     *
92
     * @return boolean
93
     */
94 8
    public function hasTokenValue()
95
    {
96 8
        return isset($this->tokenValue);
97
    }
98
99
    /**
100
     * Get the token value.
101
     *
102
     * @return string|null
103
     */
104 8
    public function getTokenValue()
105
    {
106 8
        if (!$this->hasTokenValue()) {
107 1
            return null;
108
        }
109 8
        return $this->tokenValue;
110
    }
111
112
    /**
113
     * Set the token value.
114
     *
115
     * @param string $value The new value.
116
     * @return $this
117
     */
118 14
    public function setTokenValue($value)
119
    {
120 14
        if (isset($value) && (!isset($this->unfilteredTokenValue))) {
121
            $this->setUnfilteredTokenValue($value);
122
        }
123 14
        $this->tokenValue = $value;
124 14
        $this->setIsFiltered(isset($value));
125 14
        return $this;
126
    }
127
128
    /**
129
     * Check if the unfiltered token value was set.
130
     *
131
     * @return boolean
132
     */
133 3
    public function hasUnfilteredTokenValue()
134
    {
135 3
        return isset($this->unfilteredTokenValue);
136
    }
137
138
    /**
139
     * Get the unfiltered token value.
140
     *
141
     * @return string|null
142
     */
143
    public function getUnfilteredTokenValue()
144
    {
145
        if (!$this->hasUnfilteredTokenValue()) {
146
            return null;
147
        }
148
        return $this->unfilteredTokenValue;
149
    }
150
151
    /**
152
     * Set the unfiltered token value.
153
     *
154
     * @param string $value The new value.
155
     * @return $this
156
     */
157 14
    public function setUnfilteredTokenValue($value)
158
    {
159 14
        $this->unfilteredTokenValue = $value;
160 14
        $this->tokenValue = $value;
161 14
        $this->setIsResolved(isset($value));
162 14
        $this->setIsFiltered(False);
163 14
        $this->setUnresolvedFilters($this->filters);
164 14
        return $this;
165
    }
166
167
    /**
168
     * Get if the token value is resolved.
169
     *
170
     * @return boolean
171
     */
172 14
    public function getIsResolved()
173
    {
174 14
        return $this->isResolved;
175
    }
176
177
    /**
178
     * Set if the token value is resolved.
179
     *
180
     * @param boolean $value The new value.
181
     * @return $this
182
     */
183 14
    public function setIsResolved($value)
184
    {
185 14
        $this->isResolved = $value;
186 14
        return $this;
187
    }
188
189
    /**
190
     * Get if the token value is filtered.
191
     *
192
     * @return boolean
193
     */
194 1
    public function getIsFiltered()
195
    {
196 1
        return $this->isFiltered;
197
    }
198
199
    /**
200
     * Set if the token value is filtered.
201
     *
202
     * @param boolean $value The new value.
203
     * @return $this
204
     */
205 14
    public function setIsFiltered($value)
206
    {
207 14
        $this->isFiltered = $value;
208 14
        return $this;
209
    }
210
211
    /**
212
     * Get if the token value was injected.
213
     *
214
     * @return boolean
215
     */
216 7
    public function getIsInjected()
217
    {
218 7
        return $this->isInjected;
219
    }
220
221
    /**
222
     * Set if the token value was injected.
223
     *
224
     * @param boolean $value The new value.
225
     * @return $this
226
     */
227 7
    public function setIsInjected($value)
228
    {
229 7
        $this->isInjected = $value;
230 7
        return $this;
231
    }
232
233
    /**
234
     * Get the array of offsets.
235
     *
236
     * @return integer[]
237
     */
238 7
    public function getOffsets()
239
    {
240 7
        return $this->offsets;
241
    }
242
243
    /**
244
     * Set the array of offsets.
245
     *
246
     * @param integer[] $value The new array of offsets.
247
     * @return $this
248
     */
249 14
    public function setOffsets($value)
250
    {
251 14
        $this->offsets = $value;
252 14
        return $this;
253
    }
254
255
    /**
256
     * Add a new offset.
257
     *
258
     * @param integer $offset
259
     * @return $this
260
     */
261 1
    public function addOffset($offset)
262
    {
263 1
        $this->offsets[$offset] = $offset;
264 1
        return $this;
265
    }
266
267
    /**
268
     * Adjust the given offset by adding the given delta value.
269
     *
270
     * @param integer $offset
271
     * @param integer $delta
272
     * @return $this
273
     */
274 7
    public function adjustOffset($offset, $delta)
275
    {
276 7
        if ($delta == 0) {
277 7
            return $this;
278
        }
279 2
        unset($this->offsets[$offset]);
280 2
        $newOffset = $offset + $delta;
281 2
        $this->offsets[$newOffset] = $newOffset;
282 2
        return $this;
283
    }
284
285
    /**
286
     * Check if the filters array was set.
287
     *
288
     * @return boolean
289
     */
290 10
    public function hasFilters()
291
    {
292 10
        return !empty($this->filters);
293
    }
294
295
    /**
296
     * Get the filters array.
297
     *
298
     * @return string[]
299
     */
300 1
    public function getFilters()
301
    {
302 1
        return $this->filters;
303
    }
304
305
    /**
306
     * Set the filters array.
307
     *
308
     * @param string[] $value The new value.
309
     * @return $this
310
     */
311 14
    public function setFilters($value)
312
    {
313 14
        $this->filters = $value;
314 14
        return $this;
315
    }
316
317
    /**
318
     * Check if the unresolved filters array was set.
319
     *
320
     * @return boolean
321
     */
322 1
    public function hasUnresolvedFilters()
323
    {
324 1
        return !empty($this->unresolvedFilters);
325
    }
326
327
    /**
328
     * Get the unresolved filters array.
329
     *
330
     * @return string[]
331
     */
332 1
    public function getUnresolvedFilters()
333
    {
334 1
        return $this->unresolvedFilters;
335
    }
336
337
    /**
338
     * Set the unresolved filters array.
339
     *
340
     * @param string[] $value The new value.
341
     * @return $this
342
     */
343 14
    public function setUnresolvedFilters($value)
344
    {
345 14
        $this->unresolvedFilters = $value;
346 14
        return $this;
347
    }
348
349
    /**
350
     * @param TokenResolverInterface $tokenResolver
351
     * @param boolean $ignoreUnknownTokens
352
     * @return $this
353
     */
354 14
    public function resolveUnfilteredValue(TokenResolverInterface $tokenResolver, $ignoreUnknownTokens = True)
355
    {
356 14
        $tokenValueUnfiltered = $tokenResolver->getTokenValue(
357 14
            $this->tokenName,
358 14
            $ignoreUnknownTokens,
359 14
            $this->unfilteredTokenValue
360 14
        );
361
362 10
        $this->setUnfilteredTokenValue($tokenValueUnfiltered);
363 10
        return $this;
364
    }
365
366
    /**
367
     * @param boolean $ignoreUnknownFilters
368
     * @throws UnknownFilterException
369
     * @return $this
370
     */
371 3
    public function applyFilters($ignoreUnknownFilters = True)
372
    {
373 3
        $unresolvedFilters = array();
374 3
        $filteredValue = $this->unfilteredTokenValue;
375 3
        foreach ($this->filters as $filterName) {
376 3
            if (!TokenFilterFactory::isRegisteredByName($filterName)) {
377 1
                if (!$ignoreUnknownFilters) {
378 1
                    throw new UnknownFilterException($filterName);
379
                }
380 1
                $unresolvedFilters[$filterName] = $filterName;
381 1
                continue;
382
            }
383 3
            $filteredValue = TokenFilterFactory::getFilteredValue($filterName, $filteredValue, $ignoreUnknownFilters);
384 3
        }
385 3
        $this->setTokenValue($filteredValue);
386 3
        $this->unresolvedFilters = $unresolvedFilters;
387 3
        return $this;
388
    }
389
390
    /**
391
     * Attempt to resolve and filter the token value.
392
     *
393
     * @param TokenResolverInterface $tokenResolver
394
     * @param boolean $ignoreUnknownTokens
395
     * @param boolean $ignoreUnknownFilters
396
     * @throws UnknownFilterException
397
     *
398
     * If using RegisteredTokenResolver:
399
     * @throws UnknownTokenException If the token name was not registered and set not to ignore unknown tokens.
400
     *
401
     * If using ScopeTokenResolver:
402
     * @throws UnknownTokenException
403
     * @throws OutOfScopeException
404
     * @throws ScopeTokenValueSerializationException
405
     * @throws TokenFormatException
406
     * @return $this
407
     */
408 1
    public function resolve(TokenResolverInterface $tokenResolver, $ignoreUnknownTokens = True,
409
                            $ignoreUnknownFilters = True)
410
    {
411 1
        if ($this->isResolved && (!$this->hasUnresolvedFilters())) {
412 1
            return $this;
413
        }
414 1
        if (!$this->isResolved) {
415 1
            $this->resolveUnfilteredValue($tokenResolver, $ignoreUnknownTokens);
416 1
        }
417 1
        if ($this->hasUnfilteredTokenValue() && $this->hasUnresolvedFilters()) {
418 1
            $this->applyFilters($ignoreUnknownFilters);
419 1
        }
420 1
        return $this;
421
    }
422
}