NodeDefinition.php 7.2 KB

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