Skip to content

Commit

Permalink
Merge pull request #3 from sroehrl/callback
Browse files Browse the repository at this point in the history
Custom closures now support deep (flat) arrays
  • Loading branch information
sroehrl authored Feb 5, 2020
2 parents e1bad22 + 5d5bde1 commit 2601538
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions TemplateFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 2601538

Please sign in to comment.