4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Definition\Builder;
14 use Symfony\Component\Config\Definition\NodeInterface;
15 use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
18 * This class provides a fluent interface for defining a node.
20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
22 abstract class NodeDefinition implements NodeParentInterface
25 protected $normalization;
26 protected $validation;
27 protected $defaultValue;
28 protected $default = false;
29 protected $required = false;
31 protected $allowEmptyValue = true;
32 protected $nullEquivalent;
33 protected $trueEquivalent = true;
34 protected $falseEquivalent = false;
37 * @var NodeParentInterface|null
40 protected $attributes = array();
45 * @param string $name The name of the node
46 * @param NodeParentInterface|null $parent The parent
48 public function __construct($name, NodeParentInterface $parent = null)
50 $this->parent = $parent;
55 * Sets the parent node.
57 * @param NodeParentInterface $parent The parent
61 public function setParent(NodeParentInterface $parent)
63 $this->parent = $parent;
71 * @param string $info The info text
75 public function info($info)
77 return $this->attribute('info', $info);
81 * Sets example configuration.
83 * @param string|array $example
87 public function example($example)
89 return $this->attribute('example', $example);
93 * Sets an attribute on the node.
100 public function attribute($key, $value)
102 $this->attributes[$key] = $value;
108 * Returns the parent node.
110 * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
112 public function end()
114 return $this->parent;
120 * @param bool $forceRootNode Whether to force this node as the root node
122 * @return NodeInterface
124 public function getNode($forceRootNode = false)
126 if ($forceRootNode) {
127 $this->parent = null;
130 if (null !== $this->normalization) {
131 $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
134 if (null !== $this->validation) {
135 $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
138 $node = $this->createNode();
139 $node->setAttributes($this->attributes);
145 * Sets the default value.
147 * @param mixed $value The default value
151 public function defaultValue($value)
153 $this->default = true;
154 $this->defaultValue = $value;
160 * Sets the node as required.
164 public function isRequired()
166 $this->required = true;
172 * Sets the equivalent value used when the node contains null.
174 * @param mixed $value
178 public function treatNullLike($value)
180 $this->nullEquivalent = $value;
186 * Sets the equivalent value used when the node contains true.
188 * @param mixed $value
192 public function treatTrueLike($value)
194 $this->trueEquivalent = $value;
200 * Sets the equivalent value used when the node contains false.
202 * @param mixed $value
206 public function treatFalseLike($value)
208 $this->falseEquivalent = $value;
214 * Sets null as the default value.
218 public function defaultNull()
220 return $this->defaultValue(null);
224 * Sets true as the default value.
228 public function defaultTrue()
230 return $this->defaultValue(true);
234 * Sets false as the default value.
238 public function defaultFalse()
240 return $this->defaultValue(false);
244 * Sets an expression to run before the normalization.
246 * @return ExprBuilder
248 public function beforeNormalization()
250 return $this->normalization()->before();
254 * Denies the node value being empty.
258 public function cannotBeEmpty()
260 $this->allowEmptyValue = false;
266 * Sets an expression to run for the validation.
268 * The expression receives the value of the node and must return it. It can
270 * An exception should be thrown when the node is not valid.
272 * @return ExprBuilder
274 public function validate()
276 return $this->validation()->rule();
280 * Sets whether the node can be overwritten.
282 * @param bool $deny Whether the overwriting is forbidden or not
286 public function cannotBeOverwritten($deny = true)
288 $this->merge()->denyOverwrite($deny);
294 * Gets the builder for validation rules.
296 * @return ValidationBuilder
298 protected function validation()
300 if (null === $this->validation) {
301 $this->validation = new ValidationBuilder($this);
304 return $this->validation;
308 * Gets the builder for merging rules.
310 * @return MergeBuilder
312 protected function merge()
314 if (null === $this->merge) {
315 $this->merge = new MergeBuilder($this);
322 * Gets the builder for normalization rules.
324 * @return NormalizationBuilder
326 protected function normalization()
328 if (null === $this->normalization) {
329 $this->normalization = new NormalizationBuilder($this);
332 return $this->normalization;
336 * Instantiate and configure the node according to this definition.
338 * @return NodeInterface $node The node instance
340 * @throws InvalidDefinitionException When the definition is invalid
342 abstract protected function createNode();