|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HttpSoft\Router\Exception; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
use function gettype; |
|
10
|
|
|
use function get_class; |
|
11
|
|
|
use function is_object; |
|
12
|
|
|
use function sprintf; |
|
13
|
|
|
|
|
14
|
|
|
class InvalidRouteParameterException extends InvalidArgumentException |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param mixed $method |
|
18
|
|
|
* @return self |
|
19
|
|
|
*/ |
|
20
|
9 |
|
public static function forMethods($method): self |
|
21
|
|
|
{ |
|
22
|
9 |
|
return self::forType($method, 'The request methods MUST be a string type, %s received.'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param mixed $token |
|
27
|
|
|
* @return self |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public static function forTokens($token): self |
|
30
|
|
|
{ |
|
31
|
9 |
|
return self::forType($token, 'Parameter token values MUST be null or non-empty string, %s received.'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param mixed $default |
|
36
|
|
|
* @return self |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public static function forDefaults($default): self |
|
39
|
|
|
{ |
|
40
|
5 |
|
return self::forType( |
|
41
|
5 |
|
$default, |
|
42
|
5 |
|
'The default parameter values MUST be a scalar type (string, integer, float, boolean), %s received.' |
|
43
|
5 |
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $parameter |
|
48
|
|
|
* @return self |
|
49
|
|
|
*/ |
|
50
|
12 |
|
public static function forNotNullOrScalar($parameter): self |
|
51
|
|
|
{ |
|
52
|
12 |
|
return self::forType( |
|
53
|
12 |
|
$parameter, |
|
54
|
12 |
|
'The parameter values MUST be a null or scalar type (string, integer, float, boolean), %s received.' |
|
55
|
12 |
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
8 |
|
public static function forNotPassed(string $name): self |
|
63
|
|
|
{ |
|
64
|
8 |
|
return new self(sprintf('The value of the required parameter "%s" is not passed or is null.', $name)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $name |
|
69
|
|
|
* @param string $value |
|
70
|
|
|
* @param string $pattern |
|
71
|
|
|
* @return self |
|
72
|
|
|
*/ |
|
73
|
11 |
|
public static function forNotMatched(string $name, string $value, string $pattern): self |
|
74
|
|
|
{ |
|
75
|
11 |
|
return new self(sprintf( |
|
76
|
11 |
|
'The value "%s" of the "%s" parameter does not match the regexp `%s`.', |
|
77
|
11 |
|
$value, |
|
78
|
11 |
|
$name, |
|
79
|
11 |
|
$pattern |
|
80
|
11 |
|
)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $host |
|
85
|
|
|
* @param string $pattern |
|
86
|
|
|
* @return self |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public static function forNotHostMatched(string $host, string $pattern): self |
|
89
|
|
|
{ |
|
90
|
2 |
|
return new self(sprintf( |
|
91
|
2 |
|
'The passed host "%s" of does not match the regexp `%s`.', |
|
92
|
2 |
|
$host, |
|
93
|
2 |
|
$pattern |
|
94
|
2 |
|
)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param mixed $parameter |
|
99
|
|
|
* @param string $message |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
35 |
|
private static function forType($parameter, string $message): self |
|
103
|
|
|
{ |
|
104
|
35 |
|
return new self(sprintf($message, (is_object($parameter) ? get_class($parameter) : gettype($parameter)))); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|