Version 1
[yaffs-website] / vendor / symfony / config / Definition / Builder / NodeDefinition.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Config\Definition\Builder;
13
14 use Symfony\Component\Config\Definition\NodeInterface;
15 use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
16
17 /**
18  * This class provides a fluent interface for defining a node.
19  *
20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21  */
22 abstract class NodeDefinition implements NodeParentInterface
23 {
24     protected $name;
25     protected $normalization;
26     protected $validation;
27     protected $defaultValue;
28     protected $default = false;
29     protected $required = false;
30     protected $merge;
31     protected $allowEmptyValue = true;
32     protected $nullEquivalent;
33     protected $trueEquivalent = true;
34     protected $falseEquivalent = false;
35
36     /**
37      * @var NodeParentInterface|null
38      */
39     protected $parent;
40     protected $attributes = array();
41
42     /**
43      * Constructor.
44      *
45      * @param string                   $name   The name of the node
46      * @param NodeParentInterface|null $parent The parent
47      */
48     public function __construct($name, NodeParentInterface $parent = null)
49     {
50         $this->parent = $parent;
51         $this->name = $name;
52     }
53
54     /**
55      * Sets the parent node.
56      *
57      * @param NodeParentInterface $parent The parent
58      *
59      * @return $this
60      */
61     public function setParent(NodeParentInterface $parent)
62     {
63         $this->parent = $parent;
64
65         return $this;
66     }
67
68     /**
69      * Sets info message.
70      *
71      * @param string $info The info text
72      *
73      * @return $this
74      */
75     public function info($info)
76     {
77         return $this->attribute('info', $info);
78     }
79
80     /**
81      * Sets example configuration.
82      *
83      * @param string|array $example
84      *
85      * @return $this
86      */
87     public function example($example)
88     {
89         return $this->attribute('example', $example);
90     }
91
92     /**
93      * Sets an attribute on the node.
94      *
95      * @param string $key
96      * @param mixed  $value
97      *
98      * @return $this
99      */
100     public function attribute($key, $value)
101     {
102         $this->attributes[$key] = $value;
103
104         return $this;
105     }
106
107     /**
108      * Returns the parent node.
109      *
110      * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
111      */
112     public function end()
113     {
114         return $this->parent;
115     }
116
117     /**
118      * Creates the node.
119      *
120      * @param bool $forceRootNode Whether to force this node as the root node
121      *
122      * @return NodeInterface
123      */
124     public function getNode($forceRootNode = false)
125     {
126         if ($forceRootNode) {
127             $this->parent = null;
128         }
129
130         if (null !== $this->normalization) {
131             $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
132         }
133
134         if (null !== $this->validation) {
135             $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
136         }
137
138         $node = $this->createNode();
139         $node->setAttributes($this->attributes);
140
141         return $node;
142     }
143
144     /**
145      * Sets the default value.
146      *
147      * @param mixed $value The default value
148      *
149      * @return $this
150      */
151     public function defaultValue($value)
152     {
153         $this->default = true;
154         $this->defaultValue = $value;
155
156         return $this;
157     }
158
159     /**
160      * Sets the node as required.
161      *
162      * @return $this
163      */
164     public function isRequired()
165     {
166         $this->required = true;
167
168         return $this;
169     }
170
171     /**
172      * Sets the equivalent value used when the node contains null.
173      *
174      * @param mixed $value
175      *
176      * @return $this
177      */
178     public function treatNullLike($value)
179     {
180         $this->nullEquivalent = $value;
181
182         return $this;
183     }
184
185     /**
186      * Sets the equivalent value used when the node contains true.
187      *
188      * @param mixed $value
189      *
190      * @return $this
191      */
192     public function treatTrueLike($value)
193     {
194         $this->trueEquivalent = $value;
195
196         return $this;
197     }
198
199     /**
200      * Sets the equivalent value used when the node contains false.
201      *
202      * @param mixed $value
203      *
204      * @return $this
205      */
206     public function treatFalseLike($value)
207     {
208         $this->falseEquivalent = $value;
209
210         return $this;
211     }
212
213     /**
214      * Sets null as the default value.
215      *
216      * @return $this
217      */
218     public function defaultNull()
219     {
220         return $this->defaultValue(null);
221     }
222
223     /**
224      * Sets true as the default value.
225      *
226      * @return $this
227      */
228     public function defaultTrue()
229     {
230         return $this->defaultValue(true);
231     }
232
233     /**
234      * Sets false as the default value.
235      *
236      * @return $this
237      */
238     public function defaultFalse()
239     {
240         return $this->defaultValue(false);
241     }
242
243     /**
244      * Sets an expression to run before the normalization.
245      *
246      * @return ExprBuilder
247      */
248     public function beforeNormalization()
249     {
250         return $this->normalization()->before();
251     }
252
253     /**
254      * Denies the node value being empty.
255      *
256      * @return $this
257      */
258     public function cannotBeEmpty()
259     {
260         $this->allowEmptyValue = false;
261
262         return $this;
263     }
264
265     /**
266      * Sets an expression to run for the validation.
267      *
268      * The expression receives the value of the node and must return it. It can
269      * modify it.
270      * An exception should be thrown when the node is not valid.
271      *
272      * @return ExprBuilder
273      */
274     public function validate()
275     {
276         return $this->validation()->rule();
277     }
278
279     /**
280      * Sets whether the node can be overwritten.
281      *
282      * @param bool $deny Whether the overwriting is forbidden or not
283      *
284      * @return $this
285      */
286     public function cannotBeOverwritten($deny = true)
287     {
288         $this->merge()->denyOverwrite($deny);
289
290         return $this;
291     }
292
293     /**
294      * Gets the builder for validation rules.
295      *
296      * @return ValidationBuilder
297      */
298     protected function validation()
299     {
300         if (null === $this->validation) {
301             $this->validation = new ValidationBuilder($this);
302         }
303
304         return $this->validation;
305     }
306
307     /**
308      * Gets the builder for merging rules.
309      *
310      * @return MergeBuilder
311      */
312     protected function merge()
313     {
314         if (null === $this->merge) {
315             $this->merge = new MergeBuilder($this);
316         }
317
318         return $this->merge;
319     }
320
321     /**
322      * Gets the builder for normalization rules.
323      *
324      * @return NormalizationBuilder
325      */
326     protected function normalization()
327     {
328         if (null === $this->normalization) {
329             $this->normalization = new NormalizationBuilder($this);
330         }
331
332         return $this->normalization;
333     }
334
335     /**
336      * Instantiate and configure the node according to this definition.
337      *
338      * @return NodeInterface $node The node instance
339      *
340      * @throws InvalidDefinitionException When the definition is invalid
341      */
342     abstract protected function createNode();
343 }