summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--libs/plugins/modifier.implode.php15
-rw-r--r--libs/plugins/modifiercompiler.json_encode.php11
-rw-r--r--libs/plugins/modifiercompiler.substr.php12
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php44
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php76
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php48
7 files changed, 209 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c54f7872..acd4981b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Changed
+- Using the `|implode`, `|json_encode` and `|substr` modifiers does not generate a deprecation warning anymore as they will continue to be supported in v5 [#939](https://github.com/smarty-php/smarty/issues/939)
+
### Added
- PHP8.3 support [#925](https://github.com/smarty-php/smarty/issues/925)
diff --git a/libs/plugins/modifier.implode.php b/libs/plugins/modifier.implode.php
new file mode 100644
index 00000000..679d71d7
--- /dev/null
+++ b/libs/plugins/modifier.implode.php
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Smarty plugin
+ *
+ * @package Smarty
+ * @subpackage PluginsModifier
+ */
+
+function smarty_modifier_implode($values, $separator = '')
+{
+ if (is_array($separator)) {
+ return implode((string) ($values ?? ''), (array) $separator);
+ }
+ return implode((string) ($separator ?? ''), (array) $values);
+}
diff --git a/libs/plugins/modifiercompiler.json_encode.php b/libs/plugins/modifiercompiler.json_encode.php
new file mode 100644
index 00000000..629277f2
--- /dev/null
+++ b/libs/plugins/modifiercompiler.json_encode.php
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * Smarty plugin
+ *
+ * @package Smarty
+ * @subpackage PluginsModifierCompiler
+ */
+function smarty_modifiercompiler_json_encode($params) {
+ return 'json_encode(' . $params[0] . (isset($params[1]) ? ', (int) ' . $params[1] : '') . ')';
+}
diff --git a/libs/plugins/modifiercompiler.substr.php b/libs/plugins/modifiercompiler.substr.php
new file mode 100644
index 00000000..17721f52
--- /dev/null
+++ b/libs/plugins/modifiercompiler.substr.php
@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * Smarty plugin
+ *
+ * @package Smarty
+ * @subpackage PluginsModifierCompiler
+ */
+function smarty_modifiercompiler_substr($params) {
+ return 'substr((string) ' . $params[0] . ', (int) ' . $params[1] .
+ (isset($params[2]) ? ', (int) ' . $params[2] : '') . ')';
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php
new file mode 100644
index 00000000..5b816aaf
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Smarty PHPunit tests of modifier
+ */
+
+/**
+ * class for modifier tests
+ *
+ * @runTestsInSeparateProcess
+ * @preserveGlobalState disabled
+ * @backupStaticAttributes enabled
+ */
+class PluginModifierImplodeTest extends PHPUnit_Smarty
+{
+ public function setUp(): void
+ {
+ $this->setUpSmarty(__DIR__);
+ }
+
+ public function testDefault()
+ {
+ $tpl = $this->smarty->createTemplate('string:{""|implode:$v}');
+ $tpl->assign("v", ["1", "2"]);
+ $this->assertEquals("12", $this->smarty->fetch($tpl));
+ }
+ public function testWithSeparator()
+ {
+ $tpl = $this->smarty->createTemplate('string:{","|implode:$v}');
+ $tpl->assign("v", ["a", "b"]);
+ $this->assertEquals("a,b", $this->smarty->fetch($tpl));
+ }
+ public function testInConditional()
+ {
+ $tpl = $this->smarty->createTemplate('string:{if implode("", $v) == "abc"}good{else}bad{/if}');
+ $tpl->assign("v", ['a','b','c']);
+ $this->assertEquals("good", $this->smarty->fetch($tpl));
+ }
+ public function testInConditionalWithSeparator()
+ {
+ $tpl = $this->smarty->createTemplate('string:{if implode("-", $v) == "a-b-c"}good{else}bad{/if}');
+ $tpl->assign("v", ['a','b','c']);
+ $this->assertEquals("good", $this->smarty->fetch($tpl));
+ }
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php
new file mode 100644
index 00000000..965c8a6d
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * Smarty PHPunit tests of modifier
+ */
+
+/**
+ * class for modifier tests
+ *
+ * @runTestsInSeparateProcess
+ * @preserveGlobalState disabled
+ * @backupStaticAttributes enabled
+ */
+class PluginModifierJsonEncodeTest extends PHPUnit_Smarty
+{
+ public function setUp(): void
+ {
+ $this->setUpSmarty(__DIR__);
+ }
+
+ /**
+ * @dataProvider dataForDefault
+ */
+ public function testDefault($value, $expected)
+ {
+ $tpl = $this->smarty->createTemplate('string:{$v|json_encode}');
+ $tpl->assign("v", $value);
+ $this->assertEquals($expected, $this->smarty->fetch($tpl));
+ }
+
+ /**
+ * @dataProvider dataForDefault
+ */
+ public function testDefaultAsFunction($value, $expected)
+ {
+ $tpl = $this->smarty->createTemplate('string:{json_encode($v)}');
+ $tpl->assign("v", $value);
+ $this->assertEquals($expected, $this->smarty->fetch($tpl));
+ }
+
+ public function dataForDefault() {
+ return [
+ ["abc", '"abc"'],
+ [["abc"], '["abc"]'],
+ [["abc",["a"=>2]], '["abc",{"a":2}]'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataForForceObject
+ */
+ public function testForceObject($value, $expected)
+ {
+ $tpl = $this->smarty->createTemplate('string:{$v|json_encode:16}');
+ $tpl->assign("v", $value);
+ $this->assertEquals($expected, $this->smarty->fetch($tpl));
+ }
+
+ /**
+ * @dataProvider dataForForceObject
+ */
+ public function testForceObjectAsFunction($value, $expected)
+ {
+ $tpl = $this->smarty->createTemplate('string:{json_encode($v,16)}');
+ $tpl->assign("v", $value);
+ $this->assertEquals($expected, $this->smarty->fetch($tpl));
+ }
+
+ public function dataForForceObject() {
+ return [
+ ["abc", '"abc"'],
+ [["abc"], '{"0":"abc"}'],
+ [["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'],
+ ];
+ }
+
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php
new file mode 100644
index 00000000..42abf869
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Smarty PHPunit tests of modifier
+ */
+
+/**
+ * class for modifier tests
+ *
+ * @runTestsInSeparateProcess
+ * @preserveGlobalState disabled
+ * @backupStaticAttributes enabled
+ */
+class PluginModifierSubstrTest extends PHPUnit_Smarty
+{
+ public function setUp(): void
+ {
+ $this->setUpSmarty(__DIR__);
+ }
+
+ public function testDefault()
+ {
+ $tpl = $this->smarty->createTemplate('string:{$v|substr:1}');
+ $tpl->assign("v", "abc");
+ $this->assertEquals("bc", $this->smarty->fetch($tpl));
+ }
+
+ public function testTwoArguments()
+ {
+ $tpl = $this->smarty->createTemplate('string:{$v|substr:1:1}');
+ $tpl->assign("v", "abc");
+ $this->assertEquals("b", $this->smarty->fetch($tpl));
+ }
+
+ public function testNegativeOffset()
+ {
+ $tpl = $this->smarty->createTemplate('string:{$v|substr:-1}');
+ $tpl->assign("v", "abc");
+ $this->assertEquals("c", $this->smarty->fetch($tpl));
+ }
+
+ public function testInConditional()
+ {
+ $tpl = $this->smarty->createTemplate('string:{if substr($v, -1) == "c"}good{else}bad{/if}');
+ $tpl->assign("v", "abc");
+ $this->assertEquals("good", $this->smarty->fetch($tpl));
+ }
+
+}