mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-03 18:04:26 +02:00
Added strlen function, fixing some unit tests
This commit is contained in:
@@ -86,6 +86,7 @@ class DefaultExtension extends Base {
|
|||||||
case 'isset': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\IssetHandler(); break;
|
case 'isset': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\IssetHandler(); break;
|
||||||
case 'mailto': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Mailto(); break;
|
case 'mailto': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Mailto(); break;
|
||||||
case 'math': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Math(); break;
|
case 'math': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Math(); break;
|
||||||
|
case 'strlen': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Strlen(); break;
|
||||||
case 'time': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Time(); break;
|
case 'time': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Time(); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,7 +20,7 @@ class Count extends Base {
|
|||||||
$params = array_values($params ?? []);
|
$params = array_values($params ?? []);
|
||||||
|
|
||||||
if (count($params) < 1 || count($params) > 2) {
|
if (count($params) < 1 || count($params) > 2) {
|
||||||
throw new Exception("Invalid number of arguments for in_array. in_arrays expects 2 or 3 parameters.");
|
throw new Exception("Invalid number of arguments for count. count expects 2 or 3 parameters.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $params[0];
|
$value = $params[0];
|
||||||
|
28
src/FunctionHandler/Strlen.php
Normal file
28
src/FunctionHandler/Strlen.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Smarty\FunctionHandler;
|
||||||
|
|
||||||
|
use Smarty\Exception;
|
||||||
|
use Smarty\Template;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get string length
|
||||||
|
*
|
||||||
|
* strlen(string $string): int
|
||||||
|
*
|
||||||
|
* Returns length of the string on success, and 0 if the string is empty.
|
||||||
|
*/
|
||||||
|
class Strlen extends Base {
|
||||||
|
|
||||||
|
public function handle($params, Template $template) {
|
||||||
|
|
||||||
|
$params = array_values($params ?? []);
|
||||||
|
|
||||||
|
if (count($params) !== 1) {
|
||||||
|
throw new Exception("Invalid number of arguments for strlen. strlen expects exactly 1 parameter.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return strlen((string) $params[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user