diff options
| author | Simon Wisselink <wisskid@users.noreply.github.com> | 2022-09-23 00:09:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-23 00:09:00 +0200 |
| commit | 4550fc03391bd120b4add41ad7934c4c59aa21e8 (patch) | |
| tree | e4c74de17d9033236183daabbf83da0808a7f0de /libs/plugins | |
| parent | 4fc39d59a5d7b3cb3d49b5170eba43ceafcf6074 (diff) | |
| download | smarty-4550fc03391bd120b4add41ad7934c4c59aa21e8.tar.gz smarty-4550fc03391bd120b4add41ad7934c4c59aa21e8.tar.bz2 smarty-4550fc03391bd120b4add41ad7934c4c59aa21e8.zip | |
Using PHP functions as modifiers now triggers a deprecation notice (#814)
Fixes #813
Diffstat (limited to 'libs/plugins')
| -rw-r--r-- | libs/plugins/modifier.count.php | 36 | ||||
| -rw-r--r-- | libs/plugins/modifiercompiler.nl2br.php | 23 | ||||
| -rw-r--r-- | libs/plugins/modifiercompiler.round.php | 23 | ||||
| -rw-r--r-- | libs/plugins/modifiercompiler.str_repeat.php | 23 | ||||
| -rw-r--r-- | libs/plugins/modifiercompiler.strlen.php | 23 |
5 files changed, 128 insertions, 0 deletions
diff --git a/libs/plugins/modifier.count.php b/libs/plugins/modifier.count.php new file mode 100644 index 00000000..ca35fc11 --- /dev/null +++ b/libs/plugins/modifier.count.php @@ -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; +} diff --git a/libs/plugins/modifiercompiler.nl2br.php b/libs/plugins/modifiercompiler.nl2br.php new file mode 100644 index 00000000..308c00e4 --- /dev/null +++ b/libs/plugins/modifiercompiler.nl2br.php @@ -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) . ')'; +} diff --git a/libs/plugins/modifiercompiler.round.php b/libs/plugins/modifiercompiler.round.php new file mode 100644 index 00000000..97f072f0 --- /dev/null +++ b/libs/plugins/modifiercompiler.round.php @@ -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) . ')'; +} diff --git a/libs/plugins/modifiercompiler.str_repeat.php b/libs/plugins/modifiercompiler.str_repeat.php new file mode 100644 index 00000000..18eb7a76 --- /dev/null +++ b/libs/plugins/modifiercompiler.str_repeat.php @@ -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] . ')'; +} diff --git a/libs/plugins/modifiercompiler.strlen.php b/libs/plugins/modifiercompiler.strlen.php new file mode 100644 index 00000000..d43e8ef1 --- /dev/null +++ b/libs/plugins/modifiercompiler.strlen.php @@ -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] . ')'; +} |
