From 483331882c2e0be2bd405aef7078d56ea11c31c7 Mon Sep 17 00:00:00 2001 From: Matronator <5470780+matronator@users.noreply.github.com> Date: Sat, 25 May 2024 21:11:04 +0200 Subject: [PATCH] strict parsing --- src/Parsem/Parser.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Parsem/Parser.php b/src/Parsem/Parser.php index 03fb83c..029dff6 100644 --- a/src/Parsem/Parser.php +++ b/src/Parsem/Parser.php @@ -46,9 +46,10 @@ final class Parser * @return mixed The parsed string or the original `$string` value if it's not string * @param mixed $string String to parse. If not provided with a string, the function will return this value * @param array $arguments Array of arguments to find and replace while parsing `['key' => 'value']` + * @param bool $strict [optional] If set to `true`, the function will throw an exception if a variable is not found in the `$arguments` array. If set to `false` null will be used. * @param string|null $pattern [optional] You can provide custom regex with two matching groups (for the variable name and for the filter) to use custom template syntax instead of the default one `<% name|filter %>` */ - public static function parseString(mixed $string, array $arguments = [], ?string $pattern = null): mixed + public static function parseString(mixed $string, array $arguments = [], bool $strict = true, ?string $pattern = null): mixed { if (!is_string($string)) return $string; @@ -64,6 +65,10 @@ public static function parseString(mixed $string, array $arguments = [], ?string if ($default !== static::LITERALLY_NULL) { $args[] = $default; } else { + if ($strict) { + throw new RuntimeException("Variable '$match' not found in arguments."); + } + $args[] = null; } } @@ -211,7 +216,7 @@ public static function parseFileToString(string $filename, array $arguments = [] throw new RuntimeException("File '$filename' does not exist."); $contents = file_get_contents($filename); - return self::parseString($contents, $arguments, $pattern); + return self::parseString($contents, $arguments, false, $pattern); } /** @@ -430,7 +435,7 @@ private static function removeDuplicates(array $array): array /** * @param string $defaultMatch * @param array $defaults - * @return array + * @return mixed */ private static function getDefaultValue(string $defaultMatch): mixed { @@ -442,12 +447,14 @@ private static function getDefaultValue(string $defaultMatch): mixed if (is_numeric($default) && !preg_match('/([\'"`])/', $default)) { return strpos($default, '.') === false ? (int)$default : (float)$default; + } else if ((str_starts_with($default, '"') && str_ends_with($default, '"')) || (str_starts_with($default, "'") && str_ends_with($default, "'"))) { + return (string) trim($default, '\'"`'); } else if (in_array($default, ['false', 'true'])) { - return (bool)trim($default, '\'"`'); + return (bool) $default; } else if ($default === 'null') { return null; } else { - return (string)trim($default, '\'"`\\'); + return $default; } } }