BaseNode.php 8.5 KB

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