VariableNode.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
  74. if ($hint = $this->getInfo()) {
  75. $ex->addHint($hint);
  76. }
  77. $ex->setPath($this->getPath());
  78. throw $ex;
  79. }
  80. return $value;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function normalizeValue($value)
  86. {
  87. return $value;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function mergeValues($leftSide, $rightSide)
  93. {
  94. return $rightSide;
  95. }
  96. /**
  97. * Evaluates if the given value is to be treated as empty.
  98. *
  99. * By default, PHP's empty() function is used to test for emptiness. This
  100. * method may be overridden by subtypes to better match their understanding
  101. * of empty data.
  102. *
  103. * @param mixed $value
  104. *
  105. * @return bool
  106. */
  107. protected function isValueEmpty($value)
  108. {
  109. return empty($value);
  110. }
  111. }