Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Last active November 15, 2021 04:38
Show Gist options
  • Save DarkGhostHunter/edcfa999ab358640fa4eba52fc5764ba to your computer and use it in GitHub Desktop.
Save DarkGhostHunter/edcfa999ab358640fa4eba52fc5764ba to your computer and use it in GitHub Desktop.
Normalizes a parameter from Laravel's middleware declaration.
<?php
namespace YOURNAMESPACE;
use function is_numeric;
use function strtolower;
trait NormalizesMiddlewareParameter
{
/**
* Normalizes a parameter from the middleware declaration.
*
* @param string $parameter
* @return bool|int|float|string|null
*/
protected function normalize(string $parameter): bool|int|float|string|null
{
if (is_numeric($parameter)) {
$parameter = (float) $parameter;
return $parameter == (int) $parameter ? $parameter : (int) $parameter;
}
return match (strtolower($parameter)) {
'null', '' => null,
'false' => false,
'true' => true,
default => $parameter,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment