<?php
 class vB_Utility_Template_ParserNode_Tag_Each extends vB_Utility_Template_ParserNode_Tag { public function validate() : array { $errors = []; if (strpos(implode('', $this->main_node->attributes), '(') !== false) { throw new vB_Utility_Template_Parser_FatalError('template_text_not_safe'); } if (!$this->validateVar($this->main_node->attributes['from'])) { $errors[] = ['invalid_each_from']; } if (!$this->validateVar($this->main_node->attributes['value'], false)) { $errors[] = ['invalid_each_value']; } if (isset($this->main_node->attributes['key'])) { if (!$this->validateVar($this->main_node->attributes['key'], false)) { $errors[] = ['invalid_each_key']; } } if (isset($this->main_node->attributes['index'])) { if (!$this->validateVar($this->main_node->attributes['index'], false)) { $errors[] = ['invalid_each_index']; } } return $errors; } public function compile() : string { $key = ''; if (isset($this->main_node->attributes['key'])) { $key = $this->compileVar($this->main_node->attributes['key']); } $indexInit = ''; $indexIncrement = ''; if (isset($this->main_node->attributes['index'])) { $index = $this->compileVar($this->main_node->attributes['index']); $indexInit = $index . ' = 0; '; $indexIncrement = $index . '++; '; } $from = $this->compileVar($this->main_node->attributes['from']); $value = $this->compileVar($this->main_node->attributes['value']); $children = $this->parser->_parse_nodes($this->main_node->childNodes()); if ($children) { $children = $this->parser->outputVar . " .= " . $children . "; "; } return "''" . '; if (isset(' . $from . ') AND (is_array(' . $from . ') OR ' . $from . ' instanceof ArrayAccess)) { ' . $indexInit . ' foreach (' . $from . ' AS ' . ($key ? $key . ' => ' : '') . $value . ') {' . $children . $indexIncrement . "} }" . $this->parser->outputVar . " .= ''"; } } 