vendor/symfony/config/Definition/Builder/NodeDefinition.php line 243

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