|
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 WebHelper\Parser\Server\ServerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This is a simple directive implementation that other Directives can inherit from. |
|
18
|
|
|
* |
|
19
|
|
|
* a Directive may be a simple key/value information or an ordered list |
|
20
|
|
|
* of simple directives set in a context (or block) with a name and an optional value |
|
21
|
|
|
* that can be itself nested in another block directive. |
|
22
|
|
|
* |
|
23
|
|
|
* @author James <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class Directive implements DirectiveInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string the name of the key/context */ |
|
28
|
|
|
private $name; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string the value of the key/context */ |
|
31
|
|
|
private $value; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Base contructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $name the name of the key/context to instanciate |
|
37
|
|
|
* @param string $value the optional value of the key/context to instanciate |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function __construct($name, $value = '') |
|
40
|
|
|
{ |
|
41
|
11 |
|
$this->name = $name; |
|
42
|
11 |
|
$this->value = $value; |
|
43
|
11 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the key name. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string the key name |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function getName() |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the key value. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string the key value |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function getValue() |
|
61
|
|
|
{ |
|
62
|
6 |
|
return $this->value; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Adds a Directive at the end of a list. |
|
67
|
|
|
* |
|
68
|
|
|
* @param DirectiveInterface $directive a directive to add |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function add(DirectiveInterface $directive) |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Confirms if the directive contains a specified directive. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name the directive for which to check existence |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool true if the sub-directive exists, false otherwise |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function hasDirective($name); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Confirms if the directive is simple. |
|
86
|
|
|
* |
|
87
|
|
|
* Simple directive cannot have sub directive |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool true if the directive is simple, false otherwise |
|
90
|
|
|
*/ |
|
91
|
|
|
abstract public function isSimple(); |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Dumps the directive respecting a server syntax. |
|
95
|
|
|
* |
|
96
|
|
|
* @param ServerInterface $server a server instance |
|
97
|
|
|
* @param int $spaces the indentation spaces |
|
98
|
|
|
* |
|
99
|
|
|
* @return string the dumped directive |
|
100
|
|
|
*/ |
|
101
|
|
|
abstract public function dump(ServerInterface $server, $spaces = 0); |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Dumps a simple directive. |
|
105
|
|
|
* |
|
106
|
|
|
* @param ServerInterface $server a server instance |
|
107
|
|
|
* @param int $spaces the indentation spaces |
|
108
|
|
|
* |
|
109
|
|
|
* @return string the dumped simple directive |
|
110
|
|
|
*/ |
|
111
|
1 |
|
protected function dumpSimple(ServerInterface $server, $spaces = 0) |
|
112
|
|
|
{ |
|
113
|
1 |
|
$value = $this->getValue() ? ' '.$this->getValue() : ''; |
|
114
|
|
|
|
|
115
|
1 |
|
return str_repeat(' ', $spaces).sprintf( |
|
116
|
1 |
|
$server->getDumperSimpleDirective(), |
|
117
|
1 |
|
$this->getName(), |
|
118
|
|
|
$value |
|
119
|
1 |
|
).PHP_EOL; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|