-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using PHP functions as modifiers now triggers a deprecation notice (#814
) Fixes #813
- Loading branch information
Showing
20 changed files
with
302 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Smarty plugin | ||
* | ||
* @package Smarty | ||
* @subpackage PluginsModifier | ||
*/ | ||
/** | ||
* Smarty count modifier plugin | ||
* Type: modifier | ||
* Name: count | ||
* Purpose: counts all elements in an array or in a Countable object | ||
* Input: | ||
* - Countable|array: array or object to count | ||
* - mode: int defaults to 0 for normal count mode, if set to 1 counts recursive | ||
* | ||
* @param mixed $arrayOrObject input array/object | ||
* @param int $mode count mode | ||
* | ||
* @return int | ||
*/ | ||
function smarty_modifier_count($arrayOrObject, $mode = 0) | ||
{ | ||
/* | ||
* @see https://www.php.net/count | ||
* > Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, | ||
* > 1 would be returned, unless value was null, in which case 0 would be returned. | ||
*/ | ||
|
||
if ($arrayOrObject instanceof Countable || is_array($arrayOrObject)) { | ||
return count($arrayOrObject, (int) $mode); | ||
} elseif ($arrayOrObject === null) { | ||
return 0; | ||
} | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Smarty plugin | ||
* | ||
* @package Smarty | ||
* @subpackage PluginsModifierCompiler | ||
*/ | ||
/** | ||
* Smarty nl2br modifier plugin | ||
* Type: modifier | ||
* Name: nl2br | ||
* Purpose: insert HTML line breaks before all newlines in a string | ||
* | ||
* @link https://www.smarty.net/docs/en/language.modifier.nl2br.tpl nl2br (Smarty online manual) | ||
* | ||
* @param array $params parameters | ||
* | ||
* @return string with compiled code | ||
*/ | ||
function smarty_modifiercompiler_nl2br($params) { | ||
return 'nl2br((string) ' . $params[0] . ', (bool) ' . ($params[1] ?? true) . ')'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Smarty plugin | ||
* | ||
* @package Smarty | ||
* @subpackage PluginsModifierCompiler | ||
*/ | ||
/** | ||
* Smarty round modifier plugin | ||
* Type: modifier | ||
* Name: round | ||
* Purpose: Returns the rounded value of num to specified precision (number of digits after the decimal point) | ||
* | ||
* @link https://www.smarty.net/docs/en/language.modifier.round.tpl round (Smarty online manual) | ||
* | ||
* @param array $params parameters | ||
* | ||
* @return string with compiled code | ||
*/ | ||
function smarty_modifiercompiler_round($params) { | ||
return 'round((float) ' . $params[0] . ', (int) ' . ($params[1] ?? 0) . ', (int) ' . ($params[2] ?? PHP_ROUND_HALF_UP) . ')'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Smarty plugin | ||
* | ||
* @package Smarty | ||
* @subpackage PluginsModifierCompiler | ||
*/ | ||
/** | ||
* Smarty str_repeat modifier plugin | ||
* Type: modifier | ||
* Name: str_repeat | ||
* Purpose: returns string repeated times times | ||
* | ||
* @link https://www.smarty.net/docs/en/language.modifier.str_repeat.tpl str_repeat (Smarty online manual) | ||
* | ||
* @param array $params parameters | ||
* | ||
* @return string with compiled code | ||
*/ | ||
function smarty_modifiercompiler_str_repeat($params) { | ||
return 'str_repeat((string) ' . $params[0] . ', (int) ' . $params[1] . ')'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Smarty plugin | ||
* | ||
* @package Smarty | ||
* @subpackage PluginsModifierCompiler | ||
*/ | ||
/** | ||
* Smarty strlen modifier plugin | ||
* Type: modifier | ||
* Name: strlen | ||
* Purpose: return the length of the given string | ||
* | ||
* @link https://www.smarty.net/docs/en/language.modifier.strlen.tpl strlen (Smarty online manual) | ||
* | ||
* @param array $params parameters | ||
* | ||
* @return string with compiled code | ||
*/ | ||
function smarty_modifiercompiler_strlen($params) { | ||
return 'strlen((string) ' . $params[0] . ')'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierCountTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Smarty PHPunit tests of modifier | ||
*/ | ||
|
||
/** | ||
* class for modifier tests | ||
* | ||
* @runTestsInSeparateProcess | ||
* @preserveGlobalState disabled | ||
* @backupStaticAttributes enabled | ||
*/ | ||
class PluginModifierCountTest extends PHPUnit_Smarty | ||
{ | ||
public function setUp(): void | ||
{ | ||
$this->setUpSmarty(dirname(__FILE__)); | ||
} | ||
|
||
public function testArray() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:count:{$v|count}'); | ||
$tpl->assign("v", array(1, 2, 3)); | ||
$this->assertEquals("count:3", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
public function testEmptyArray() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:count:{$v|count}'); | ||
$tpl->assign("v", array()); | ||
$this->assertEquals("count:0", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
public function testNull() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:count:{$v|count}'); | ||
$tpl->assign("v", null); | ||
$this->assertEquals("count:0", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
public function testString() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:count:{$v|count}'); | ||
$tpl->assign("v", "string"); | ||
$this->assertEquals("count:1", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNl2brTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Smarty PHPunit tests of modifier | ||
*/ | ||
|
||
/** | ||
* class for modifier tests | ||
* | ||
* @runTestsInSeparateProcess | ||
* @preserveGlobalState disabled | ||
* @backupStaticAttributes enabled | ||
*/ | ||
class PluginModifierNl2brTest extends PHPUnit_Smarty | ||
{ | ||
public function setUp(): void | ||
{ | ||
$this->setUpSmarty(dirname(__FILE__)); | ||
} | ||
|
||
public function testDefault() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:{$v|nl2br}'); | ||
$tpl->assign("v", "Line1\nLine2"); | ||
$this->assertEquals("Line1<br />\nLine2", $this->smarty->fetch($tpl)); | ||
} | ||
|
||
public function testNoXHTML() | ||
{ | ||
$tpl = $this->smarty->createTemplate('string:{$v|nl2br:false}'); | ||
$tpl->assign("v", "Line1\nLine2"); | ||
$this->assertEquals("Line1<br>\nLine2", $this->smarty->fetch($tpl)); | ||
} | ||
} |
Oops, something went wrong.