mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Done #83
This commit is contained in:
parent
2cc9ffdf62
commit
6b8ddd4ecc
@ -134,24 +134,23 @@ class Compiler
|
||||
*/
|
||||
public static function foreachOpen(Tokenizer $tokens, Tag $scope)
|
||||
{
|
||||
$p = array("index" => false, "first" => false, "last" => false);
|
||||
$key = null;
|
||||
$before = $body = array();
|
||||
$p = array("index" => false, "first" => false, "last" => false);
|
||||
$key = null;
|
||||
$before = $body = array();
|
||||
$prepend = "";
|
||||
if ($tokens->is(T_VARIABLE)) {
|
||||
$from = $scope->tpl->parseTerm($tokens, $is_var);
|
||||
if($is_var) {
|
||||
$check = '!empty('.$from.')';
|
||||
$prepend = "";
|
||||
} else {
|
||||
$scope["var"] = $scope->tpl->tmpVar();
|
||||
$prepend = $scope["var"].' = (array)('.$from.')';
|
||||
$from = $check = $scope["var"];
|
||||
}
|
||||
} elseif ($tokens->is('[')) {
|
||||
$from = $scope->tpl->parseArray($tokens);
|
||||
$scope["var"] = $scope->tpl->tmpVar();
|
||||
$prepend = $scope["var"].' = (array)('.$from.')';
|
||||
$from = $check = $scope["var"];
|
||||
$count = 0;
|
||||
$from = $scope->tpl->parseArray($tokens, $count);
|
||||
$check = $count;
|
||||
} else {
|
||||
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ class Template extends Render
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
* @return bool|\Fenom\Scope
|
||||
* @return bool|\Fenom\Tag
|
||||
*/
|
||||
public function getParentScope($tag)
|
||||
{
|
||||
@ -1281,35 +1281,28 @@ class Template extends Render
|
||||
public function parseArray(Tokenizer $tokens, &$count = 0)
|
||||
{
|
||||
if ($tokens->is("[")) {
|
||||
$_arr = "array(";
|
||||
$key = $val = false;
|
||||
$arr = array();
|
||||
$tokens->next();
|
||||
while ($tokens->valid()) {
|
||||
if ($tokens->is(',') && $val) {
|
||||
$key = true;
|
||||
$val = false;
|
||||
$_arr .= $tokens->getAndNext() . ' ';
|
||||
} elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(") && !$val) {
|
||||
$_arr .= $this->parseExpr($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is('"') && !$val) {
|
||||
$_arr .= $this->parseQuote($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is(T_DOUBLE_ARROW) && $val) {
|
||||
$_arr .= ' ' . $tokens->getAndNext() . ' ';
|
||||
$key = true;
|
||||
$val = false;
|
||||
} elseif (!$val && $tokens->is('[')) {
|
||||
$_arr .= $this->parseArray($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is(']') && (!$key || $tokens->prev[0] === ',')) {
|
||||
if ($tokens->is(']')) {
|
||||
$tokens->next();
|
||||
return $_arr . ')';
|
||||
return 'array(' . implode(', ', $arr) . ')';
|
||||
}
|
||||
if ($tokens->is('[')) {
|
||||
$arr[] = $this->parseArray($tokens);
|
||||
$count++;
|
||||
} else {
|
||||
break;
|
||||
$expr = $this->parseExpr($tokens);
|
||||
if($tokens->is(T_DOUBLE_ARROW)) {
|
||||
$tokens->next();
|
||||
$arr[] = $expr.' => '.$this->parseExpr($tokens);
|
||||
} else {
|
||||
$arr[] = $expr;
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
if($tokens->is(',')) {
|
||||
$tokens->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -572,6 +572,30 @@ class TemplateTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerArrays()
|
||||
{
|
||||
return array(
|
||||
array('{var $arr = []}', array()),
|
||||
array('{var $arr = [1]}', array(1)),
|
||||
array('{var $arr = [1,]}', array(1)),
|
||||
array('{var $arr = [1, 2, 3, 5]}', array(1, 2, 3, 5)),
|
||||
array('{var $arr = [1, true, false, null, -1, 1.1, -2.2, 5, "str"]}', array(1, true, false, null, -1, 1.1, -2.2, 5, "str")),
|
||||
array('{var $arr = [5 => 1, "two" => 2, 3]}', array(5 => 1, "two" => 2, 3)),
|
||||
array('{var $arr = [1 + 1, 2 * 2, 3 / 3 + 7,]}', array(1 + 1, 2 * 2, 3 / 3 + 7)),
|
||||
array('{var $arr = [$zero, $two => $one, $num.3 => $.const.PHP_VERSION]}', array(0, 2 => 1, "three" => PHP_VERSION)),
|
||||
array('{var $arr = [5 - 1 => 1, "two"|up => "two"|low, 3 => count([1,2])]}', array(4 => 1, "TWO" => "two", 3 => 2)),
|
||||
|
||||
array('{var $arr = [[1]]}', array(array(1))),
|
||||
array('{var $arr = [[],[]]}', array(array(),array())),
|
||||
array('{var $arr = [1, [2, 3], 5]}', array(1, array(2, 3), 5)),
|
||||
array('{var $arr = [1, [true, false, null, -1, 1.1, -2.2, 5], "str"]}', array(1, array(true, false, null, -1, 1.1, -2.2, 5), "str")),
|
||||
array('{var $arr = [5 => [1, "two" => 2], 3]}', array(5 => array(1, "two" => 2), 3)),
|
||||
array('{var $arr = [1 + 1, [2 * 2, 3 / 3 + 7,],]}', array(1 + 1, array(2 * 2, 3 / 3 + 7))),
|
||||
array('{var $arr = [$zero, [$two => $one, $num.3 => $.const.PHP_VERSION]]}', array(0, array(2 => 1, "three" => PHP_VERSION))),
|
||||
array('{var $arr = [5 - 1 => 1, ["two"|up => ("two"|low ~ "..."), 3 => count([1,2])]]}', array(4 => 1, array("TWO" => "two...", 3 => 2))),
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerTernary()
|
||||
{
|
||||
$a = array(
|
||||
@ -683,6 +707,7 @@ class TemplateTest extends TestCase
|
||||
),
|
||||
array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
|
||||
array('Foreach: {foreach [] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
|
||||
array('Foreach: {foreach $unexists as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
|
||||
array(
|
||||
'Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end',
|
||||
$a,
|
||||
@ -1276,14 +1301,14 @@ class TemplateTest extends TestCase
|
||||
try {
|
||||
var_dump(
|
||||
$this->fenom->compileCode(
|
||||
'{var $a = [3, 5,6]}'
|
||||
'{foreach $fff as $k}{/foreach}'
|
||||
)->getBody()
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
while ($e->getPrevious()) {
|
||||
$e = $e->getPrevious();
|
||||
print_r("\n\n" . $e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
print_r("\n\n" . $e->getMessage() . " in {$e->getFile()}:{$e->getLine()}\n" . $e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
exit;
|
||||
@ -1419,6 +1444,25 @@ class TemplateTest extends TestCase
|
||||
$this->execError($code, $exception, $message, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerArrays
|
||||
* @group arrays
|
||||
*/
|
||||
public function testArrays($code, $vars)
|
||||
{
|
||||
$v = $this->getVars();
|
||||
$v['vars'] = $vars;
|
||||
$this->exec($code.'{if $arr === $vars}equal{/if}', $v, 'equal');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerCreateVarInvalid
|
||||
*/
|
||||
// public function testCreateVarInvalid($code, $exception, $message, $options = 0)
|
||||
// {
|
||||
// $this->execError($code, $exception, $message, $options);
|
||||
// }
|
||||
|
||||
/**
|
||||
* @group ternary
|
||||
* @dataProvider providerTernary
|
||||
|
Loading…
Reference in New Issue
Block a user