/** * Compiles an {if} tag and returns the compiled PHP code. * * @param string $tagArgs * @param boolean $elseif true, if this tag is an else tag * @return string */ protected function compileIfTag($tagArgs, $elseif = false) { $tagArgs = $this->replaceQuotes($tagArgs); $tagArgs = str_replace(' ', '', $tagArgs); // split tags preg_match_all('~('.$this->conditionOperatorPattern.')~', $tagArgs, $matches); $operators = $matches[1]; $values = preg_split('~(?:'.$this->conditionOperatorPattern.')~', $tagArgs); $leftParentheses = 0; $result = ''; for ($i = 0, $j = count($values); $i < $j; $i++) { $operator = (isset($operators[$i]) ? $operators[$i] : null); if ($operator !== '!' && $values[$i] == '') { throw new SystemException($this->formatSyntaxError('syntax error in tag {'.($elseif ? 'elseif' : 'if').'}', $this->currentIdentifier, $this->currentLineNo)); } $leftParenthesis = StringUtil::countSubstring($values[$i], '('); $rightParenthesis = StringUtil::countSubstring($values[$i], ')'); if ($leftParenthesis > $rightParenthesis) { $leftParentheses += $leftParenthesis - $rightParenthesis; $value = StringUtil::substring($values[$i], $leftParenthesis - $rightParenthesis); $result .= str_repeat('(', $leftParenthesis - $rightParenthesis); if (str_replace('(', '', StringUtil::substring($values[$i], 0, $leftParenthesis - $rightParenthesis)) != '') { throw new SystemException($this->formatSyntaxError('syntax error in tag {'.($elseif ? 'elseif' : 'if').'}', $this->currentIdentifier, $this->currentLineNo)); } } else if ($leftParenthesis < $rightParenthesis) { $leftParentheses += $leftParenthesis - $rightParenthesis; $value = StringUtil::substring($values[$i], 0, $leftParenthesis - $rightParenthesis); if ($leftParentheses < 0 || str_replace(')', '', StringUtil::substring($values[$i], $leftParenthesis - $rightParenthesis)) != '') { throw new SystemException($this->formatSyntaxError('syntax error in tag {'.($elseif ? 'elseif' : 'if').'}', $this->currentIdentifier, $this->currentLineNo)); } } else $value = $values[$i]; try { $result .= $this->compileVariableTag($value, false); } catch (SystemException $e) { throw new SystemException($this->formatSyntaxError('syntax error in tag {'.($elseif ? 'elseif' : 'if').'}', $this->currentIdentifier, $this->currentLineNo), 0, nl2br($e)); } if ($leftParenthesis < $rightParenthesis) { $result .= str_repeat(')', $rightParenthesis - $leftParenthesis); } if ($operator) $result .= ' '.$operator.' '; } return '<?php '.($elseif ? '} elseif' : 'if').' ('.$result.') { ?>'; }