1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\Schemator\Exceptions; |
4
|
|
|
|
5
|
|
|
use Smoren\Schemator\Interfaces\FilterContextInterface; |
6
|
|
|
use Throwable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SchematorException. |
10
|
|
|
*/ |
11
|
|
|
class SchematorException extends \Exception |
12
|
|
|
{ |
13
|
|
|
public const UNSUPPORTED_SOURCE_TYPE = 1; |
14
|
|
|
public const UNSUPPORTED_KEY_TYPE = 2; |
15
|
|
|
public const UNSUPPORTED_FILTER_CONFIG_TYPE = 3; |
16
|
|
|
public const FILTER_NOT_FOUND = 4; |
17
|
|
|
public const FILTER_ERROR = 5; |
18
|
|
|
public const CANNOT_GET_VALUE = 6; |
19
|
|
|
public const BAD_FILTER_CONFIG = 7; |
20
|
|
|
public const BAD_FILTER_SOURCE = 8; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array<mixed>|null |
24
|
|
|
*/ |
25
|
|
|
protected ?array $data; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $message |
29
|
|
|
* @param int $code |
30
|
|
|
* @param Throwable|null $previous |
31
|
|
|
* @param array<mixed>|null $data |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
string $message, |
35
|
|
|
int $code, |
36
|
|
|
Throwable $previous = null, |
37
|
|
|
?array $data = null |
38
|
|
|
) { |
39
|
|
|
parent::__construct($message, $code, $previous); |
40
|
|
|
$this->data = $data; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array<mixed> |
45
|
|
|
*/ |
46
|
|
|
public function getData(): array |
47
|
|
|
{ |
48
|
|
|
return $this->data ?? []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates a new exception instance for filter not found error. |
53
|
|
|
* |
54
|
|
|
* @param string $filterName name of filter. |
55
|
|
|
*/ |
56
|
|
|
public static function createAsFilterNotFound(string $filterName): SchematorException |
57
|
|
|
{ |
58
|
|
|
return new SchematorException( |
59
|
|
|
"filter '{$filterName}' not found", |
60
|
|
|
SchematorException::FILTER_NOT_FOUND, |
61
|
|
|
null, |
62
|
|
|
[ |
63
|
|
|
'filter_name' => $filterName, |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates a new exception instance for "cannot get value" error |
70
|
|
|
* @param mixed $source data to get value from |
71
|
|
|
* @param string $key path key to get value by |
72
|
|
|
* @param Throwable|null $previous previous exception |
73
|
|
|
* @return SchematorException |
74
|
|
|
*/ |
75
|
|
|
public static function createAsCannotGetValue( |
76
|
|
|
$source, |
77
|
|
|
string $key, |
78
|
|
|
?Throwable $previous = null |
79
|
|
|
): SchematorException { |
80
|
|
|
return new SchematorException( |
81
|
|
|
"cannot get value by key '{$key}'", |
82
|
|
|
SchematorException::CANNOT_GET_VALUE, |
83
|
|
|
$previous, |
84
|
|
|
[ |
85
|
|
|
'key' => $key, |
86
|
|
|
'source' => $source, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates a new exception instance for "unsupported source type" error |
93
|
|
|
* @param mixed $source source |
94
|
|
|
* @param mixed $key path key to get value by |
95
|
|
|
* @param Throwable|null $previous previous exception |
96
|
|
|
* @return SchematorException |
97
|
|
|
*/ |
98
|
|
|
public static function createAsUnsupportedSourceType( |
99
|
|
|
$source, |
100
|
|
|
$key, |
101
|
|
|
?Throwable $previous = null |
102
|
|
|
): SchematorException { |
103
|
|
|
$sourceType = gettype($source); |
104
|
|
|
return new SchematorException( |
105
|
|
|
"unsupported source type '{$sourceType}'", |
106
|
|
|
SchematorException::UNSUPPORTED_SOURCE_TYPE, |
107
|
|
|
$previous, |
108
|
|
|
[ |
109
|
|
|
'key' => $key, |
110
|
|
|
'source' => $source, |
111
|
|
|
'source_type' => $sourceType, |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Creates a new exception instance for "unsupported key type" error |
118
|
|
|
* @param mixed $source source |
119
|
|
|
* @param mixed $key path key to get value by |
120
|
|
|
* @param Throwable|null $previous previous exception |
121
|
|
|
* @return SchematorException |
122
|
|
|
*/ |
123
|
|
|
public static function createAsUnsupportedKeyType( |
124
|
|
|
$source, |
125
|
|
|
$key, |
126
|
|
|
?Throwable $previous = null |
127
|
|
|
): SchematorException { |
128
|
|
|
$keyType = gettype($key); |
129
|
|
|
return new SchematorException( |
130
|
|
|
"unsupported key type '{$keyType}'", |
131
|
|
|
SchematorException::UNSUPPORTED_KEY_TYPE, |
132
|
|
|
$previous, |
133
|
|
|
[ |
134
|
|
|
'key' => $key, |
135
|
|
|
'source' => $source, |
136
|
|
|
'key_type' => $keyType, |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Creates a new exception instance for "unsupported filter config type" error |
143
|
|
|
* @param mixed $filterConfig filter config |
144
|
|
|
* @param Throwable|null $previous previous exception |
145
|
|
|
* @return SchematorException |
146
|
|
|
*/ |
147
|
|
|
public static function createAsUnsupportedFilterConfigType( |
148
|
|
|
$filterConfig, |
149
|
|
|
?Throwable $previous = null |
150
|
|
|
): SchematorException { |
151
|
|
|
$filterConfigType = gettype($filterConfig); |
152
|
|
|
return new SchematorException( |
153
|
|
|
"unsupported filter config type '{$filterConfigType}'", |
154
|
|
|
SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE, |
155
|
|
|
$previous, |
156
|
|
|
[ |
157
|
|
|
'filter_config' => $filterConfig, |
158
|
|
|
'filter_config_type' => $filterConfigType, |
159
|
|
|
] |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Creates a new exception instance for filter execution error |
165
|
|
|
* @param FilterContextInterface $filterContext name of the filter |
166
|
|
|
* @param ?Throwable $previous exception thrown in the filter body |
167
|
|
|
* @return SchematorException |
168
|
|
|
*/ |
169
|
|
|
public static function createAsFilterError( |
170
|
|
|
FilterContextInterface $filterContext, |
171
|
|
|
?Throwable $previous = null |
172
|
|
|
): SchematorException { |
173
|
|
|
return new SchematorException( |
174
|
|
|
"filter error: '{$filterContext->getFilterName()}'", |
175
|
|
|
SchematorException::FILTER_ERROR, |
176
|
|
|
$previous, |
177
|
|
|
[ |
178
|
|
|
'error' => $previous ? $previous->getMessage() : "filter error: '{$filterContext->getFilterName()}'", |
179
|
|
|
'filter_name' => $filterContext->getFilterName(), |
180
|
|
|
'config' => $filterContext->getConfig(), |
181
|
|
|
'source' => $filterContext->getSource(), |
182
|
|
|
] |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Creates a new exception instance for filter config error |
188
|
|
|
* @param FilterContextInterface $filterContext name of the filter |
189
|
|
|
* @param ?Throwable $previous exception thrown in the filter body |
190
|
|
|
* @return SchematorException |
191
|
|
|
*/ |
192
|
|
|
public static function createAsBadFilterConfig( |
193
|
|
|
FilterContextInterface $filterContext, |
194
|
|
|
?Throwable $previous = null |
195
|
|
|
): SchematorException { |
196
|
|
|
return new SchematorException( |
197
|
|
|
"bad config for filter '{$filterContext->getFilterName()}'", |
198
|
|
|
SchematorException::BAD_FILTER_CONFIG, |
199
|
|
|
$previous, |
200
|
|
|
[ |
201
|
|
|
'filter_name' => $filterContext->getFilterName(), |
202
|
|
|
'config' => $filterContext->getConfig(), |
203
|
|
|
'source' => $filterContext->getSource(), |
204
|
|
|
] |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Creates a new exception instance for filter source error |
210
|
|
|
* @param FilterContextInterface $filterContext name of the filter |
211
|
|
|
* @param ?Throwable $previous exception thrown in the filter body |
212
|
|
|
* @return SchematorException |
213
|
|
|
*/ |
214
|
|
|
public static function createAsBadFilterSource( |
215
|
|
|
FilterContextInterface $filterContext, |
216
|
|
|
?Throwable $previous = null |
217
|
|
|
): SchematorException { |
218
|
|
|
return new SchematorException( |
219
|
|
|
"bad source for filter '{$filterContext->getFilterName()}'", |
220
|
|
|
SchematorException::BAD_FILTER_SOURCE, |
221
|
|
|
$previous, |
222
|
|
|
[ |
223
|
|
|
'filter_name' => $filterContext->getFilterName(), |
224
|
|
|
'config' => $filterContext->getConfig(), |
225
|
|
|
'source' => $filterContext->getSource(), |
226
|
|
|
] |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|