1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of WebHelper Parser. |
5
|
|
|
* |
6
|
|
|
* (c) James <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WebHelper\Parser\Directive; |
13
|
|
|
|
14
|
|
|
use IteratorAggregate; |
15
|
|
|
use ArrayIterator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Describes a block directive instance or a context. |
19
|
|
|
* |
20
|
|
|
* a BlockDirective is an ordered list of other directives set in a context. |
21
|
|
|
* |
22
|
|
|
* @author James <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class BlockDirective extends Directive implements DirectiveInterface, IteratorAggregate |
25
|
|
|
{ |
26
|
|
|
/** @var array orderd list of sub directives */ |
27
|
|
|
private $directives = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Adds a Directive at the end of the list. |
31
|
|
|
* |
32
|
|
|
* @param DirectiveInterface $directive a directive to add |
33
|
|
|
*/ |
34
|
9 |
|
public function add(DirectiveInterface $directive) |
35
|
|
|
{ |
36
|
9 |
|
$this->directives[] = $directive; |
37
|
|
|
|
38
|
9 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Confirms if the directive contains a specified directive. |
43
|
|
|
* |
44
|
|
|
* @param string $name the directive for which to check existence |
45
|
|
|
* |
46
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
47
|
|
|
*/ |
48
|
1 |
|
public function hasDirective($name) |
49
|
|
|
{ |
50
|
1 |
|
$inSubDirective = false; |
51
|
|
|
|
52
|
1 |
|
foreach ($this->directives as $directive) { |
53
|
1 |
|
if ($directive->getName() == $name) { |
54
|
1 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$inSubDirective = $this->hasInnerDirective($name, $inSubDirective, $directive); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
return $inSubDirective; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Looks into sub directives to confirm if the actual contains a specified directive. |
65
|
|
|
* |
66
|
|
|
* @param string $name the directive for which to check existence |
67
|
|
|
* @param bool $inSubDirective the actual state |
68
|
|
|
* @param DirectiveInterface $directive the sub directive to look into |
69
|
|
|
* |
70
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
71
|
|
|
*/ |
72
|
1 |
|
private function hasInnerDirective($name, $inSubDirective, DirectiveInterface $directive) |
73
|
|
|
{ |
74
|
1 |
|
if (!$directive->isSimple()) { |
75
|
1 |
|
$inSubDirective = $inSubDirective || $directive->hasDirective($name); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
return $inSubDirective; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Confirms if the directive is simple. |
83
|
|
|
* |
84
|
|
|
* Block directive can have sub directives |
85
|
|
|
* |
86
|
|
|
* @return bool true if the directive is simple, false otherwise |
87
|
|
|
*/ |
88
|
2 |
|
public function isSimple() |
89
|
|
|
{ |
90
|
2 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the sub directives as a Traversable array. |
95
|
|
|
* |
96
|
|
|
* @return ArrayIterator the sub directives |
97
|
|
|
*/ |
98
|
1 |
|
public function getIterator() |
99
|
|
|
{ |
100
|
1 |
|
return new ArrayIterator($this->directives); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|