Added support for null coalescing operator

Fixes #882
This commit is contained in:
Simon Wisselink
2023-05-01 23:54:25 +02:00
parent 7fa8db3bc1
commit e99d2fae39
4 changed files with 900 additions and 810 deletions

View File

@@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for PHP8.2
- Added a new way to extend Smarty functionality using `Smarty::addExtension()`. Please see the docs for more information.
- Custom tags can accept positional parameters, so you can write a block compiler that support this: `{trans "Jack" "dull boy"}All work and no play makes %s a %s.{/trans}` [#164](https://github.com/smarty-php/smarty/issues/164)
- Full support for ternary operator: `{$test ? $a : $b}` and `{$var ?: $alternative}` [#881](https://github.com/smarty-php/smarty/issues/881)
- Full support for ternary operator: `{$test ? $a : $b}` and `{$var ?: $value_if_falsy}` [#881](https://github.com/smarty-php/smarty/issues/881)
- Full support for null coalescing operator: `{$var ?? $value_if_null}` [#882](https://github.com/smarty-php/smarty/issues/882)
### Changed
- All Smarty code is now in the \Smarty namespace. For simple use-cases, you only need to add

File diff suppressed because it is too large Load Diff

View File

@@ -617,6 +617,11 @@ expr(res) ::= value(v). {
res = v;
}
// nullcoalescing
expr(res) ::= nullcoalescing(v). {
res = v;
}
// ternary
expr(res) ::= ternary(v). {
res = v;
@@ -668,6 +673,10 @@ expr(res) ::= expr(e1) ISIN value(v). {
res = 'in_array('.e1.',(array)'.v.')';
}
// null coalescing
nullcoalescing(res) ::= expr(v) QMARK QMARK expr(e2). {
res = v.' ?? '.e2;
}
//
// ternary

View File

@@ -0,0 +1,34 @@
<?php
class NullCoalescingTest extends PHPUnit_Smarty {
public function setUp(): void
{
$this->setUpSmarty('/tmp');
$this->cleanDirs();
}
public function testUndefined() {
$tpl = $this->smarty->createTemplate('string:{$myvar ?? "undefined"}');
$this->assertEquals('undefined', $this->smarty->fetch($tpl));
}
/**
* @dataProvider dataForOther
*/
public function testOther($value, $expected) {
$tpl = $this->smarty->createTemplate('string:{$myvar ?? "undefined"}');
$tpl->assign('myvar', $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
public function dataForOther() {
return [
[null, 'undefined'],
['blah', ''],
['', ''],
[false, false],
];
}
}