Added strlen function, fixing some unit tests

This commit is contained in:
Simon Wisselink
2023-01-09 11:54:13 +01:00
parent e124f71dd8
commit 9016dd9908
3 changed files with 30 additions and 1 deletions

View File

@@ -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;
} }

View File

@@ -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];

View 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]);
}
}