[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/config/Definition/Builder/ -> NodeDefinition.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  use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
  15  use Symfony\Component\Config\Definition\NodeInterface;
  16  
  17  /**
  18   * This class provides a fluent interface for defining a node.
  19   *
  20   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  21   */
  22  abstract class NodeDefinition implements NodeParentInterface
  23  {
  24      protected $name;
  25      protected $normalization;
  26      protected $validation;
  27      protected $defaultValue;
  28      protected $default = false;
  29      protected $required = false;
  30      protected $merge;
  31      protected $allowEmptyValue = true;
  32      protected $nullEquivalent;
  33      protected $trueEquivalent = true;
  34      protected $falseEquivalent = false;
  35      protected $parent;
  36      protected $attributes = array();
  37  
  38      /**
  39       * @param string|null              $name   The name of the node
  40       * @param NodeParentInterface|null $parent The parent
  41       */
  42      public function __construct($name, NodeParentInterface $parent = null)
  43      {
  44          $this->parent = $parent;
  45          $this->name = $name;
  46      }
  47  
  48      /**
  49       * Sets the parent node.
  50       *
  51       * @return $this
  52       */
  53      public function setParent(NodeParentInterface $parent)
  54      {
  55          $this->parent = $parent;
  56  
  57          return $this;
  58      }
  59  
  60      /**
  61       * Sets info message.
  62       *
  63       * @param string $info The info text
  64       *
  65       * @return $this
  66       */
  67      public function info($info)
  68      {
  69          return $this->attribute('info', $info);
  70      }
  71  
  72      /**
  73       * Sets example configuration.
  74       *
  75       * @param string|array $example
  76       *
  77       * @return $this
  78       */
  79      public function example($example)
  80      {
  81          return $this->attribute('example', $example);
  82      }
  83  
  84      /**
  85       * Sets an attribute on the node.
  86       *
  87       * @param string $key
  88       * @param mixed  $value
  89       *
  90       * @return $this
  91       */
  92      public function attribute($key, $value)
  93      {
  94          $this->attributes[$key] = $value;
  95  
  96          return $this;
  97      }
  98  
  99      /**
 100       * Returns the parent node.
 101       *
 102       * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
 103       */
 104      public function end()
 105      {
 106          return $this->parent;
 107      }
 108  
 109      /**
 110       * Creates the node.
 111       *
 112       * @param bool $forceRootNode Whether to force this node as the root node
 113       *
 114       * @return NodeInterface
 115       */
 116      public function getNode($forceRootNode = false)
 117      {
 118          if ($forceRootNode) {
 119              $this->parent = null;
 120          }
 121  
 122          if (null !== $this->normalization) {
 123              $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
 124          }
 125  
 126          if (null !== $this->validation) {
 127              $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
 128          }
 129  
 130          $node = $this->createNode();
 131          $node->setAttributes($this->attributes);
 132  
 133          return $node;
 134      }
 135  
 136      /**
 137       * Sets the default value.
 138       *
 139       * @param mixed $value The default value
 140       *
 141       * @return $this
 142       */
 143      public function defaultValue($value)
 144      {
 145          $this->default = true;
 146          $this->defaultValue = $value;
 147  
 148          return $this;
 149      }
 150  
 151      /**
 152       * Sets the node as required.
 153       *
 154       * @return $this
 155       */
 156      public function isRequired()
 157      {
 158          $this->required = true;
 159  
 160          return $this;
 161      }
 162  
 163      /**
 164       * Sets the equivalent value used when the node contains null.
 165       *
 166       * @param mixed $value
 167       *
 168       * @return $this
 169       */
 170      public function treatNullLike($value)
 171      {
 172          $this->nullEquivalent = $value;
 173  
 174          return $this;
 175      }
 176  
 177      /**
 178       * Sets the equivalent value used when the node contains true.
 179       *
 180       * @param mixed $value
 181       *
 182       * @return $this
 183       */
 184      public function treatTrueLike($value)
 185      {
 186          $this->trueEquivalent = $value;
 187  
 188          return $this;
 189      }
 190  
 191      /**
 192       * Sets the equivalent value used when the node contains false.
 193       *
 194       * @param mixed $value
 195       *
 196       * @return $this
 197       */
 198      public function treatFalseLike($value)
 199      {
 200          $this->falseEquivalent = $value;
 201  
 202          return $this;
 203      }
 204  
 205      /**
 206       * Sets null as the default value.
 207       *
 208       * @return $this
 209       */
 210      public function defaultNull()
 211      {
 212          return $this->defaultValue(null);
 213      }
 214  
 215      /**
 216       * Sets true as the default value.
 217       *
 218       * @return $this
 219       */
 220      public function defaultTrue()
 221      {
 222          return $this->defaultValue(true);
 223      }
 224  
 225      /**
 226       * Sets false as the default value.
 227       *
 228       * @return $this
 229       */
 230      public function defaultFalse()
 231      {
 232          return $this->defaultValue(false);
 233      }
 234  
 235      /**
 236       * Sets an expression to run before the normalization.
 237       *
 238       * @return ExprBuilder
 239       */
 240      public function beforeNormalization()
 241      {
 242          return $this->normalization()->before();
 243      }
 244  
 245      /**
 246       * Denies the node value being empty.
 247       *
 248       * @return $this
 249       */
 250      public function cannotBeEmpty()
 251      {
 252          $this->allowEmptyValue = false;
 253  
 254          return $this;
 255      }
 256  
 257      /**
 258       * Sets an expression to run for the validation.
 259       *
 260       * The expression receives the value of the node and must return it. It can
 261       * modify it.
 262       * An exception should be thrown when the node is not valid.
 263       *
 264       * @return ExprBuilder
 265       */
 266      public function validate()
 267      {
 268          return $this->validation()->rule();
 269      }
 270  
 271      /**
 272       * Sets whether the node can be overwritten.
 273       *
 274       * @param bool $deny Whether the overwriting is forbidden or not
 275       *
 276       * @return $this
 277       */
 278      public function cannotBeOverwritten($deny = true)
 279      {
 280          $this->merge()->denyOverwrite($deny);
 281  
 282          return $this;
 283      }
 284  
 285      /**
 286       * Gets the builder for validation rules.
 287       *
 288       * @return ValidationBuilder
 289       */
 290      protected function validation()
 291      {
 292          if (null === $this->validation) {
 293              $this->validation = new ValidationBuilder($this);
 294          }
 295  
 296          return $this->validation;
 297      }
 298  
 299      /**
 300       * Gets the builder for merging rules.
 301       *
 302       * @return MergeBuilder
 303       */
 304      protected function merge()
 305      {
 306          if (null === $this->merge) {
 307              $this->merge = new MergeBuilder($this);
 308          }
 309  
 310          return $this->merge;
 311      }
 312  
 313      /**
 314       * Gets the builder for normalization rules.
 315       *
 316       * @return NormalizationBuilder
 317       */
 318      protected function normalization()
 319      {
 320          if (null === $this->normalization) {
 321              $this->normalization = new NormalizationBuilder($this);
 322          }
 323  
 324          return $this->normalization;
 325      }
 326  
 327      /**
 328       * Instantiate and configure the node according to this definition.
 329       *
 330       * @return NodeInterface The node instance
 331       *
 332       * @throws InvalidDefinitionException When the definition is invalid
 333       */
 334      abstract protected function createNode();
 335  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1