Skip to content

Commit

Permalink
fix argument getting
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed May 12, 2023
1 parent a9f5df4 commit 1b66357
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Parsem/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ final class Parser
* 3: filter:10,'arg','another' --> (filter with args)
* 4: filter --> (only filter name)
*/

public const PATTERN = '/<%\s?([a-zA-Z0-9_]+)(=.+?)?\|?(([a-zA-Z0-9_]+?)(?:\:(?:(?:\\?\'|\\?")?.?(?:\\?\'|\\?")?,?)+?)*?)?\s?%>/m';

public const LITERALLY_NULL = '__:-LITERALLY_NULL-:__';

/**
* Parses a string, replacing all template variables with the corresponding values passed in `$arguments`.
* @return mixed The parsed string or the original `$string` value if it's not string
Expand Down Expand Up @@ -191,8 +192,11 @@ public static function getArguments(string $string, ?string $pattern = null): ob
$defaults = [];

foreach ($matches[1] as $key => $match) {
if ($matches[3][$key]) {
$defaults = self::getDefaultValue($matches[2][$key], $defaults);
if ($matches[2][$key] !== '') {
$default = self::getDefaultValue($matches[2][$key]);
if ($default !== static::LITERALLY_NULL) {
$defaults[$key] = $default;
}
}
}

Expand Down Expand Up @@ -284,18 +288,22 @@ private static function removeDuplicates(array $array): array
* @param array $defaults
* @return array
*/
private static function getDefaultValue(string $defaultMatch, array $defaults): array
private static function getDefaultValue(string $defaultMatch): mixed
{
$default = trim($defaultMatch, '=') ?? null;

if (!$default) {
return static::LITERALLY_NULL;
}

if (is_numeric($default) && !preg_match('/([\'"`])/', $default)) {
$defaults[] = strpos($default, '.') === false ? (int)$default : (float)$default;
return strpos($default, '.') === false ? (int)$default : (float)$default;
} else if (in_array($default, ['false', 'true'])) {
$defaults[] = (bool)trim($default, '\'"`');
return (bool)trim($default, '\'"`');
} else if ($default === 'null') {
$defaults[] = null;
return null;
} else {
$defaults[] = (string)trim($default, '\'"`\\');
return (string)trim($default, '\'"`\\');
}
return $defaults;
}
}

0 comments on commit 1b66357

Please sign in to comment.