VariableNode.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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 variable value in the config tree.
  14. *
  15. * This node is intended for arbitrary variables.
  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;
  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. return true;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. protected function finalizeValue($value)
  74. {
  75. if (!$this->allowEmptyValue && empty($value)) {
  76. throw new InvalidConfigurationException(sprintf(
  77. 'The path "%s" cannot contain an empty value, but got %s.',
  78. $this->getPath(),
  79. json_encode($value)
  80. ));
  81. }
  82. return $value;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. protected function normalizeValue($value)
  88. {
  89. return $value;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. protected function mergeValues($leftSide, $rightSide)
  95. {
  96. return $rightSide;
  97. }
  98. }