NodeDefinition.php 8.3 KB

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