NodeDefinition.php 7.3 KB

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