1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import functions |
16
|
|
|
*/ |
17
|
|
|
use function is_array; |
18
|
|
|
use function is_int; |
19
|
|
|
use function is_string; |
20
|
|
|
use function is_subclass_of; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* InvalidArgumentException |
24
|
|
|
* |
25
|
|
|
* @since 2.9.0 |
26
|
|
|
*/ |
27
|
|
|
class InvalidArgumentException extends Exception |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets an invalid value |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
2 |
|
public function getInvalidValue() |
36
|
|
|
{ |
37
|
2 |
|
return $this->fromContext('invalidValue'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Throws the exception if the given operand isn't an array |
42
|
|
|
* |
43
|
|
|
* @param mixed $operand |
44
|
|
|
* @param string $message |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
* @throws static |
48
|
|
|
*/ |
49
|
52 |
|
public static function throwIfNotArray($operand, string $message) : void |
50
|
|
|
{ |
51
|
52 |
|
if (!is_array($operand)) { |
52
|
26 |
|
throw new static($message, [ |
53
|
26 |
|
'invalidValue' => $operand, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
26 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Throws the exception if the given operand isn't an integer |
60
|
|
|
* |
61
|
|
|
* @param mixed $operand |
62
|
|
|
* @param string $message |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
* @throws static |
66
|
|
|
*/ |
67
|
13 |
|
public static function throwIfNotInteger($operand, string $message) : void |
68
|
|
|
{ |
69
|
13 |
|
if (!is_int($operand)) { |
70
|
9 |
|
throw new static($message, [ |
71
|
9 |
|
'invalidValue' => $operand, |
72
|
|
|
]); |
73
|
|
|
} |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Throws the exception if the given operand isn't a string |
78
|
|
|
* |
79
|
|
|
* @param mixed $operand |
80
|
|
|
* @param string $message |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
* @throws static |
84
|
|
|
*/ |
85
|
20 |
|
public static function throwIfNotString($operand, string $message) : void |
86
|
|
|
{ |
87
|
20 |
|
if (!is_string($operand)) { |
88
|
16 |
|
throw new static($message, [ |
89
|
16 |
|
'invalidValue' => $operand, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
4 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Throws the exception if the given operand doesn't implement the given class |
96
|
|
|
* |
97
|
|
|
* @param mixed $operand |
98
|
|
|
* @param string $className |
99
|
|
|
* @param string $message |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
* @throws static |
103
|
|
|
*/ |
104
|
32 |
|
public static function throwIfNotImplemented($operand, string $className, string $message) : void |
105
|
|
|
{ |
106
|
32 |
|
if (!is_subclass_of($operand, $className)) { |
107
|
25 |
|
throw new static($message, [ |
108
|
25 |
|
'invalidValue' => $operand, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
8 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Throws the exception if the given operand is an empty |
115
|
|
|
* |
116
|
|
|
* @param mixed $operand |
117
|
|
|
* @param string $message |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
* @throws static |
121
|
|
|
*/ |
122
|
40 |
|
public static function throwIfEmpty($operand, string $message) : void |
123
|
|
|
{ |
124
|
40 |
|
if ('' === $operand || [] === $operand) { |
125
|
4 |
|
throw new static($message, [ |
126
|
4 |
|
'invalidValue' => $operand, |
127
|
|
|
]); |
128
|
|
|
} |
129
|
39 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Throws the exception if the given operand isn't an array or empty |
133
|
|
|
* |
134
|
|
|
* @param mixed $operand |
135
|
|
|
* @param string $message |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
* @throws static |
139
|
|
|
*/ |
140
|
102 |
|
public static function throwIfEmptyArray($operand, string $message) : void |
141
|
|
|
{ |
142
|
102 |
|
if ([] === $operand || !is_array($operand)) { |
143
|
13 |
|
throw new static($message, [ |
144
|
13 |
|
'invalidValue' => $operand, |
145
|
|
|
]); |
146
|
|
|
} |
147
|
89 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Throws the exception if the given operand isn't a string or empty |
151
|
|
|
* |
152
|
|
|
* @param mixed $operand |
153
|
|
|
* @param string $message |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
* @throws static |
157
|
|
|
*/ |
158
|
174 |
|
public static function throwIfEmptyString($operand, string $message) : void |
159
|
|
|
{ |
160
|
174 |
|
if ('' === $operand || !is_string($operand)) { |
161
|
73 |
|
throw new static($message, [ |
162
|
73 |
|
'invalidValue' => $operand, |
163
|
|
|
]); |
164
|
|
|
} |
165
|
151 |
|
} |
166
|
|
|
} |
167
|
|
|
|