BaseNode.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Exception;
  12. use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException;
  13. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  14. /**
  15. * The base node class
  16. *
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. abstract class BaseNode implements NodeInterface
  20. {
  21. protected $name;
  22. protected $parent;
  23. protected $normalizationClosures;
  24. protected $finalValidationClosures;
  25. protected $allowOverwrite;
  26. protected $required;
  27. protected $equivalentValues;
  28. /**
  29. * Constructor.
  30. *
  31. * @param string $name The name of the node
  32. * @param NodeInterface $parent The parent of this node
  33. * @throws \InvalidArgumentException if the name contains a period.
  34. */
  35. public function __construct($name, NodeInterface $parent = null)
  36. {
  37. if (false !== strpos($name, '.')) {
  38. throw new \InvalidArgumentException('The name must not contain ".".');
  39. }
  40. $this->name = $name;
  41. $this->parent = $parent;
  42. $this->normalizationClosures = array();
  43. $this->finalValidationClosures = array();
  44. $this->allowOverwrite = true;
  45. $this->required = false;
  46. $this->equivalentValues = array();
  47. }
  48. /**
  49. * Adds an equivilent value.
  50. *
  51. * @param mixed $originalValue
  52. * @param mixed $equivalentValue
  53. * @return void
  54. */
  55. public function addEquivalentValue($originalValue, $equivalentValue)
  56. {
  57. $this->equivalentValues[] = array($originalValue, $equivalentValue);
  58. }
  59. /**
  60. * Set this node as required.
  61. *
  62. * @param boolean $boolean Required node
  63. * @return void
  64. */
  65. public function setRequired($boolean)
  66. {
  67. $this->required = (Boolean) $boolean;
  68. }
  69. /**
  70. * Sets if this node can be overridden.
  71. *
  72. * @param boolean $allow
  73. * @return void
  74. */
  75. public function setAllowOverwrite($allow)
  76. {
  77. $this->allowOverwrite = (Boolean) $allow;
  78. }
  79. /**
  80. * Sets the closures used for normalization.
  81. *
  82. * @param array $closures An array of Closures used for normalization
  83. * @return void
  84. */
  85. public function setNormalizationClosures(array $closures)
  86. {
  87. $this->normalizationClosures = $closures;
  88. }
  89. /**
  90. * Sets the closures used for final validation.
  91. *
  92. * @param array $closures An array of Closures used for final validation
  93. * @return void
  94. */
  95. public function setFinalValidationClosures(array $closures)
  96. {
  97. $this->finalValidationClosures = $closures;
  98. }
  99. /**
  100. * Checks if this node is required.
  101. *
  102. * @return boolean
  103. */
  104. public function isRequired()
  105. {
  106. return $this->required;
  107. }
  108. /**
  109. * Returns the name of this node
  110. *
  111. * @return string The Node's name.
  112. */
  113. public function getName()
  114. {
  115. return $this->name;
  116. }
  117. /**
  118. * Retrieves the path of this node.
  119. *
  120. * @return string The Node's path
  121. */
  122. public function getPath()
  123. {
  124. $path = $this->name;
  125. if (null !== $this->parent) {
  126. $path = $this->parent->getPath().'.'.$path;
  127. }
  128. return $path;
  129. }
  130. /**
  131. * Merges two values together.
  132. *
  133. * @param mixed $leftSide
  134. * @param mixed $rightSide
  135. * @return mixed The merged value
  136. * @throws ForbiddenOverwriteException
  137. */
  138. public final function merge($leftSide, $rightSide)
  139. {
  140. if (!$this->allowOverwrite) {
  141. throw new ForbiddenOverwriteException(sprintf(
  142. 'Configuration path "%s" cannot be overwritten. You have to '
  143. .'define all options for this path, and any of its sub-paths in '
  144. .'one configuration section.',
  145. $this->getPath()
  146. ));
  147. }
  148. $this->validateType($leftSide);
  149. $this->validateType($rightSide);
  150. return $this->mergeValues($leftSide, $rightSide);
  151. }
  152. /**
  153. * Normalizes a value, applying all normalization closures.
  154. *
  155. * @param mixed $value Value to normalize.
  156. * @return mied The normalized value.
  157. */
  158. public final function normalize($value)
  159. {
  160. // run custom normalization closures
  161. foreach ($this->normalizationClosures as $closure) {
  162. $value = $closure($value);
  163. }
  164. // replace value with their equivalent
  165. foreach ($this->equivalentValues as $data) {
  166. if ($data[0] === $value) {
  167. $value = $data[1];
  168. }
  169. }
  170. // validate type
  171. $this->validateType($value);
  172. // normalize value
  173. return $this->normalizeValue($value);
  174. }
  175. /**
  176. * Finalizes a value, applying all finalization closures.
  177. *
  178. * @param mixed $value The value to finalize
  179. * @return mixed The finalized value
  180. */
  181. public final function finalize($value)
  182. {
  183. $this->validateType($value);
  184. $value = $this->finalizeValue($value);
  185. // Perform validation on the final value if a closure has been set.
  186. // The closure is also allowed to return another value.
  187. foreach ($this->finalValidationClosures as $closure) {
  188. try {
  189. $value = $closure($value);
  190. } catch (Exception $correctEx) {
  191. throw $correctEx;
  192. } catch (\Exception $invalid) {
  193. throw new InvalidConfigurationException(sprintf(
  194. 'Invalid configuration for path "%s": %s',
  195. $this->getPath(),
  196. $invalid->getMessage()
  197. ), $invalid->getCode(), $invalid);
  198. }
  199. }
  200. return $value;
  201. }
  202. /**
  203. * Validates the type of a Node.
  204. *
  205. * @param mixed $value The value to validate
  206. * @return void
  207. * @throws \InvalidTypeException when the value is invalid
  208. */
  209. abstract protected function validateType($value);
  210. /**
  211. * Normalizes the value.
  212. *
  213. * @param mixed $value The value to normalize.
  214. * @return mixed The normalized value
  215. */
  216. abstract protected function normalizeValue($value);
  217. /**
  218. * Merges two values together
  219. *
  220. * @param mixed $leftSide
  221. * @param mixed $rightSide
  222. * @return mixed The merged value
  223. */
  224. abstract protected function mergeValues($leftSide, $rightSide);
  225. /**
  226. * Finalizes a value
  227. *
  228. * @param mixed $value The value to finalize
  229. * @return mixed The finalized value
  230. */
  231. abstract protected function finalizeValue($value);
  232. }