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;
30 protected $deprecationMessage = null;
32 protected $allowEmptyValue = true;
33 protected $nullEquivalent;
34 protected $trueEquivalent = true;
35 protected $falseEquivalent = false;
37 protected $attributes = array();
40 * @param string|null $name The name of the node
41 * @param NodeParentInterface|null $parent The parent
43 public function __construct($name, NodeParentInterface $parent = null)
45 $this->parent = $parent;
50 * Sets the parent node.
54 public function setParent(NodeParentInterface $parent)
56 $this->parent = $parent;
64 * @param string $info The info text
68 public function info($info)
70 return $this->attribute('info', $info);
74 * Sets example configuration.
76 * @param string|array $example
80 public function example($example)
82 return $this->attribute('example', $example);
86 * Sets an attribute on the node.
93 public function attribute($key, $value)
95 $this->attributes[$key] = $value;
101 * Returns the parent node.
103 * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
105 public function end()
107 return $this->parent;
113 * @param bool $forceRootNode Whether to force this node as the root node
115 * @return NodeInterface
117 public function getNode($forceRootNode = false)
119 if ($forceRootNode) {
120 $this->parent = null;
123 if (null !== $this->normalization) {
124 $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
127 if (null !== $this->validation) {
128 $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
131 $node = $this->createNode();
132 $node->setAttributes($this->attributes);
138 * Sets the default value.
140 * @param mixed $value The default value
144 public function defaultValue($value)
146 $this->default = true;
147 $this->defaultValue = $value;
153 * Sets the node as required.
157 public function isRequired()
159 $this->required = true;
165 * Sets the node as deprecated.
167 * You can use %node% and %path% placeholders in your message to display,
168 * respectively, the node name and its complete path.
170 * @param string $message Deprecation message
174 public function setDeprecated($message = 'The child node "%node%" at path "%path%" is deprecated.')
176 $this->deprecationMessage = $message;
182 * Sets the equivalent value used when the node contains null.
184 * @param mixed $value
188 public function treatNullLike($value)
190 $this->nullEquivalent = $value;
196 * Sets the equivalent value used when the node contains true.
198 * @param mixed $value
202 public function treatTrueLike($value)
204 $this->trueEquivalent = $value;
210 * Sets the equivalent value used when the node contains false.
212 * @param mixed $value
216 public function treatFalseLike($value)
218 $this->falseEquivalent = $value;
224 * Sets null as the default value.
228 public function defaultNull()
230 return $this->defaultValue(null);
234 * Sets true as the default value.
238 public function defaultTrue()
240 return $this->defaultValue(true);
244 * Sets false as the default value.
248 public function defaultFalse()
250 return $this->defaultValue(false);
254 * Sets an expression to run before the normalization.
256 * @return ExprBuilder
258 public function beforeNormalization()
260 return $this->normalization()->before();
264 * Denies the node value being empty.
268 public function cannotBeEmpty()
270 $this->allowEmptyValue = false;
276 * Sets an expression to run for the validation.
278 * The expression receives the value of the node and must return it. It can
280 * An exception should be thrown when the node is not valid.
282 * @return ExprBuilder
284 public function validate()
286 return $this->validation()->rule();
290 * Sets whether the node can be overwritten.
292 * @param bool $deny Whether the overwriting is forbidden or not
296 public function cannotBeOverwritten($deny = true)
298 $this->merge()->denyOverwrite($deny);
304 * Gets the builder for validation rules.
306 * @return ValidationBuilder
308 protected function validation()
310 if (null === $this->validation) {
311 $this->validation = new ValidationBuilder($this);
314 return $this->validation;
318 * Gets the builder for merging rules.
320 * @return MergeBuilder
322 protected function merge()
324 if (null === $this->merge) {
325 $this->merge = new MergeBuilder($this);
332 * Gets the builder for normalization rules.
334 * @return NormalizationBuilder
336 protected function normalization()
338 if (null === $this->normalization) {
339 $this->normalization = new NormalizationBuilder($this);
342 return $this->normalization;
346 * Instantiate and configure the node according to this definition.
348 * @return NodeInterface $node The node instance
350 * @throws InvalidDefinitionException When the definition is invalid
352 abstract protected function createNode();