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

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)
  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)
  57.     {
  58.         return $this->attribute('info'$info);
  59.     }
  60.     /**
  61.      * Sets example configuration.
  62.      *
  63.      * @param string|array $example
  64.      *
  65.      * @return $this
  66.      */
  67.     public function example($example)
  68.     {
  69.         return $this->attribute('example'$example);
  70.     }
  71.     /**
  72.      * Sets an attribute on the node.
  73.      *
  74.      * @param mixed $value
  75.      *
  76.      * @return $this
  77.      */
  78.     public function attribute(string $key$value)
  79.     {
  80.         $this->attributes[$key] = $value;
  81.         return $this;
  82.     }
  83.     /**
  84.      * Returns the parent node.
  85.      *
  86.      * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
  87.      */
  88.     public function end()
  89.     {
  90.         return $this->parent;
  91.     }
  92.     /**
  93.      * Creates the node.
  94.      *
  95.      * @param bool $forceRootNode Whether to force this node as the root node
  96.      *
  97.      * @return NodeInterface
  98.      */
  99.     public function getNode(bool $forceRootNode false)
  100.     {
  101.         if ($forceRootNode) {
  102.             $this->parent null;
  103.         }
  104.         if (null !== $this->normalization) {
  105.             $this->normalization->before ExprBuilder::buildExpressions($this->normalization->before);
  106.         }
  107.         if (null !== $this->validation) {
  108.             $this->validation->rules ExprBuilder::buildExpressions($this->validation->rules);
  109.         }
  110.         $node $this->createNode();
  111.         if ($node instanceof BaseNode) {
  112.             $node->setAttributes($this->attributes);
  113.         }
  114.         return $node;
  115.     }
  116.     /**
  117.      * Sets the default value.
  118.      *
  119.      * @param mixed $value The default value
  120.      *
  121.      * @return $this
  122.      */
  123.     public function defaultValue($value)
  124.     {
  125.         $this->default true;
  126.         $this->defaultValue $value;
  127.         return $this;
  128.     }
  129.     /**
  130.      * Sets the node as required.
  131.      *
  132.      * @return $this
  133.      */
  134.     public function isRequired()
  135.     {
  136.         $this->required true;
  137.         return $this;
  138.     }
  139.     /**
  140.      * Sets the node as deprecated.
  141.      *
  142.      * @param string $package The name of the composer package that is triggering the deprecation
  143.      * @param string $version The version of the package that introduced the deprecation
  144.      * @param string $message the deprecation message to use
  145.      *
  146.      * You can use %node% and %path% placeholders in your message to display,
  147.      * respectively, the node name and its complete path
  148.      *
  149.      * @return $this
  150.      */
  151.     public function setDeprecated(/* string $package, string $version, string $message = 'The child node "%node%" at path "%path%" is deprecated.' */)
  152.     {
  153.         $args \func_get_args();
  154.         if (\func_num_args() < 2) {
  155.             trigger_deprecation('symfony/config''5.1''The signature of method "%s()" requires 3 arguments: "string $package, string $version, string $message", not defining them is deprecated.'__METHOD__);
  156.             $message $args[0] ?? 'The child node "%node%" at path "%path%" is deprecated.';
  157.             $package $version '';
  158.         } else {
  159.             $package = (string) $args[0];
  160.             $version = (string) $args[1];
  161.             $message = (string) ($args[2] ?? 'The child node "%node%" at path "%path%" is deprecated.');
  162.         }
  163.         $this->deprecation = [
  164.             'package' => $package,
  165.             'version' => $version,
  166.             'message' => $message,
  167.         ];
  168.         return $this;
  169.     }
  170.     /**
  171.      * Sets the equivalent value used when the node contains null.
  172.      *
  173.      * @param mixed $value
  174.      *
  175.      * @return $this
  176.      */
  177.     public function treatNullLike($value)
  178.     {
  179.         $this->nullEquivalent $value;
  180.         return $this;
  181.     }
  182.     /**
  183.      * Sets the equivalent value used when the node contains true.
  184.      *
  185.      * @param mixed $value
  186.      *
  187.      * @return $this
  188.      */
  189.     public function treatTrueLike($value)
  190.     {
  191.         $this->trueEquivalent $value;
  192.         return $this;
  193.     }
  194.     /**
  195.      * Sets the equivalent value used when the node contains false.
  196.      *
  197.      * @param mixed $value
  198.      *
  199.      * @return $this
  200.      */
  201.     public function treatFalseLike($value)
  202.     {
  203.         $this->falseEquivalent $value;
  204.         return $this;
  205.     }
  206.     /**
  207.      * Sets null as the default value.
  208.      *
  209.      * @return $this
  210.      */
  211.     public function defaultNull()
  212.     {
  213.         return $this->defaultValue(null);
  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.      * Sets false as the default value.
  226.      *
  227.      * @return $this
  228.      */
  229.     public function defaultFalse()
  230.     {
  231.         return $this->defaultValue(false);
  232.     }
  233.     /**
  234.      * Sets an expression to run before the normalization.
  235.      *
  236.      * @return ExprBuilder
  237.      */
  238.     public function beforeNormalization()
  239.     {
  240.         return $this->normalization()->before();
  241.     }
  242.     /**
  243.      * Denies the node value being empty.
  244.      *
  245.      * @return $this
  246.      */
  247.     public function cannotBeEmpty()
  248.     {
  249.         $this->allowEmptyValue false;
  250.         return $this;
  251.     }
  252.     /**
  253.      * Sets an expression to run for the validation.
  254.      *
  255.      * The expression receives the value of the node and must return it. It can
  256.      * modify it.
  257.      * An exception should be thrown when the node is not valid.
  258.      *
  259.      * @return ExprBuilder
  260.      */
  261.     public function validate()
  262.     {
  263.         return $this->validation()->rule();
  264.     }
  265.     /**
  266.      * Sets whether the node can be overwritten.
  267.      *
  268.      * @return $this
  269.      */
  270.     public function cannotBeOverwritten(bool $deny true)
  271.     {
  272.         $this->merge()->denyOverwrite($deny);
  273.         return $this;
  274.     }
  275.     /**
  276.      * Gets the builder for validation rules.
  277.      *
  278.      * @return ValidationBuilder
  279.      */
  280.     protected function validation()
  281.     {
  282.         if (null === $this->validation) {
  283.             $this->validation = new ValidationBuilder($this);
  284.         }
  285.         return $this->validation;
  286.     }
  287.     /**
  288.      * Gets the builder for merging rules.
  289.      *
  290.      * @return MergeBuilder
  291.      */
  292.     protected function merge()
  293.     {
  294.         if (null === $this->merge) {
  295.             $this->merge = new MergeBuilder($this);
  296.         }
  297.         return $this->merge;
  298.     }
  299.     /**
  300.      * Gets the builder for normalization rules.
  301.      *
  302.      * @return NormalizationBuilder
  303.      */
  304.     protected function normalization()
  305.     {
  306.         if (null === $this->normalization) {
  307.             $this->normalization = new NormalizationBuilder($this);
  308.         }
  309.         return $this->normalization;
  310.     }
  311.     /**
  312.      * Instantiate and configure the node according to this definition.
  313.      *
  314.      * @return NodeInterface The node instance
  315.      *
  316.      * @throws InvalidDefinitionException When the definition is invalid
  317.      */
  318.     abstract protected function createNode();
  319.     /**
  320.      * Set PathSeparator to use.
  321.      *
  322.      * @return $this
  323.      */
  324.     public function setPathSeparator(string $separator)
  325.     {
  326.         if ($this instanceof ParentNodeDefinitionInterface) {
  327.             foreach ($this->getChildNodeDefinitions() as $child) {
  328.                 $child->setPathSeparator($separator);
  329.             }
  330.         }
  331.         $this->pathSeparator $separator;
  332.         return $this;
  333.     }
  334. }