[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1