composer package updates
[openemr.git] / vendor / symfony / config / Definition / Builder / NodeBuilder.php
blob1fac66fd3702f2d14730cf01b2c7553e7a725817
1 <?php
3 /*
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 /**
15 * This class provides a fluent interface for building a node.
17 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
19 class NodeBuilder implements NodeParentInterface
21 protected $parent;
22 protected $nodeMapping;
24 public function __construct()
26 $this->nodeMapping = array(
27 'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
28 'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
29 'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
30 'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
31 'float' => __NAMESPACE__.'\\FloatNodeDefinition',
32 'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
33 'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
37 /**
38 * Set the parent node.
40 * @return $this
42 public function setParent(ParentNodeDefinitionInterface $parent = null)
44 $this->parent = $parent;
46 return $this;
49 /**
50 * Creates a child array node.
52 * @param string $name The name of the node
54 * @return ArrayNodeDefinition The child node
56 public function arrayNode($name)
58 return $this->node($name, 'array');
61 /**
62 * Creates a child scalar node.
64 * @param string $name The name of the node
66 * @return ScalarNodeDefinition The child node
68 public function scalarNode($name)
70 return $this->node($name, 'scalar');
73 /**
74 * Creates a child Boolean node.
76 * @param string $name The name of the node
78 * @return BooleanNodeDefinition The child node
80 public function booleanNode($name)
82 return $this->node($name, 'boolean');
85 /**
86 * Creates a child integer node.
88 * @param string $name The name of the node
90 * @return IntegerNodeDefinition The child node
92 public function integerNode($name)
94 return $this->node($name, 'integer');
97 /**
98 * Creates a child float node.
100 * @param string $name The name of the node
102 * @return FloatNodeDefinition The child node
104 public function floatNode($name)
106 return $this->node($name, 'float');
110 * Creates a child EnumNode.
112 * @param string $name
114 * @return EnumNodeDefinition
116 public function enumNode($name)
118 return $this->node($name, 'enum');
122 * Creates a child variable node.
124 * @param string $name The name of the node
126 * @return VariableNodeDefinition The builder of the child node
128 public function variableNode($name)
130 return $this->node($name, 'variable');
134 * Returns the parent node.
136 * @return ParentNodeDefinitionInterface|NodeDefinition The parent node
138 public function end()
140 return $this->parent;
144 * Creates a child node.
146 * @param string|null $name The name of the node
147 * @param string $type The type of the node
149 * @return NodeDefinition The child node
151 * @throws \RuntimeException When the node type is not registered
152 * @throws \RuntimeException When the node class is not found
154 public function node($name, $type)
156 $class = $this->getNodeClass($type);
158 $node = new $class($name);
160 $this->append($node);
162 return $node;
166 * Appends a node definition.
168 * Usage:
170 * $node = new ArrayNodeDefinition('name')
171 * ->children()
172 * ->scalarNode('foo')->end()
173 * ->scalarNode('baz')->end()
174 * ->append($this->getBarNodeDefinition())
175 * ->end()
178 * @return $this
180 public function append(NodeDefinition $node)
182 if ($node instanceof ParentNodeDefinitionInterface) {
183 $builder = clone $this;
184 $builder->setParent(null);
185 $node->setBuilder($builder);
188 if (null !== $this->parent) {
189 $this->parent->append($node);
190 // Make this builder the node parent to allow for a fluid interface
191 $node->setParent($this);
194 return $this;
198 * Adds or overrides a node Type.
200 * @param string $type The name of the type
201 * @param string $class The fully qualified name the node definition class
203 * @return $this
205 public function setNodeClass($type, $class)
207 $this->nodeMapping[strtolower($type)] = $class;
209 return $this;
213 * Returns the class name of the node definition.
215 * @param string $type The node type
217 * @return string The node definition class name
219 * @throws \RuntimeException When the node type is not registered
220 * @throws \RuntimeException When the node class is not found
222 protected function getNodeClass($type)
224 $type = strtolower($type);
226 if (!isset($this->nodeMapping[$type])) {
227 throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
230 $class = $this->nodeMapping[$type];
232 if (!class_exists($class)) {
233 throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));
236 return $class;