VariableNode.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(bool $boolean)
  51. {
  52. $this->allowEmptyValue = $boolean;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function setName(string $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. // deny environment variables only when using custom validators
  73. // this avoids ever passing an empty value to final validation closures
  74. if (!$this->allowEmptyValue && $this->isHandlingPlaceholder() && $this->finalValidationClosures) {
  75. $e = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an environment variable when empty values are not allowed by definition and are validated.', $this->getPath()));
  76. if ($hint = $this->getInfo()) {
  77. $e->addHint($hint);
  78. }
  79. $e->setPath($this->getPath());
  80. throw $e;
  81. }
  82. if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
  83. $ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
  84. if ($hint = $this->getInfo()) {
  85. $ex->addHint($hint);
  86. }
  87. $ex->setPath($this->getPath());
  88. throw $ex;
  89. }
  90. return $value;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function normalizeValue($value)
  96. {
  97. return $value;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function mergeValues($leftSide, $rightSide)
  103. {
  104. return $rightSide;
  105. }
  106. /**
  107. * Evaluates if the given value is to be treated as empty.
  108. *
  109. * By default, PHP's empty() function is used to test for emptiness. This
  110. * method may be overridden by subtypes to better match their understanding
  111. * of empty data.
  112. *
  113. * @param mixed $value
  114. *
  115. * @return bool
  116. *
  117. * @see finalizeValue()
  118. */
  119. protected function isValueEmpty($value)
  120. {
  121. return empty($value);
  122. }
  123. }