<?php
 class vB_Utility_Template_ParserNode_Tag_If extends vB_Utility_Template_ParserNode_Tag { public function validate() : array { $errors = []; if (trim($this->main_node->attributes['condition'] ?? '') === '') { $errors[] = ['empty_if_condition']; } else { $errors = $this->validateCondition($this->main_node->attributes['condition']); } return $errors; } public function compile() : string { $conditions = []; $current = [ 'condition' => $this->main_node->attributes['condition'] ?? '', 'value' => '', ]; foreach ($this->main_node->childNodes() AS $key => $node) { if ($node->type == 'tag' AND in_array($node->value, ['elseif', 'else'])) { $conditions[] = $current; $current = [ 'condition' => null, 'value' => '', ]; if ($node->value == 'elseif') { $current['condition'] = $node->attributes['condition'] ?? ''; } } else { $current['value'] .= (self::createNode($node)->compile()) . ' . '; } } $conditions[] = $current; $blocks = []; $elseblock = null; foreach ($conditions AS $condition) { $block = ''; $ifheader = ''; if (!is_null($condition['condition'])) { $expression = $condition['condition']; $ifheader = 'if (' . $expression . ') '; } $block = $ifheader . '{' . "\n\t" . $this->parser->outputVar . " .= " . $condition['value'] . "'';" . "\n" . '}'; if (!is_null($condition['condition'])) { $blocks[] = $block; } else { $elseblock = $block; } } if ($elseblock) { $blocks[] = $elseblock; } return "'';\n" . implode(' else ', $blocks) . $this->parser->outputVar . " .= ''"; } } 