1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
class URL{
public static function getInt(string $name, ?int $default = null): ?int
{
global $router, $match;
if(!isset($_GET[$name])) return $default;
if($_GET[$name] === '0') return 0;
if(!filter_var($_GET[$name], FILTER_VALIDATE_INT)) {
if(isset($match['params']) && $match['params'] != null){
if(isset($match['params']['slug']) && $match['params']['slug'] != null){
header('Location:' . $router->generate($match['name'], ['slug' => $match['params']['slug'], 'id' => $match['params']['id']]));
}else{
header('Location:' . $router->generate($match['name'], ['id' => $match['params']['id']]));
}
}else{
header('Location:' . $router->generate($match['name']));
}
setFlash("Le paramètre $name dans l'url n'est pas un entier",'orange');
http_response_code(301);
exit();
}
return (int)$_GET[$name];
}
public static function getPositiveInt(string $name, ?int $default = null): ?int
{
global $router,$match;
$param = self::getInt($name, $default);
if($param !== null && $param <= 0){
if(isset($match['params']) && $match['params'] != null){
if(isset($match['params']['slug']) && $match['params']['slug'] != null){
header('Location:' . $router->generate($match['name'], ['slug' => $match['params']['slug'], 'id' => $match['params']['id']]));
}else{
header('Location:' . $router->generate($match['name'], ['id' => $match['params']['id']]));
}
}else{
header('Location:' . $router->generate($match['name']));
}
setFlash("Le paramètre $name dans l'url n'est pas un entier positif",'orange');
http_response_code(301);
exit();
}
return $param;
}
} |
Partager