vendor/symfony/config/Definition/VariableNode.php line 51

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;
  11. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  12. /**
  13.  * This node represents a value of variable type in the config tree.
  14.  *
  15.  * This node is intended for values of arbitrary type.
  16.  * Any PHP type is accepted as a value.
  17.  *
  18.  * @author Jeremy Mikola <jmikola@gmail.com>
  19.  */
  20. class VariableNode extends BaseNode implements PrototypeNodeInterface
  21. {
  22.     protected $defaultValueSet false;
  23.     protected $defaultValue;
  24.     protected $allowEmptyValue true;
  25.     public function setDefaultValue($value)
  26.     {
  27.         $this->defaultValueSet true;
  28.         $this->defaultValue $value;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function hasDefaultValue()
  34.     {
  35.         return $this->defaultValueSet;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getDefaultValue()
  41.     {
  42.         $v $this->defaultValue;
  43.         return $v instanceof \Closure $v() : $v;
  44.     }
  45.     /**
  46.      * Sets if this node is allowed to have an empty value.
  47.      *
  48.      * @param bool $boolean True if this entity will accept empty values
  49.      */
  50.     public function setAllowEmptyValue($boolean)
  51.     {
  52.         $this->allowEmptyValue = (bool) $boolean;
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function setName($name)
  58.     {
  59.         $this->name $name;
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     protected function validateType($value)
  65.     {
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     protected function finalizeValue($value)
  71.     {
  72.         if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
  73.             $ex = new InvalidConfigurationException(sprintf(
  74.                 'The path "%s" cannot contain an empty value, but got %s.',
  75.                 $this->getPath(),
  76.                 json_encode($value)
  77.             ));
  78.             if ($hint $this->getInfo()) {
  79.                 $ex->addHint($hint);
  80.             }
  81.             $ex->setPath($this->getPath());
  82.             throw $ex;
  83.         }
  84.         return $value;
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     protected function normalizeValue($value)
  90.     {
  91.         return $value;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     protected function mergeValues($leftSide$rightSide)
  97.     {
  98.         return $rightSide;
  99.     }
  100.     /**
  101.      * Evaluates if the given value is to be treated as empty.
  102.      *
  103.      * By default, PHP's empty() function is used to test for emptiness. This
  104.      * method may be overridden by subtypes to better match their understanding
  105.      * of empty data.
  106.      *
  107.      * @param mixed $value
  108.      *
  109.      * @return bool
  110.      */
  111.     protected function isValueEmpty($value)
  112.     {
  113.         return empty($value);
  114.     }
  115. }