[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/config/Definition/Builder/ -> NodeBuilder.php (source)

   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  /**
  15   * This class provides a fluent interface for building a node.
  16   *
  17   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18   */
  19  class NodeBuilder implements NodeParentInterface
  20  {
  21      protected $parent;
  22      protected $nodeMapping;
  23  
  24      public function __construct()
  25      {
  26          $this->nodeMapping = [
  27              'variable' => VariableNodeDefinition::class,
  28              'scalar' => ScalarNodeDefinition::class,
  29              'boolean' => BooleanNodeDefinition::class,
  30              'integer' => IntegerNodeDefinition::class,
  31              'float' => FloatNodeDefinition::class,
  32              'array' => ArrayNodeDefinition::class,
  33              'enum' => EnumNodeDefinition::class,
  34          ];
  35      }
  36  
  37      /**
  38       * Set the parent node.
  39       *
  40       * @return $this
  41       */
  42      public function setParent(ParentNodeDefinitionInterface $parent = null)
  43      {
  44          $this->parent = $parent;
  45  
  46          return $this;
  47      }
  48  
  49      /**
  50       * Creates a child array node.
  51       *
  52       * @param string $name The name of the node
  53       *
  54       * @return ArrayNodeDefinition The child node
  55       */
  56      public function arrayNode($name)
  57      {
  58          return $this->node($name, 'array');
  59      }
  60  
  61      /**
  62       * Creates a child scalar node.
  63       *
  64       * @param string $name The name of the node
  65       *
  66       * @return ScalarNodeDefinition The child node
  67       */
  68      public function scalarNode($name)
  69      {
  70          return $this->node($name, 'scalar');
  71      }
  72  
  73      /**
  74       * Creates a child Boolean node.
  75       *
  76       * @param string $name The name of the node
  77       *
  78       * @return BooleanNodeDefinition The child node
  79       */
  80      public function booleanNode($name)
  81      {
  82          return $this->node($name, 'boolean');
  83      }
  84  
  85      /**
  86       * Creates a child integer node.
  87       *
  88       * @param string $name The name of the node
  89       *
  90       * @return IntegerNodeDefinition The child node
  91       */
  92      public function integerNode($name)
  93      {
  94          return $this->node($name, 'integer');
  95      }
  96  
  97      /**
  98       * Creates a child float node.
  99       *
 100       * @param string $name The name of the node
 101       *
 102       * @return FloatNodeDefinition The child node
 103       */
 104      public function floatNode($name)
 105      {
 106          return $this->node($name, 'float');
 107      }
 108  
 109      /**
 110       * Creates a child EnumNode.
 111       *
 112       * @param string $name
 113       *
 114       * @return EnumNodeDefinition
 115       */
 116      public function enumNode($name)
 117      {
 118          return $this->node($name, 'enum');
 119      }
 120  
 121      /**
 122       * Creates a child variable node.
 123       *
 124       * @param string $name The name of the node
 125       *
 126       * @return VariableNodeDefinition The builder of the child node
 127       */
 128      public function variableNode($name)
 129      {
 130          return $this->node($name, 'variable');
 131      }
 132  
 133      /**
 134       * Returns the parent node.
 135       *
 136       * @return NodeDefinition&ParentNodeDefinitionInterface The parent node
 137       */
 138      public function end()
 139      {
 140          return $this->parent;
 141      }
 142  
 143      /**
 144       * Creates a child node.
 145       *
 146       * @param string|null $name The name of the node
 147       * @param string      $type The type of the node
 148       *
 149       * @return NodeDefinition The child node
 150       *
 151       * @throws \RuntimeException When the node type is not registered
 152       * @throws \RuntimeException When the node class is not found
 153       */
 154      public function node($name, $type)
 155      {
 156          $class = $this->getNodeClass($type);
 157  
 158          $node = new $class($name);
 159  
 160          $this->append($node);
 161  
 162          return $node;
 163      }
 164  
 165      /**
 166       * Appends a node definition.
 167       *
 168       * Usage:
 169       *
 170       *     $node = new ArrayNodeDefinition('name')
 171       *         ->children()
 172       *             ->scalarNode('foo')->end()
 173       *             ->scalarNode('baz')->end()
 174       *             ->append($this->getBarNodeDefinition())
 175       *         ->end()
 176       *     ;
 177       *
 178       * @return $this
 179       */
 180      public function append(NodeDefinition $node)
 181      {
 182          if ($node instanceof ParentNodeDefinitionInterface) {
 183              $builder = clone $this;
 184              $builder->setParent(null);
 185              $node->setBuilder($builder);
 186          }
 187  
 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);
 192          }
 193  
 194          return $this;
 195      }
 196  
 197      /**
 198       * Adds or overrides a node Type.
 199       *
 200       * @param string $type  The name of the type
 201       * @param string $class The fully qualified name the node definition class
 202       *
 203       * @return $this
 204       */
 205      public function setNodeClass($type, $class)
 206      {
 207          $this->nodeMapping[strtolower($type)] = $class;
 208  
 209          return $this;
 210      }
 211  
 212      /**
 213       * Returns the class name of the node definition.
 214       *
 215       * @param string $type The node type
 216       *
 217       * @return string The node definition class name
 218       *
 219       * @throws \RuntimeException When the node type is not registered
 220       * @throws \RuntimeException When the node class is not found
 221       */
 222      protected function getNodeClass($type)
 223      {
 224          $type = strtolower($type);
 225  
 226          if (!isset($this->nodeMapping[$type])) {
 227              throw new \RuntimeException(sprintf('The node type "%s" is not registered.', $type));
 228          }
 229  
 230          $class = $this->nodeMapping[$type];
 231  
 232          if (!class_exists($class)) {
 233              throw new \RuntimeException(sprintf('The node class "%s" does not exist.', $class));
 234          }
 235  
 236          return $class;
 237      }
 238  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1