diff options
| author | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
|---|---|---|
| committer | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
| commit | 4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch) | |
| tree | 5c07151ae61276d334e88f6309c30d439a85c12e /lib/exTpl/ConditionNode.php | |
| parent | da0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff) | |
| parent | 97a188592c679890a25c37ab78463add76a52ff7 (diff) | |
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/exTpl/ConditionNode.php')
| -rw-r--r-- | lib/exTpl/ConditionNode.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/exTpl/ConditionNode.php b/lib/exTpl/ConditionNode.php new file mode 100644 index 0000000..bab2121 --- /dev/null +++ b/lib/exTpl/ConditionNode.php @@ -0,0 +1,59 @@ +<?php + +namespace exTpl; + +/** + * ConditionNode represents a single condition tag: + * "{if CONDITION}...{else}...{endif}". + */ +class ConditionNode extends ArrayNode +{ + protected Expression $condition; + protected ArrayNode|null $else_node = null; + + /** + * Initializes a new Node instance with the given expression. + * + * @param Expression $condition expression object + */ + public function __construct(Expression $condition) + { + $this->condition = $condition; + } + + /** + * Adds an else block to this condition node. + */ + public function addElse(): void + { + $this->else_node = new ArrayNode(); + } + + /** + * Adds a child node to this condition node. + * + * @param Node $node child node to add + */ + public function addChild(Node $node): void + { + if ($this->else_node) { + $this->else_node->addChild($node); + } else { + parent::addChild($node); + } + } + + /** + * Returns a string representation of this node. + * + * @param Context $context symbol table + */ + public function render(Context $context): string + { + if ($this->condition->value($context)) { + return parent::render($context); + } + + return $this->else_node ? $this->else_node->render($context) : ''; + } +} |
