From d216b3147bc3f37cf2337acab5767c6a4f74aa2e Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 31 Oct 2016 21:58:33 +0100 Subject: * PHPTAL library --- lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php | 93 +++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php (limited to 'lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php') diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php new file mode 100644 index 0000000..d86b94b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php @@ -0,0 +1,93 @@ + + * @author Kornel LesiƄski + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= expression + * + * Example: + * + *

(c) 2000

+ * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg + */ +class PHPTAL_Php_Attribute_TAL_Condition +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + private $expressions = array(); + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $code = $codewriter->evaluateExpression($this->expression); + + // If it's a chained expression build a new code path + if (is_array($code)) { + $this->expressions = array(); + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $code, $this); + return; + } + + // Force a falsy condition if the nothing keyword is active + if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + $code = 'false'; + } + + $codewriter->doIf('phptal_true(' . $code . ')'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('if'); + } + + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + // check if the expression is empty + if ($exp !== 'false') { + $this->expressions[] = '!phptal_isempty(' . $exp . ')'; + } + + if ($islast) { + // for the last one in the chain build a ORed condition + $executor->getCodeWriter()->doIf( implode(' || ', $this->expressions ) ); + // The executor will always end an if so we output a dummy if + $executor->doIf('false'); + } + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + // end the chain + $this->talesChainPart($executor, 'false', true); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + throw new PHPTAL_ParserException('\'default\' keyword not allowed on conditional expressions', + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + +} + -- cgit v1.2.3