From 5d5bde1a4fea69736910e7a3a830cd79000488ab Mon Sep 17 00:00:00 2001 From: neoan Date: Tue, 4 Feb 2020 19:51:11 -0500 Subject: [PATCH] Custom closures now support deep (flat) arrays --- TemplateFunctions.php | 23 +++++++++++++++++------ composer.json | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/TemplateFunctions.php b/TemplateFunctions.php index 2a10e51..ab6f477 100644 --- a/TemplateFunctions.php +++ b/TemplateFunctions.php @@ -35,27 +35,38 @@ static function tryClosures($substitutions, $content, $executePure = true) static function executeClosure($content, $callBackName, $closure, $valueArray, $pure = true) { $pattern = self::retrieveClosurePattern($pure, $callBackName); - return preg_replace_callback( + $replacement = preg_replace_callback( $pattern, function ($hit) use ($closure, $valueArray) { - if (isset($valueArray[$hit[1]])) { - return $closure($valueArray[$hit[1]]); + $params = explode(',', $hit[1]); + $finalsInputs = []; + $found = true; + foreach ($params as $param){ + if(!isset($valueArray[trim($param)])){ + $found = false; + } else { + $finalsInputs[] = $valueArray[trim($param)]; + } + } + if ($found) { + return $closure(...$finalsInputs); } return $hit[0]; }, $content ); + return $replacement; } private static function retrieveClosurePattern($pure, $closureName) { $pattern = '/'; if (!$pure) { - $pattern .= addslashes(self::$registeredDelimiters[0]) . "\s*"; + $pattern .= preg_quote(self::$registeredDelimiters[0]) . "\s*"; } - $pattern .= "$closureName\(([a-z0-9,\s]+)\)"; + $pattern .= "$closureName\(([a-z0-9,\.\s]+)\)"; if (!$pure) { - $pattern .= "\s*" . addslashes(self::$registeredDelimiters[1]); + $pattern .= "\s*" . preg_quote(self::$registeredDelimiters[1]); } return $pattern . "/i"; } diff --git a/composer.json b/composer.json index bb6e669..2d0c835 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "neoan3-apps/template", "description": "neoan3 minimal template engine", - "version": "1.0.2", + "version": "1.0.3", "license": "MIT", "autoload": { "psr-4": {