NodeDefinition.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\Builder;
  11. use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
  12. use Symfony\Component\Config\Definition\NodeInterface;
  13. /**
  14. * This class provides a fluent interface for defining a node.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. abstract class NodeDefinition implements NodeParentInterface
  19. {
  20. protected $name;
  21. protected $normalization;
  22. protected $validation;
  23. protected $defaultValue;
  24. protected $default = false;
  25. protected $required = false;
  26. protected $merge;
  27. protected $allowEmptyValue = true;
  28. protected $nullEquivalent;
  29. protected $trueEquivalent = true;
  30. protected $falseEquivalent = false;
  31. protected $parent;
  32. protected $attributes = array();
  33. /**
  34. * @param string|null $name The name of the node
  35. * @param NodeParentInterface|null $parent The parent
  36. */
  37. public function __construct($name, NodeParentInterface $parent = null)
  38. {
  39. $this->parent = $parent;
  40. $this->name = $name;
  41. }
  42. /**
  43. * Sets the parent node.
  44. *
  45. * @return $this
  46. */
  47. public function setParent(NodeParentInterface $parent)
  48. {
  49. $this->parent = $parent;
  50. return $this;
  51. }
  52. /**
  53. * Sets info message.
  54. *
  55. * @param string $info The info text
  56. *
  57. * @return $this
  58. */
  59. public function info($info)
  60. {
  61. return $this->attribute('info', $info);
  62. }
  63. /**
  64. * Sets example configuration.
  65. *
  66. * @param string|array $example
  67. *
  68. * @return $this
  69. */
  70. public function example($example)
  71. {
  72. return $this->attribute('example', $example);
  73. }
  74. /**
  75. * Sets an attribute on the node.
  76. *
  77. * @param string $key
  78. * @param mixed $value
  79. *
  80. * @return $this
  81. */
  82. public function attribute($key, $value)
  83. {
  84. $this->attributes[$key] = $value;
  85. return $this;
  86. }
  87. /**
  88. * Returns the parent node.
  89. *
  90. * @return NodeParentInterface|NodeBuilder|NodeDefinition|ArrayNodeDefinition|VariableNodeDefinition|null The builder of the parent node
  91. */
  92. public function end()
  93. {
  94. return $this->parent;
  95. }
  96. /**
  97. * Creates the node.
  98. *
  99. * @param bool $forceRootNode Whether to force this node as the root node
  100. *
  101. * @return NodeInterface
  102. */
  103. public function getNode($forceRootNode = false)
  104. {
  105. if ($forceRootNode) {
  106. $this->parent = null;
  107. }
  108. if (null !== $this->normalization) {
  109. $this->normalization->before = ExprBuilder::buildExpressions($this->normalization->before);
  110. }
  111. if (null !== $this->validation) {
  112. $this->validation->rules = ExprBuilder::buildExpressions($this->validation->rules);
  113. }
  114. $node = $this->createNode();
  115. $node->setAttributes($this->attributes);
  116. return $node;
  117. }
  118. /**
  119. * Sets the default value.
  120. *
  121. * @param mixed $value The default value
  122. *
  123. * @return $this
  124. */
  125. public function defaultValue($value)
  126. {
  127. $this->default = true;
  128. $this->defaultValue = $value;
  129. return $this;
  130. }
  131. /**
  132. * Sets the node as required.
  133. *
  134. * @return $this
  135. */
  136. public function isRequired()
  137. {
  138. $this->required = true;
  139. return $this;
  140. }
  141. /**
  142. * Sets the equivalent value used when the node contains null.
  143. *
  144. * @param mixed $value
  145. *
  146. * @return $this
  147. */
  148. public function treatNullLike($value)
  149. {
  150. $this->nullEquivalent = $value;
  151. return $this;
  152. }
  153. /**
  154. * Sets the equivalent value used when the node contains true.
  155. *
  156. * @param mixed $value
  157. *
  158. * @return $this
  159. */
  160. public function treatTrueLike($value)
  161. {
  162. $this->trueEquivalent = $value;
  163. return $this;
  164. }
  165. /**
  166. * Sets the equivalent value used when the node contains false.
  167. *
  168. * @param mixed $value
  169. *
  170. * @return $this
  171. */
  172. public function treatFalseLike($value)
  173. {
  174. $this->falseEquivalent = $value;
  175. return $this;
  176. }
  177. /**
  178. * Sets null as the default value.
  179. *
  180. * @return $this
  181. */
  182. public function defaultNull()
  183. {
  184. return $this->defaultValue(null);
  185. }
  186. /**
  187. * Sets true as the default value.
  188. *
  189. * @return $this
  190. */
  191. public function defaultTrue()
  192. {
  193. return $this->defaultValue(true);
  194. }
  195. /**
  196. * Sets false as the default value.
  197. *
  198. * @return $this
  199. */
  200. public function defaultFalse()
  201. {
  202. return $this->defaultValue(false);
  203. }
  204. /**
  205. * Sets an expression to run before the normalization.
  206. *
  207. * @return ExprBuilder
  208. */
  209. public function beforeNormalization()
  210. {
  211. return $this->normalization()->before();
  212. }
  213. /**
  214. * Denies the node value being empty.
  215. *
  216. * @return $this
  217. */
  218. public function cannotBeEmpty()
  219. {
  220. $this->allowEmptyValue = false;
  221. return $this;
  222. }
  223. /**
  224. * Sets an expression to run for the validation.
  225. *
  226. * The expression receives the value of the node and must return it. It can
  227. * modify it.
  228. * An exception should be thrown when the node is not valid.
  229. *
  230. * @return ExprBuilder
  231. */
  232. public function validate()
  233. {
  234. return $this->validation()->rule();
  235. }
  236. /**
  237. * Sets whether the node can be overwritten.
  238. *
  239. * @param bool $deny Whether the overwriting is forbidden or not
  240. *
  241. * @return $this
  242. */
  243. public function cannotBeOverwritten($deny = true)
  244. {
  245. $this->merge()->denyOverwrite($deny);
  246. return $this;
  247. }
  248. /**
  249. * Gets the builder for validation rules.
  250. *
  251. * @return ValidationBuilder
  252. */
  253. protected function validation()
  254. {
  255. if (null === $this->validation) {
  256. $this->validation = new ValidationBuilder($this);
  257. }
  258. return $this->validation;
  259. }
  260. /**
  261. * Gets the builder for merging rules.
  262. *
  263. * @return MergeBuilder
  264. */
  265. protected function merge()
  266. {
  267. if (null === $this->merge) {
  268. $this->merge = new MergeBuilder($this);
  269. }
  270. return $this->merge;
  271. }
  272. /**
  273. * Gets the builder for normalization rules.
  274. *
  275. * @return NormalizationBuilder
  276. */
  277. protected function normalization()
  278. {
  279. if (null === $this->normalization) {
  280. $this->normalization = new NormalizationBuilder($this);
  281. }
  282. return $this->normalization;
  283. }
  284. /**
  285. * Instantiate and configure the node according to this definition.
  286. *
  287. * @return NodeInterface The node instance
  288. *
  289. * @throws InvalidDefinitionException When the definition is invalid
  290. */
  291. abstract protected function createNode();
  292. }