1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Config package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Config\Definition\Builder; |
12
|
|
|
|
13
|
|
|
use Borobudur\Config\Definition\NodeInterface; |
14
|
|
|
use Borobudur\Config\Definition\PrototypeNodeInterface; |
15
|
|
|
use Borobudur\Config\Exception\InvalidArgumentException; |
16
|
|
|
use Borobudur\Config\Exception\RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Iqbal Maulana <[email protected]> |
20
|
|
|
* @created 8/10/15 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractDefinition implements NodeDefinitionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var mixed |
31
|
|
|
*/ |
32
|
|
|
protected $defaultValue; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $defaultValueSet = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $required = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var mixed |
46
|
|
|
*/ |
47
|
|
|
protected $nullEquivalent; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var DefinitionInterface|NodeDefinitionInterface|NodeInterface|null |
51
|
|
|
*/ |
52
|
|
|
protected $parent; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $allowOverwrite = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
* |
62
|
|
|
* @param string $name |
63
|
|
|
* @param NodeDefinitionInterface|null $parent |
64
|
|
|
*/ |
65
|
|
|
public function __construct($name, NodeDefinitionInterface $parent = null) |
66
|
|
|
{ |
67
|
|
|
$this->name = $name; |
68
|
|
|
$this->parent = $parent; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function setName($name) |
75
|
|
|
{ |
76
|
|
|
$this->name = $name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get node name. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getName() |
85
|
|
|
{ |
86
|
|
|
return $this->name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function setParent($parent = null) |
93
|
|
|
{ |
94
|
|
|
if (null !== $parent && $this->isValidParentInstance($parent)) { |
95
|
|
|
throw new InvalidArgumentException(sprintf( |
96
|
|
|
'Parent "%s" should be instance of DefinitionInterface, NodeDefinitionInterface or NodeInterface.', |
97
|
|
|
get_class($parent) |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->parent = $parent; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
* |
109
|
|
|
* @return NodeInterface|NodeDefinitionBuilder|DefinitionInterface|NodeDefinitionContainerInterface|null |
110
|
|
|
*/ |
111
|
|
|
public function end() |
112
|
|
|
{ |
113
|
|
|
return $this->parent; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function cannotBeOverwritten() |
120
|
|
|
{ |
121
|
|
|
$this->allowOverwrite = false; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function defaultValue($value) |
130
|
|
|
{ |
131
|
|
|
$this->defaultValueSet = true; |
132
|
|
|
$this->defaultValue = $value; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function defaultNull() |
141
|
|
|
{ |
142
|
|
|
$this->defaultValue = $this->nullEquivalent; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function defaultTrue() |
151
|
|
|
{ |
152
|
|
|
return $this->defaultValue(true); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function defaultFalse() |
159
|
|
|
{ |
160
|
|
|
return $this->defaultValue(false); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function isRequired() |
167
|
|
|
{ |
168
|
|
|
$this->required = true; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function getNode() |
177
|
|
|
{ |
178
|
|
|
return $this->createNode(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Assert that correct node parent. |
183
|
|
|
*/ |
184
|
|
|
protected function assertParentNode() |
185
|
|
|
{ |
186
|
|
|
if (null !== $this->parent && false === $this->parent instanceof NodeInterface) { |
187
|
|
|
throw new RuntimeException(sprintf( |
188
|
|
|
'"%s" should implement \Borobudur\Config\Definition\NodeInterface', |
189
|
|
|
get_class($this->parent) |
190
|
|
|
)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Create and configure node by definition. |
196
|
|
|
* |
197
|
|
|
* @return NodeInterface|PrototypeNodeInterface |
198
|
|
|
*/ |
199
|
|
|
abstract protected function createNode(); |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check if parent is valid instance. |
203
|
|
|
* |
204
|
|
|
* @param mixed $parent |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
private function isValidParentInstance($parent) |
209
|
|
|
{ |
210
|
|
|
return false === $parent instanceof DefinitionInterface |
211
|
|
|
&& false === $parent instanceof NodeDefinitionInterface |
212
|
|
|
&& false === $parent instanceof NodeInterface; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|