-
Notifications
You must be signed in to change notification settings - Fork 16
/
Block.php
409 lines (363 loc) · 10.3 KB
/
Block.php
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/**
* -- PHP Htaccess Parser --
* Block.php created at 02-12-2014
*
* Copyright 2014-2024 Estevão Soares dos Santos
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace Tivie\HtaccessParser\Token;
use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use Traversable;
use Tivie\HtaccessParser\Exception\DomainException;
use Tivie\HtaccessParser\Exception\InvalidArgumentException;
/**
* Class Block
* A Token corresponding to a block (module) segment of .htaccess
*
* @package Tivie\HtaccessParser\Token
* @copyright 2014-2024 Estêvão Soares dos Santos
*/
class Block extends BaseToken implements IteratorAggregate, ArrayAccess, Countable
{
/**
* @var string
*/
private string $blockName;
/**
* @var array
*/
private array $arguments = array();
/**
* @var TokenInterface[]
*/
private array $children = array();
/**
* @var int
*/
private int $indentation = 4;
/**
* Create a new Block token.
*
* This token corresponds to the following structure in .htaccess:
*
* <%blockName% %argument%>
* ...
* </%blockName%>
*
* @param string $blockName [optional] The name of the block
* @param string|null $argument [optional] The argument of the block
*/
public function __construct(string $blockName = 'blockName', string $argument = null)
{
$this->setName($blockName);
if (!is_null($argument)) {
$this->arguments[] = $argument;
}
}
/**
* Set the block's name
*
* @param string $blockName [required] The name of the Block
* @return $this
*/
public function setName(string $blockName): static
{
$this->blockName = $blockName;
return $this;
}
/**
* Get the Token's name
*
* @return string
*/
public function getName(): string
{
return $this->blockName;
}
/**
* Set the block's arguments
*
* @param array $arguments [required] An array of arguments
* @return $this
* @throws DomainException
*/
public function setArguments(array $arguments): static
{
foreach ($arguments as $arg) {
if (!is_scalar($arg)) {
$type = gettype($arg);
throw new DomainException("Arguments array should be an array of scalar, but found $type");
}
}
$this->arguments = $arguments;
return $this;
}
/**
* Get the block's arguments
*
* @return array
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* A helper method that returns a string corresponding to the Token's value
* (or its arguments concatenated)
*
* @return string
*/
public function getValue(): string
{
return (implode(' ', $this->getArguments()));
}
/**
* Add an argument to the Block arguments array
*
* @param mixed $arg [required] A scalar
* @return $this
* @throws InvalidArgumentException
*/
public function addArgument(mixed $arg): static
{
if (!is_scalar($arg)) {
throw new InvalidArgumentException('scalar', 0);
}
if (!in_array($arg, $this->arguments)) {
$this->arguments[] = $arg;
}
return $this;
}
/**
* Remove an argument from the Block arguments array
*
* @param string $arg
* @return $this
*/
public function removeArgument(string $arg): static
{
if (($key = array_search($arg, $this->arguments)) !== false) {
unset($this->arguments[$key]);
}
return $this;
}
/**
* Add a child to this block
*
* @param TokenInterface $child
* @return $this
*/
public function addChild(TokenInterface $child): static
{
$this->children[] = $child;
return $this;
}
/**
* Remove a child from this block
*
* @param TokenInterface $child [required] The child to remove
* @param bool $strict [optional] Default true. If the comparison should be strict. A non strict comparsion
* will remove a child if it has the same properties with the same values
* @return $this
*/
public function removeChild(TokenInterface $child, bool $strict = true): static
{
$index = array_search($child, $this->children, !!$strict);
if ($index !== false) {
unset($this->children[$index]);
}
return $this;
}
/**
* Check if Block has children
*
* @return bool
*/
public function hasChildren(): bool
{
return ($this->count() > 0);
}
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable|ArrayIterator An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
*/
public function getIterator(): Traversable|ArrayIterator
{
return new ArrayIterator($this->children);
}
/**
* (PHP 5 >= 5.0.0)<br/>
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return argument will be cast to boolean if non-boolean was returned.
*/
public function offsetExists(mixed $offset): bool
{
if (!is_scalar($offset)) {
throw new \InvalidArgumentException("Offset must be a scalar");
}
return isset($this->children[$offset]);
}
/**
* Offset to retrieve
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset The offset to retrieve.
* @return TokenInterface Can return all argument types.
*/
public function offsetGet(mixed $offset): TokenInterface
{
if (!$this->offsetExists($offset)) {
throw new \DomainException("$offset is not set");
}
return $this->children[$offset];
}
/**
* Offset to set
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset The offset to assign the argument to.
* @param mixed $value The value to set.
* @throws InvalidArgumentException
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (!is_null($offset) && !is_scalar($offset)) {
throw new InvalidArgumentException('scalar', 0);
}
if (!$value instanceof TokenInterface) {
throw new InvalidArgumentException('TokenInterface', 1);
}
if (!in_array($value, $this->children)) {
$this->children[$offset] = $value;
}
}
/**
* Offset to unset
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset The offset to unset.
* @return void
*/
public function offsetUnset(mixed $offset): void
{
if (!is_scalar($offset)) {
throw new \InvalidArgumentException("Offset must be a scalar");
}
unset($this->children[$offset]);
}
/**
* Count elements of an object
*
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer. The return argument is cast to an integer.
*/
public function count(): int
{
return count($this->children);
}
/**
* Return an array ready for serialization. Ignores comments and whitelines
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return array data which can be serialized by <b>json_encode</b>,
* which is an argument of any type other than a resource.
*/
function jsonSerialize(): array
{
$array = [
'arguments' => $this->arguments,
'children' => array()
];
foreach ($this->children as $child) {
if (!$child instanceof WhiteLine & !$child instanceof Comment) {
$array['children'][$child->getName()] = $child->jsonSerialize();
}
}
return $array;
}
/**
* Sets the indentation level
*
* @param integer $spaces [required] The number of spaces to indent lines when outputting to string
* @return $this
*/
public function setIndentation(int $spaces): static
{
$this->indentation = $spaces;
return $this;
}
/**
* Get a string representation of this Token
*
* @return string
*/
public function __toString(): string
{
$ind = str_repeat(' ', $this->indentation);
//Opening tag
$str = "<" . $this->blockName;
// Arguments list
foreach ($this->arguments as $arg) {
$str .= " $arg";
}
$str .= '>' . PHP_EOL;
//Children
foreach ($this->children as $child) {
$str .= "$ind$child" . PHP_EOL;
}
//Closing tag
$str .= "</{$this->blockName}>";
return $str;
}
/**
* Get the Token's type
*
* @return int
*/
public function getTokenType(): int
{
return TOKEN_BLOCK;
}
/**
* Get the array representation of the Token
*
* @return array
*/
public function toArray(): array
{
$array = [
'type' => $this->getTokenType(),
'name' => $this->getName(),
'arguments' => $this->getArguments(),
'children' => array()
];
foreach ($this->children as $child) {
$array['children'][] = $child->toArray();
}
return $array;
}
}