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