vendor/symfony/src/Symfony/Component/Config/Definition/VariableNode.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Config\Definition;
       
    13 
       
    14 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
       
    15 use Symfony\Component\Config\Definition\Builder\NodeDefinition;
       
    16 
       
    17 /**
       
    18  * This node represents a variable value in the config tree.
       
    19  *
       
    20  * This node is intended for arbitrary variables.
       
    21  * Any PHP type is accepted as a value.
       
    22  *
       
    23  * @author Jeremy Mikola <jmikola@gmail.com>
       
    24  */
       
    25 class VariableNode extends BaseNode implements PrototypeNodeInterface
       
    26 {
       
    27     protected $defaultValueSet = false;
       
    28     protected $defaultValue;
       
    29     protected $allowEmptyValue = true;
       
    30 
       
    31     /**
       
    32      * {@inheritDoc}
       
    33      */
       
    34     public function setDefaultValue($value)
       
    35     {
       
    36         $this->defaultValueSet = true;
       
    37         $this->defaultValue = $value;
       
    38     }
       
    39 
       
    40     /**
       
    41      * {@inheritDoc}
       
    42      */
       
    43     public function hasDefaultValue()
       
    44     {
       
    45         return $this->defaultValueSet;
       
    46     }
       
    47 
       
    48     /**
       
    49      * {@inheritDoc}
       
    50      */
       
    51     public function getDefaultValue()
       
    52     {
       
    53         return $this->defaultValue instanceof \Closure ? call_user_func($this->defaultValue) : $this->defaultValue;
       
    54     }
       
    55 
       
    56     /**
       
    57      * Sets if this node is allowed to have an empty value.
       
    58      *
       
    59      * @param Boolean $boolean True if this entity will accept empty values.
       
    60      */
       
    61     public function setAllowEmptyValue($boolean)
       
    62     {
       
    63         $this->allowEmptyValue = (Boolean) $boolean;
       
    64     }
       
    65 
       
    66     /**
       
    67      * {@inheritDoc}
       
    68      */
       
    69     public function setName($name)
       
    70     {
       
    71         $this->name = $name;
       
    72     }
       
    73 
       
    74     /**
       
    75      * {@inheritDoc}
       
    76      */
       
    77     protected function validateType($value)
       
    78     {
       
    79     }
       
    80 
       
    81     /**
       
    82      * {@inheritDoc}
       
    83      */
       
    84     protected function finalizeValue($value)
       
    85     {
       
    86         if (!$this->allowEmptyValue && empty($value)) {
       
    87             $ex = new InvalidConfigurationException(sprintf(
       
    88                 'The path "%s" cannot contain an empty value, but got %s.',
       
    89                 $this->getPath(),
       
    90                 json_encode($value)
       
    91             ));
       
    92             $ex->setPath($this->getPath());
       
    93 
       
    94             throw $ex;
       
    95         }
       
    96 
       
    97         return $value;
       
    98     }
       
    99 
       
   100     /**
       
   101      * {@inheritDoc}
       
   102      */
       
   103     protected function normalizeValue($value)
       
   104     {
       
   105         return $value;
       
   106     }
       
   107 
       
   108     /**
       
   109      * {@inheritDoc}
       
   110      */
       
   111     protected function mergeValues($leftSide, $rightSide)
       
   112     {
       
   113         return $rightSide;
       
   114     }
       
   115 }