|
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
|
|
|
/** |
|
15
|
|
|
* Describes a block directive instance or a context. |
|
16
|
|
|
* |
|
17
|
|
|
* a BlockDirective is an ordered list of other directives set in a context. |
|
18
|
|
|
* |
|
19
|
|
|
* @author James <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class BlockDirective extends Directive implements DirectiveInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var array orderd list of sub directives */ |
|
24
|
|
|
private $directives = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Adds a Directive at the end of the list. |
|
28
|
|
|
* |
|
29
|
|
|
* @param DirectiveInterface $directive a directive to add |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function add(DirectiveInterface $directive) |
|
32
|
|
|
{ |
|
33
|
6 |
|
$this->directives[] = $directive; |
|
34
|
|
|
|
|
35
|
6 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Confirms if the directive contains a specified directive. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name the directive for which to check existence |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function hasDirective($name) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$inSubDirective = false; |
|
48
|
|
|
|
|
49
|
1 |
|
foreach ($this->directives as $directive) { |
|
50
|
1 |
|
if ($directive->getName() == $name) { |
|
51
|
1 |
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$inSubDirective = $this->hasInnerDirective($name, $inSubDirective, $directive); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return $inSubDirective; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Looks into sub directives to confirm if the actual contains a specified directive. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name the directive for which to check existence |
|
64
|
|
|
* @param bool $inSubDirective the actual state |
|
65
|
|
|
* @param DirectiveInterface $directive the sub directive to look into |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
|
68
|
|
|
*/ |
|
69
|
1 |
|
private function hasInnerDirective($name, $inSubDirective, DirectiveInterface $directive) |
|
70
|
|
|
{ |
|
71
|
1 |
|
if (!$directive->isSimple()) { |
|
72
|
1 |
|
$inSubDirective = $inSubDirective || $directive->hasDirective($name); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
return $inSubDirective; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Confirms if the directive is simple. |
|
80
|
|
|
* |
|
81
|
|
|
* Block directive can have sub directives |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool true if the directive is simple, false otherwise |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function isSimple() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|