vendor/symfony/symfony/src/Symfony/Component/Config/Definition/ArrayNode.php line 252

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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;
  11. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  12. use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
  13. use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
  14. /**
  15.  * Represents an Array node in the config tree.
  16.  *
  17.  * @author Johannes M. Schmitt <[email protected]>
  18.  */
  19. class ArrayNode extends BaseNode implements PrototypeNodeInterface
  20. {
  21.     protected $xmlRemappings = array();
  22.     protected $children = array();
  23.     protected $allowFalse false;
  24.     protected $allowNewKeys true;
  25.     protected $addIfNotSet false;
  26.     protected $performDeepMerging true;
  27.     protected $ignoreExtraKeys false;
  28.     protected $removeExtraKeys true;
  29.     protected $normalizeKeys true;
  30.     public function setNormalizeKeys($normalizeKeys)
  31.     {
  32.         $this->normalizeKeys = (bool) $normalizeKeys;
  33.     }
  34.     /**
  35.      * Normalizes keys between the different configuration formats.
  36.      *
  37.      * Namely, you mostly have foo_bar in YAML while you have foo-bar in XML.
  38.      * After running this method, all keys are normalized to foo_bar.
  39.      *
  40.      * If you have a mixed key like foo-bar_moo, it will not be altered.
  41.      * The key will also not be altered if the target key already exists.
  42.      *
  43.      * @param mixed $value
  44.      *
  45.      * @return array The value with normalized keys
  46.      */
  47.     protected function preNormalize($value)
  48.     {
  49.         if (!$this->normalizeKeys || !is_array($value)) {
  50.             return $value;
  51.         }
  52.         $normalized = array();
  53.         foreach ($value as $k => $v) {
  54.             if (false !== strpos($k'-') && false === strpos($k'_') && !array_key_exists($normalizedKey str_replace('-''_'$k), $value)) {
  55.                 $normalized[$normalizedKey] = $v;
  56.             } else {
  57.                 $normalized[$k] = $v;
  58.             }
  59.         }
  60.         return $normalized;
  61.     }
  62.     /**
  63.      * Retrieves the children of this node.
  64.      *
  65.      * @return array The children
  66.      */
  67.     public function getChildren()
  68.     {
  69.         return $this->children;
  70.     }
  71.     /**
  72.      * Sets the xml remappings that should be performed.
  73.      *
  74.      * @param array $remappings An array of the form array(array(string, string))
  75.      */
  76.     public function setXmlRemappings(array $remappings)
  77.     {
  78.         $this->xmlRemappings $remappings;
  79.     }
  80.     /**
  81.      * Gets the xml remappings that should be performed.
  82.      *
  83.      * @return array $remappings an array of the form array(array(string, string))
  84.      */
  85.     public function getXmlRemappings()
  86.     {
  87.         return $this->xmlRemappings;
  88.     }
  89.     /**
  90.      * Sets whether to add default values for this array if it has not been
  91.      * defined in any of the configuration files.
  92.      *
  93.      * @param bool $boolean
  94.      */
  95.     public function setAddIfNotSet($boolean)
  96.     {
  97.         $this->addIfNotSet = (bool) $boolean;
  98.     }
  99.     /**
  100.      * Sets whether false is allowed as value indicating that the array should be unset.
  101.      *
  102.      * @param bool $allow
  103.      */
  104.     public function setAllowFalse($allow)
  105.     {
  106.         $this->allowFalse = (bool) $allow;
  107.     }
  108.     /**
  109.      * Sets whether new keys can be defined in subsequent configurations.
  110.      *
  111.      * @param bool $allow
  112.      */
  113.     public function setAllowNewKeys($allow)
  114.     {
  115.         $this->allowNewKeys = (bool) $allow;
  116.     }
  117.     /**
  118.      * Sets if deep merging should occur.
  119.      *
  120.      * @param bool $boolean
  121.      */
  122.     public function setPerformDeepMerging($boolean)
  123.     {
  124.         $this->performDeepMerging = (bool) $boolean;
  125.     }
  126.     /**
  127.      * Whether extra keys should just be ignore without an exception.
  128.      *
  129.      * @param bool $boolean To allow extra keys
  130.      * @param bool $remove  To remove extra keys
  131.      */
  132.     public function setIgnoreExtraKeys($boolean$remove true)
  133.     {
  134.         $this->ignoreExtraKeys = (bool) $boolean;
  135.         $this->removeExtraKeys $this->ignoreExtraKeys && $remove;
  136.     }
  137.     /**
  138.      * Sets the node Name.
  139.      *
  140.      * @param string $name The node's name
  141.      */
  142.     public function setName($name)
  143.     {
  144.         $this->name $name;
  145.     }
  146.     /**
  147.      * Checks if the node has a default value.
  148.      *
  149.      * @return bool
  150.      */
  151.     public function hasDefaultValue()
  152.     {
  153.         return $this->addIfNotSet;
  154.     }
  155.     /**
  156.      * Retrieves the default value.
  157.      *
  158.      * @return array The default value
  159.      *
  160.      * @throws \RuntimeException if the node has no default value
  161.      */
  162.     public function getDefaultValue()
  163.     {
  164.         if (!$this->hasDefaultValue()) {
  165.             throw new \RuntimeException(sprintf('The node at path "%s" has no default value.'$this->getPath()));
  166.         }
  167.         $defaults = array();
  168.         foreach ($this->children as $name => $child) {
  169.             if ($child->hasDefaultValue()) {
  170.                 $defaults[$name] = $child->getDefaultValue();
  171.             }
  172.         }
  173.         return $defaults;
  174.     }
  175.     /**
  176.      * Adds a child node.
  177.      *
  178.      * @throws \InvalidArgumentException when the child node has no name
  179.      * @throws \InvalidArgumentException when the child node's name is not unique
  180.      */
  181.     public function addChild(NodeInterface $node)
  182.     {
  183.         $name $node->getName();
  184.         if (!strlen($name)) {
  185.             throw new \InvalidArgumentException('Child nodes must be named.');
  186.         }
  187.         if (isset($this->children[$name])) {
  188.             throw new \InvalidArgumentException(sprintf('A child node named "%s" already exists.'$name));
  189.         }
  190.         $this->children[$name] = $node;
  191.     }
  192.     /**
  193.      * Finalizes the value of this node.
  194.      *
  195.      * @param mixed $value
  196.      *
  197.      * @return mixed The finalised value
  198.      *
  199.      * @throws UnsetKeyException
  200.      * @throws InvalidConfigurationException if the node doesn't have enough children
  201.      */
  202.     protected function finalizeValue($value)
  203.     {
  204.         if (false === $value) {
  205.             $msg sprintf('Unsetting key for path "%s", value: %s'$this->getPath(), json_encode($value));
  206.             throw new UnsetKeyException($msg);
  207.         }
  208.         foreach ($this->children as $name => $child) {
  209.             if (!array_key_exists($name$value)) {
  210.                 if ($child->isRequired()) {
  211.                     $msg sprintf('The child node "%s" at path "%s" must be configured.'$name$this->getPath());
  212.                     $ex = new InvalidConfigurationException($msg);
  213.                     $ex->setPath($this->getPath());
  214.                     throw $ex;
  215.                 }
  216.                 if ($child->hasDefaultValue()) {
  217.                     $value[$name] = $child->getDefaultValue();
  218.                 }
  219.                 continue;
  220.             }
  221.             if ($child->isDeprecated()) {
  222.                 @trigger_error($child->getDeprecationMessage($name$this->getPath()), E_USER_DEPRECATED);
  223.             }
  224.             try {
  225.                 $value[$name] = $child->finalize($value[$name]);
  226.             } catch (UnsetKeyException $e) {
  227.                 unset($value[$name]);
  228.             }
  229.         }
  230.         return $value;
  231.     }
  232.     /**
  233.      * Validates the type of the value.
  234.      *
  235.      * @param mixed $value
  236.      *
  237.      * @throws InvalidTypeException
  238.      */
  239.     protected function validateType($value)
  240.     {
  241.         if (!is_array($value) && (!$this->allowFalse || false !== $value)) {
  242.             $ex = new InvalidTypeException(sprintf(
  243.                 'Invalid type for path "%s". Expected array, but got %s',
  244.                 $this->getPath(),
  245.                 gettype($value)
  246.             ));
  247.             if ($hint $this->getInfo()) {
  248.                 $ex->addHint($hint);
  249.             }
  250.             $ex->setPath($this->getPath());
  251.             throw $ex;
  252.         }
  253.     }
  254.     /**
  255.      * Normalizes the value.
  256.      *
  257.      * @param mixed $value The value to normalize
  258.      *
  259.      * @return mixed The normalized value
  260.      *
  261.      * @throws InvalidConfigurationException
  262.      */
  263.     protected function normalizeValue($value)
  264.     {
  265.         if (false === $value) {
  266.             return $value;
  267.         }
  268.         $value $this->remapXml($value);
  269.         $normalized = array();
  270.         foreach ($value as $name => $val) {
  271.             if (isset($this->children[$name])) {
  272.                 $normalized[$name] = $this->children[$name]->normalize($val);
  273.                 unset($value[$name]);
  274.             } elseif (!$this->removeExtraKeys) {
  275.                 $normalized[$name] = $val;
  276.             }
  277.         }
  278.         // if extra fields are present, throw exception
  279.         if (count($value) && !$this->ignoreExtraKeys) {
  280.             $msg sprintf('Unrecognized option%s "%s" under "%s"'=== count($value) ? '' 's'implode(', 'array_keys($value)), $this->getPath());
  281.             $ex = new InvalidConfigurationException($msg);
  282.             $ex->setPath($this->getPath());
  283.             throw $ex;
  284.         }
  285.         return $normalized;
  286.     }
  287.     /**
  288.      * Remaps multiple singular values to a single plural value.
  289.      *
  290.      * @param array $value The source values
  291.      *
  292.      * @return array The remapped values
  293.      */
  294.     protected function remapXml($value)
  295.     {
  296.         foreach ($this->xmlRemappings as list($singular$plural)) {
  297.             if (!isset($value[$singular])) {
  298.                 continue;
  299.             }
  300.             $value[$plural] = Processor::normalizeConfig($value$singular$plural);
  301.             unset($value[$singular]);
  302.         }
  303.         return $value;
  304.     }
  305.     /**
  306.      * Merges values together.
  307.      *
  308.      * @param mixed $leftSide  The left side to merge
  309.      * @param mixed $rightSide The right side to merge
  310.      *
  311.      * @return mixed The merged values
  312.      *
  313.      * @throws InvalidConfigurationException
  314.      * @throws \RuntimeException
  315.      */
  316.     protected function mergeValues($leftSide$rightSide)
  317.     {
  318.         if (false === $rightSide) {
  319.             // if this is still false after the last config has been merged the
  320.             // finalization pass will take care of removing this key entirely
  321.             return false;
  322.         }
  323.         if (false === $leftSide || !$this->performDeepMerging) {
  324.             return $rightSide;
  325.         }
  326.         foreach ($rightSide as $k => $v) {
  327.             // no conflict
  328.             if (!array_key_exists($k$leftSide)) {
  329.                 if (!$this->allowNewKeys) {
  330.                     $ex = new InvalidConfigurationException(sprintf(
  331.                         'You are not allowed to define new elements for path "%s". '
  332.                        .'Please define all elements for this path in one config file. '
  333.                        .'If you are trying to overwrite an element, make sure you redefine it '
  334.                        .'with the same name.',
  335.                         $this->getPath()
  336.                     ));
  337.                     $ex->setPath($this->getPath());
  338.                     throw $ex;
  339.                 }
  340.                 $leftSide[$k] = $v;
  341.                 continue;
  342.             }
  343.             if (!isset($this->children[$k])) {
  344.                 throw new \RuntimeException('merge() expects a normalized config array.');
  345.             }
  346.             $leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v);
  347.         }
  348.         return $leftSide;
  349.     }
  350. }