diff --git a/src/Fenom.php b/src/Fenom.php index 2d6413d..4334b33 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -742,12 +742,12 @@ class Fenom { * @throws \RuntimeException if key from custom assoc doesn't exists into possible values */ private static function _makeMask(array $values, array $options, $mask = 0) { - foreach($values as $value) { - if(isset($options[$value])) { - if($options[$value]) { - $mask |= $options[$value]; + foreach ($values as $key=>$value) { + if (isset($options[$key])) { + if ($options[$key]) { + $mask |= $options[$key]; } else { - $mask &= ~$options[$value]; + $mask &= ~$options[$key]; } } else { throw new \RuntimeException("Undefined parameter $value"); diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index f6d0b08..d125856 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -615,7 +615,7 @@ class Compiler { } elseif(isset($params[ $param->getPosition() ])) { $args[] = $params[ $param->getPosition() ]; } elseif($param->isOptional()) { - $args[] = $param->getDefaultValue(); + $args[] = var_export($param->getDefaultValue(), true); } } return "$function(".implode(", ", $args).')'; diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 46f51e8..0e7e04d 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -62,7 +62,6 @@ class Template extends Render { * Escape outputs value * @var bool */ -// public $escape = false; public $escape = false; public $_extends; public $_extended = false; @@ -648,12 +647,12 @@ class Template extends Render { throw new TokenizeException("Unexpected token ".$tokens->getNext().", isset() and empty() accept only variables"); } $term = true; - } elseif(!$term && $tokens->is(Tokenizer::MACRO_UNARY)) { // like unary operator, see Tokenizer::MACRO_UNARY - if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY)) { + } elseif(!$term && $tokens->is(Tokenizer::MACRO_UNARY)) { + if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY, '(')) { break; } $_exp[] = $tokens->getAndNext(); - $term = false; + $term = 0; } elseif($tokens->is(Tokenizer::MACRO_BINARY)) { // like binary operator, see Tokenizer::MACRO_BINARY if(!$term) { throw new UnexpectedTokenException($tokens); @@ -671,6 +670,8 @@ class Template extends Render { } $_exp[] = " ".$tokens->getAndNext()." "; $term = 0; + } elseif($tokens->is('[')) { + $_exp[] = $this->parseArray($tokens); } else { break; } @@ -1284,7 +1285,7 @@ class Template extends Render { } if($tokens->is("=")) { $tokens->next(); - $params[ $key ] = $this->parseExp($tokens); + $params[ $key ] = $this->parseExp($tokens); } else { $params[ $key ] = 'true'; } diff --git a/tests/cases/Fenom/FunctionsTest.php b/tests/cases/Fenom/FunctionsTest.php new file mode 100644 index 0000000..755a1e0 --- /dev/null +++ b/tests/cases/Fenom/FunctionsTest.php @@ -0,0 +1,70 @@ +fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum'); + $this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow'); + $this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc'); + + $this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}'); + $this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}'); + $this->tpl('function_default_param_scalar.tpl', '{pow a=2}'); + $this->tpl('function_default_param_empty_array.tpl', '{sum}'); + $this->tpl('function_default_param_const.tpl', '{inc a=1}'); + $this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}'); + $this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}'); + } + + public function testFunctionWithParams() { + $output = $this->fenom->fetch('function_params_scalar.tpl'); + $this->assertEquals('8', $output); + } + + public function testFunctionWithDynamicParams() { + $output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4)); + $this->assertEquals('81', $output); + } + + public function testFunctionWithDefaultParamScalar() { + $output = $this->fenom->fetch('function_default_param_scalar.tpl'); + $this->assertEquals('4', $output); + } + + public function testFunctionWithDefaultParamArray() { + $output = $this->fenom->fetch('function_default_param_empty_array.tpl'); + $this->assertEquals('0', $output); + } + + public function testFunctionWithDefaultParamConst() { + $output = $this->fenom->fetch('function_default_param_const.tpl'); + $this->assertEquals('2', $output); + } + + public function testFunctionWithArrayNamedParam() { + $output = $this->fenom->fetch('function_array_param.tpl'); + $this->assertEquals('15', $output); + } + + public function testFunctionWithArrayPositionalParam() { + $output = $this->fenom->fetch('function_array_param_pos.tpl'); + $this->assertEquals('15', $output); + } + +} diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index e61e935..7ae60e0 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -157,8 +157,9 @@ class TemplateTest extends TestCase { array('Exp: {$y-$x} result', $b, 'Exp: 18 result'), array('Exp: {$y*$x} result', $b, 'Exp: 243 result'), array('Exp: {$y^$x} result', $b, 'Exp: 18 result'), - array('Exp: {-$x} result', $b, 'Exp: -9 result'), + array('Exp: {-($x)} result', $b, 'Exp: -9 result'), array('Exp: {!$x} result', $b, 'Exp: result'), + array('Exp: {!($x)} result', $b, 'Exp: result'), array('Exp: {!5} result', $b, 'Exp: result'), array('Exp: {-1} result', $b, 'Exp: -1 result'), array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'),