This commit is contained in:
bzick 2014-06-28 22:15:30 +04:00
parent 6b8ddd4ecc
commit c56623c1a4
3 changed files with 38 additions and 2 deletions

View File

@ -282,6 +282,10 @@ class Fenom
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ignoreOpen',
'close' => 'Fenom\Compiler::nope'
),
'unset' => array(
'type' => self::INLINE_COMPILER,
'parser' => 'Fenom\Compiler::tagUnset'
)
);

View File

@ -1007,4 +1007,19 @@ class Compiler
{
$tag->tpl->ignore('ignore');
}
/**
* Tag {unset ...}
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
*/
public static function tagUnset(Tokenizer $tokens, Tag $tag)
{
$unset = [];
while($tokens->valid()) {
$unset[] = $tag->tpl->parseVariable($tokens);
}
return 'unset('.implode(", ", $unset).')';
}
}

View File

@ -596,6 +596,14 @@ class TemplateTest extends TestCase
);
}
public static function providerUnset() {
return array(
array('{var $a = 5} {unset $a} {if $a is not set}not set{/if}', 'not set'),
array('{var $a = ["b" => 5, "c" => 6]} {unset $a.b} {if $a.b is not set}not set{/if} but c is {$a.c}', 'not set but c is 6'),
array('{var $a = ["b" => 5, "c" => 6]} {unset $a.b $a.c} {if $a.b is not set}not set{/if} {if $a.c is not set}not set{/if}', 'not set not set'),
);
}
public static function providerTernary()
{
$a = array(
@ -1296,12 +1304,12 @@ class TemplateTest extends TestCase
/**
* @group sb
*/
public function testSandbox()
public function _testSandbox()
{
try {
var_dump(
$this->fenom->compileCode(
'{foreach $fff as $k}{/foreach}'
'{unset $a $a.c $b}'
)->getBody()
);
} catch (\Exception $e) {
@ -1455,6 +1463,15 @@ class TemplateTest extends TestCase
$this->exec($code.'{if $arr === $vars}equal{/if}', $v, 'equal');
}
/**
* @dataProvider providerUnset
* @group unset
*/
public function testUnset($code, $result)
{
$this->exec($code, $this->getVars(), $result);
}
/**
* @dataProvider providerCreateVarInvalid
*/