VariableNode.php 2.9 KB

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