<?php
 class vB_Utility_Template_ParserNode_Curly_Math extends vB_Utility_Template_ParserNode_Curly { public function validate() : array { $errors = []; $attributecount = sizeof($this->main_node->attributes); if ($attributecount == 0) { $errors[] = 'no_variable_specified'; } else { try { $this->buildMathParser()->compile(); } catch (vB_Utility_Template_Exception_MathParse $e) { $errors[] = ['invalid_math_expression', $this->buildMathString()]; } } return $errors; } public function compile() : string { try { return 'vB_Template_Runtime::runMaths(' . $this->buildMathParser()->compile() . ')'; } catch (vB_Utility_Template_Exception_MathParse $e) { return var_export('/* Invalid math expression */', true); } } private function buildMathParser() : vB_Utility_Template_MathParser { $entries = []; foreach ($this->main_node->attributes AS $attribute) { if ($attribute instanceof vB_CurlyNode) { $entries[] = [ 'type' => 'operand', 'value' => 'vB_Template_Runtime::mathOperand(' . $this->attributeToString($attribute) . ')', ]; } else { $entries[] = ['type' => 'literal', 'value' => (string) $attribute]; } } return new vB_Utility_Template_MathParser($entries); } private function buildMathString() : string { $entries = []; foreach ($this->main_node->attributes AS $attribute) { if ($attribute instanceof vB_CurlyNode) { $entries[] = '{vb:' . $attribute->value . '...}' ; } else { $entries[] = (string) $attribute; } } return '{vb:math ' . implode(' ' , $entries) . '}'; } } 