mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add string concatenation (#3)
This commit is contained in:
parent
1e7cf29290
commit
09f952686b
@ -81,6 +81,10 @@ Operators
|
||||
* `--$a` - decrement the variable and use it
|
||||
* `$a--` - use the variable and decrement it
|
||||
|
||||
### String operator
|
||||
|
||||
* `$a ~ $b` - return concatenation of variables `$a` and `$b`
|
||||
|
||||
### Ternary operator
|
||||
|
||||
* `$a ? $b : $c` - returns `$b` if `$a` is not empty, and `$c` otherwise
|
||||
|
@ -615,36 +615,34 @@ class Template extends Render
|
||||
$exp = array();
|
||||
$var = false; // last term was: true - variable, false - mixed
|
||||
$op = false; // last exp was operator
|
||||
$cond = false; // was conditional operator
|
||||
$cond = false; // was comparison operator
|
||||
while ($tokens->valid()) {
|
||||
// parse term
|
||||
$term = $this->parseTerm($tokens, $var);
|
||||
$term = $this->parseTerm($tokens, $var); // term of the expression
|
||||
if ($term !== false) {
|
||||
$exp[] = $term;
|
||||
$op = false;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$tokens->valid()) {
|
||||
break;
|
||||
}
|
||||
|
||||
// parse operator
|
||||
if ($tokens->is(Tokenizer::MACRO_BINARY)) {
|
||||
if ($tokens->is(Tokenizer::MACRO_COND)) {
|
||||
if ($tokens->is(Tokenizer::MACRO_BINARY)) { // binary operator: $a + $b, $a <= $b, ...
|
||||
if ($tokens->is(Tokenizer::MACRO_COND)) { // comparison operator
|
||||
if ($cond) {
|
||||
break;
|
||||
}
|
||||
$cond = true;
|
||||
}
|
||||
$op = $tokens->getAndNext();
|
||||
} elseif ($tokens->is(Tokenizer::MACRO_EQUALS)) {
|
||||
} elseif ($tokens->is(Tokenizer::MACRO_EQUALS)) { // assignment operator: $a = 4, $a += 3, ...
|
||||
if (!$var) {
|
||||
break;
|
||||
}
|
||||
$op = $tokens->getAndNext();
|
||||
} elseif ($tokens->is(T_STRING)) {
|
||||
} elseif ($tokens->is(T_STRING)) { // test or containment operator: $a in $b, $a is set, ...
|
||||
if (!$exp) {
|
||||
break;
|
||||
}
|
||||
@ -658,8 +656,17 @@ class Template extends Render
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} elseif ($tokens->is('~')) {
|
||||
// string concat coming soon
|
||||
} elseif ($tokens->is('~')) { // string concatenation operator: 'asd' ~ $var
|
||||
$concat = array(array_pop($exp));
|
||||
while($tokens->is('~')) {
|
||||
$tokens->next();
|
||||
if($tokens->is(T_LNUMBER, T_DNUMBER)) {
|
||||
$concat[] = "strval(".$this->parseTerm($tokens).")";
|
||||
} else {
|
||||
$concat[] = $this->parseTerm($tokens);
|
||||
}
|
||||
}
|
||||
$exp[] = "(".implode(".", $concat).")";
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
@ -34,7 +34,9 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
"obj" => new \StdClass,
|
||||
"list" => array(
|
||||
"a" => 1,
|
||||
"b" => 2
|
||||
"one" => 1,
|
||||
"b" => 2,
|
||||
"two" => 2
|
||||
),
|
||||
0 => "empty value",
|
||||
1 => "one value",
|
||||
@ -204,7 +206,11 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function providerVariables()
|
||||
{
|
||||
return array();
|
||||
return array(
|
||||
array('$one', 1),
|
||||
array('$list.one', 1),
|
||||
array('$list[$$.DEVELOP]', 1),
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerObjects()
|
||||
|
@ -676,10 +676,20 @@ class TemplateTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public static function providerConcat() {
|
||||
return array(
|
||||
array('{"string" ~ $one ~ up("end")}', "string1END"),
|
||||
array('{"string" ~ $one++ ~ "end"}', "string1end"),
|
||||
array('{"string" ~ ++$one ~ "end"}', "string2end"),
|
||||
array('{"string" ~ "one" ~ "end"}', "stringoneend"),
|
||||
array('{"string" ~ 1 ~ "end"}', "string1end"),
|
||||
);
|
||||
}
|
||||
|
||||
public function _testSandbox()
|
||||
{
|
||||
try {
|
||||
var_dump($this->fenom->compileCode('{$a!"no way":"right"}')->getBody());
|
||||
var_dump($this->fenom->compileCode('{$a++~"hi"~time("Y:m:d")}')->getBody());
|
||||
} catch (\Exception $e) {
|
||||
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
|
||||
}
|
||||
@ -891,5 +901,13 @@ class TemplateTest extends TestCase
|
||||
{
|
||||
$this->exec($code, self::getVars(), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerConcat
|
||||
*/
|
||||
public function testConcat($code, $result)
|
||||
{
|
||||
$this->exec($code, self::getVars(), $result);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user