VariableNode.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. return $this->defaultValue instanceof \Closure ? call_user_func($this->defaultValue) : $this->defaultValue;
  46. }
  47. /**
  48. * Sets if this node is allowed to have an empty value.
  49. *
  50. * @param Boolean $boolean True if this entity will accept empty values.
  51. */
  52. public function setAllowEmptyValue($boolean)
  53. {
  54. $this->allowEmptyValue = (Boolean) $boolean;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function setName($name)
  60. {
  61. $this->name = $name;
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. protected function validateType($value)
  67. {
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. protected function finalizeValue($value)
  73. {
  74. if (!$this->allowEmptyValue && empty($value)) {
  75. $ex = new InvalidConfigurationException(sprintf(
  76. 'The path "%s" cannot contain an empty value, but got %s.',
  77. $this->getPath(),
  78. json_encode($value)
  79. ));
  80. $ex->setPath($this->getPath());
  81. throw $ex;
  82. }
  83. return $value;
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. protected function normalizeValue($value)
  89. {
  90. return $value;
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. protected function mergeValues($leftSide, $rightSide)
  96. {
  97. return $rightSide;
  98. }
  99. }