BaseNode.php 7.7 KB

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