Last active
November 15, 2021 04:38
-
-
Save DarkGhostHunter/edcfa999ab358640fa4eba52fc5764ba to your computer and use it in GitHub Desktop.
Normalizes a parameter from Laravel's middleware declaration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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