aboutsummaryrefslogtreecommitdiff
path: root/lib/exTpl/ConditionNode.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/exTpl/ConditionNode.php')
-rw-r--r--lib/exTpl/ConditionNode.php59
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) : '';
+ }
+}