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

  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.             $allowedTypes = [];
  96.             foreach ($this->normalization->before as $expr) {
  97.                 $allowedTypes[] = $expr->allowedTypes;
  98.             }
  99.             $allowedTypes array_unique($allowedTypes);
  100.             $this->normalization->before ExprBuilder::buildExpressions($this->normalization->before);
  101.             $this->normalization->declaredTypes $allowedTypes;
  102.         }
  103.         if (null !== $this->validation) {
  104.             $this->validation->rules ExprBuilder::buildExpressions($this->validation->rules);
  105.         }
  106.         $node $this->createNode();
  107.         if ($node instanceof BaseNode) {
  108.             $node->setAttributes($this->attributes);
  109.         }
  110.         return $node;
  111.     }
  112.     /**
  113.      * Sets the default value.
  114.      *
  115.      * @return $this
  116.      */
  117.     public function defaultValue(mixed $value): static
  118.     {
  119.         $this->default true;
  120.         $this->defaultValue $value;
  121.         return $this;
  122.     }
  123.     /**
  124.      * Sets the node as required.
  125.      *
  126.      * @return $this
  127.      */
  128.     public function isRequired(): static
  129.     {
  130.         $this->required true;
  131.         return $this;
  132.     }
  133.     /**
  134.      * Sets the node as deprecated.
  135.      *
  136.      * @param string $package The name of the composer package that is triggering the deprecation
  137.      * @param string $version The version of the package that introduced the deprecation
  138.      * @param string $message the deprecation message to use
  139.      *
  140.      * You can use %node% and %path% placeholders in your message to display,
  141.      * respectively, the node name and its complete path
  142.      *
  143.      * @return $this
  144.      */
  145.     public function setDeprecated(string $packagestring $versionstring $message 'The child node "%node%" at path "%path%" is deprecated.'): static
  146.     {
  147.         $this->deprecation = [
  148.             'package' => $package,
  149.             'version' => $version,
  150.             'message' => $message,
  151.         ];
  152.         return $this;
  153.     }
  154.     /**
  155.      * Sets the equivalent value used when the node contains null.
  156.      *
  157.      * @return $this
  158.      */
  159.     public function treatNullLike(mixed $value): static
  160.     {
  161.         $this->nullEquivalent $value;
  162.         return $this;
  163.     }
  164.     /**
  165.      * Sets the equivalent value used when the node contains true.
  166.      *
  167.      * @return $this
  168.      */
  169.     public function treatTrueLike(mixed $value): static
  170.     {
  171.         $this->trueEquivalent $value;
  172.         return $this;
  173.     }
  174.     /**
  175.      * Sets the equivalent value used when the node contains false.
  176.      *
  177.      * @return $this
  178.      */
  179.     public function treatFalseLike(mixed $value): static
  180.     {
  181.         $this->falseEquivalent $value;
  182.         return $this;
  183.     }
  184.     /**
  185.      * Sets null as the default value.
  186.      *
  187.      * @return $this
  188.      */
  189.     public function defaultNull(): static
  190.     {
  191.         return $this->defaultValue(null);
  192.     }
  193.     /**
  194.      * Sets true as the default value.
  195.      *
  196.      * @return $this
  197.      */
  198.     public function defaultTrue(): static
  199.     {
  200.         return $this->defaultValue(true);
  201.     }
  202.     /**
  203.      * Sets false as the default value.
  204.      *
  205.      * @return $this
  206.      */
  207.     public function defaultFalse(): static
  208.     {
  209.         return $this->defaultValue(false);
  210.     }
  211.     /**
  212.      * Sets an expression to run before the normalization.
  213.      */
  214.     public function beforeNormalization(): ExprBuilder
  215.     {
  216.         return $this->normalization()->before();
  217.     }
  218.     /**
  219.      * Denies the node value being empty.
  220.      *
  221.      * @return $this
  222.      */
  223.     public function cannotBeEmpty(): static
  224.     {
  225.         $this->allowEmptyValue false;
  226.         return $this;
  227.     }
  228.     /**
  229.      * Sets an expression to run for the validation.
  230.      *
  231.      * The expression receives the value of the node and must return it. It can
  232.      * modify it.
  233.      * An exception should be thrown when the node is not valid.
  234.      */
  235.     public function validate(): ExprBuilder
  236.     {
  237.         return $this->validation()->rule();
  238.     }
  239.     /**
  240.      * Sets whether the node can be overwritten.
  241.      *
  242.      * @return $this
  243.      */
  244.     public function cannotBeOverwritten(bool $deny true): static
  245.     {
  246.         $this->merge()->denyOverwrite($deny);
  247.         return $this;
  248.     }
  249.     /**
  250.      * Gets the builder for validation rules.
  251.      */
  252.     protected function validation(): ValidationBuilder
  253.     {
  254.         return $this->validation ??= new ValidationBuilder($this);
  255.     }
  256.     /**
  257.      * Gets the builder for merging rules.
  258.      */
  259.     protected function merge(): MergeBuilder
  260.     {
  261.         return $this->merge ??= new MergeBuilder($this);
  262.     }
  263.     /**
  264.      * Gets the builder for normalization rules.
  265.      */
  266.     protected function normalization(): NormalizationBuilder
  267.     {
  268.         return $this->normalization ??= new NormalizationBuilder($this);
  269.     }
  270.     /**
  271.      * Instantiate and configure the node according to this definition.
  272.      *
  273.      * @throws InvalidDefinitionException When the definition is invalid
  274.      */
  275.     abstract protected function createNode(): NodeInterface;
  276.     /**
  277.      * Set PathSeparator to use.
  278.      *
  279.      * @return $this
  280.      */
  281.     public function setPathSeparator(string $separator): static
  282.     {
  283.         if ($this instanceof ParentNodeDefinitionInterface) {
  284.             foreach ($this->getChildNodeDefinitions() as $child) {
  285.                 $child->setPathSeparator($separator);
  286.             }
  287.         }
  288.         $this->pathSeparator $separator;
  289.         return $this;
  290.     }
  291. }