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: /**
31: * {@inheritDoc}
32: */
33: public function setDefaultValue($value)
34: {
35: $this->defaultValueSet = true;
36: $this->defaultValue = $value;
37: }
38:
39: /**
40: * {@inheritDoc}
41: */
42: public function hasDefaultValue()
43: {
44: return $this->defaultValueSet;
45: }
46:
47: /**
48: * {@inheritDoc}
49: */
50: public function getDefaultValue()
51: {
52: return $this->defaultValue instanceof \Closure ? call_user_func($this->defaultValue) : $this->defaultValue;
53: }
54:
55: /**
56: * Sets if this node is allowed to have an empty value.
57: *
58: * @param Boolean $boolean True if this entity will accept empty values.
59: */
60: public function setAllowEmptyValue($boolean)
61: {
62: $this->allowEmptyValue = (Boolean) $boolean;
63: }
64:
65: /**
66: * {@inheritDoc}
67: */
68: public function setName($name)
69: {
70: $this->name = $name;
71: }
72:
73: /**
74: * {@inheritDoc}
75: */
76: protected function validateType($value)
77: {
78: }
79:
80: /**
81: * {@inheritDoc}
82: */
83: protected function finalizeValue($value)
84: {
85: if (!$this->allowEmptyValue && empty($value)) {
86: $ex = new InvalidConfigurationException(sprintf(
87: 'The path "%s" cannot contain an empty value, but got %s.',
88: $this->getPath(),
89: json_encode($value)
90: ));
91: $ex->setPath($this->getPath());
92:
93: throw $ex;
94: }
95:
96: return $value;
97: }
98:
99: /**
100: * {@inheritDoc}
101: */
102: protected function normalizeValue($value)
103: {
104: return $value;
105: }
106:
107: /**
108: * {@inheritDoc}
109: */
110: protected function mergeValues($leftSide, $rightSide)
111: {
112: return $rightSide;
113: }
114: }
115: