vendor/symfony/config/Definition/Builder/NodeBuilder.php line 42

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\Config\Definition\Builder;
  11. /**
  12.  * This class provides a fluent interface for building a node.
  13.  *
  14.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  15.  */
  16. class NodeBuilder implements NodeParentInterface
  17. {
  18.     protected $parent;
  19.     protected $nodeMapping;
  20.     public function __construct()
  21.     {
  22.         $this->nodeMapping = [
  23.             'variable' => VariableNodeDefinition::class,
  24.             'scalar' => ScalarNodeDefinition::class,
  25.             'boolean' => BooleanNodeDefinition::class,
  26.             'integer' => IntegerNodeDefinition::class,
  27.             'float' => FloatNodeDefinition::class,
  28.             'array' => ArrayNodeDefinition::class,
  29.             'enum' => EnumNodeDefinition::class,
  30.         ];
  31.     }
  32.     /**
  33.      * Set the parent node.
  34.      *
  35.      * @return $this
  36.      */
  37.     public function setParent(ParentNodeDefinitionInterface $parent null): static
  38.     {
  39.         if (\func_num_args()) {
  40.             trigger_deprecation('symfony/form''6.2''Calling "%s()" without any arguments is deprecated, pass null explicitly instead.'__METHOD__);
  41.         }
  42.         $this->parent $parent;
  43.         return $this;
  44.     }
  45.     /**
  46.      * Creates a child array node.
  47.      */
  48.     public function arrayNode(string $name): ArrayNodeDefinition
  49.     {
  50.         return $this->node($name'array');
  51.     }
  52.     /**
  53.      * Creates a child scalar node.
  54.      */
  55.     public function scalarNode(string $name): ScalarNodeDefinition
  56.     {
  57.         return $this->node($name'scalar');
  58.     }
  59.     /**
  60.      * Creates a child Boolean node.
  61.      */
  62.     public function booleanNode(string $name): BooleanNodeDefinition
  63.     {
  64.         return $this->node($name'boolean');
  65.     }
  66.     /**
  67.      * Creates a child integer node.
  68.      */
  69.     public function integerNode(string $name): IntegerNodeDefinition
  70.     {
  71.         return $this->node($name'integer');
  72.     }
  73.     /**
  74.      * Creates a child float node.
  75.      */
  76.     public function floatNode(string $name): FloatNodeDefinition
  77.     {
  78.         return $this->node($name'float');
  79.     }
  80.     /**
  81.      * Creates a child EnumNode.
  82.      */
  83.     public function enumNode(string $name): EnumNodeDefinition
  84.     {
  85.         return $this->node($name'enum');
  86.     }
  87.     /**
  88.      * Creates a child variable node.
  89.      */
  90.     public function variableNode(string $name): VariableNodeDefinition
  91.     {
  92.         return $this->node($name'variable');
  93.     }
  94.     /**
  95.      * Returns the parent node.
  96.      *
  97.      * @return NodeDefinition&ParentNodeDefinitionInterface
  98.      */
  99.     public function end()
  100.     {
  101.         return $this->parent;
  102.     }
  103.     /**
  104.      * Creates a child node.
  105.      *
  106.      * @throws \RuntimeException When the node type is not registered
  107.      * @throws \RuntimeException When the node class is not found
  108.      */
  109.     public function node(?string $namestring $type): NodeDefinition
  110.     {
  111.         $class $this->getNodeClass($type);
  112.         $node = new $class($name);
  113.         $this->append($node);
  114.         return $node;
  115.     }
  116.     /**
  117.      * Appends a node definition.
  118.      *
  119.      * Usage:
  120.      *
  121.      *     $node = new ArrayNodeDefinition('name')
  122.      *         ->children()
  123.      *             ->scalarNode('foo')->end()
  124.      *             ->scalarNode('baz')->end()
  125.      *             ->append($this->getBarNodeDefinition())
  126.      *         ->end()
  127.      *     ;
  128.      *
  129.      * @return $this
  130.      */
  131.     public function append(NodeDefinition $node): static
  132.     {
  133.         if ($node instanceof BuilderAwareInterface) {
  134.             $builder = clone $this;
  135.             $builder->setParent(null);
  136.             $node->setBuilder($builder);
  137.         }
  138.         if (null !== $this->parent) {
  139.             $this->parent->append($node);
  140.             // Make this builder the node parent to allow for a fluid interface
  141.             $node->setParent($this);
  142.         }
  143.         return $this;
  144.     }
  145.     /**
  146.      * Adds or overrides a node Type.
  147.      *
  148.      * @param string $type  The name of the type
  149.      * @param string $class The fully qualified name the node definition class
  150.      *
  151.      * @return $this
  152.      */
  153.     public function setNodeClass(string $typestring $class): static
  154.     {
  155.         $this->nodeMapping[strtolower($type)] = $class;
  156.         return $this;
  157.     }
  158.     /**
  159.      * Returns the class name of the node definition.
  160.      *
  161.      * @throws \RuntimeException When the node type is not registered
  162.      * @throws \RuntimeException When the node class is not found
  163.      */
  164.     protected function getNodeClass(string $type): string
  165.     {
  166.         $type strtolower($type);
  167.         if (!isset($this->nodeMapping[$type])) {
  168.             throw new \RuntimeException(sprintf('The node type "%s" is not registered.'$type));
  169.         }
  170.         $class $this->nodeMapping[$type];
  171.         if (!class_exists($class)) {
  172.             throw new \RuntimeException(sprintf('The node class "%s" does not exist.'$class));
  173.         }
  174.         return $class;
  175.     }
  176. }