Skip to content

Commit

Permalink
strict parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed May 25, 2024
1 parent 0c7647c commit 4833318
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Parsem/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
{
Expand All @@ -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;
}
}
}

0 comments on commit 4833318

Please sign in to comment.