BaseNode.php 8.5 KB

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