|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inspirum\Balikobot\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Inspirum\Balikobot\Exceptions\BadRequestException; |
|
6
|
|
|
use Inspirum\Balikobot\Exceptions\UnauthorizedException; |
|
7
|
|
|
|
|
8
|
|
|
class Validator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Validate status code |
|
12
|
|
|
* |
|
13
|
|
|
* @param int $statusCode |
|
14
|
|
|
* @param array<mixed,mixed> $response |
|
15
|
|
|
* |
|
16
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
17
|
|
|
*/ |
|
18
|
312 |
|
public function validateStatus(int $statusCode, array $response = []): void |
|
19
|
|
|
{ |
|
20
|
|
|
// unauthorized |
|
21
|
312 |
|
if ($statusCode === 401 || $statusCode === 403) { |
|
22
|
2 |
|
throw new UnauthorizedException(null, $statusCode); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// request error |
|
26
|
310 |
|
if ($statusCode >= 400) { |
|
27
|
107 |
|
throw new BadRequestException($response, (int) ($response['status'] ?? $statusCode)); |
|
28
|
|
|
} |
|
29
|
270 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Validate response item status |
|
33
|
|
|
* |
|
34
|
|
|
* @param array<mixed,mixed> $responseItem |
|
35
|
|
|
* @param array<mixed,mixed>|null $response |
|
36
|
|
|
* @param bool $shouldHaveStatus |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
* |
|
40
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
41
|
|
|
*/ |
|
42
|
270 |
|
public function validateResponseStatus( |
|
43
|
|
|
array $responseItem, |
|
44
|
|
|
array $response = null, |
|
45
|
|
|
bool $shouldHaveStatus = true |
|
46
|
|
|
): void { |
|
47
|
270 |
|
if ($shouldHaveStatus === false && isset($responseItem['status']) === false) { |
|
48
|
31 |
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
260 |
|
$this->validateStatus((int) ($responseItem['status'] ?? 500), $response ?: $responseItem); |
|
52
|
197 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Validate that every response item has given attribute |
|
56
|
|
|
* |
|
57
|
|
|
* @param array<int,array<string,mixed>> $items |
|
58
|
|
|
* @param string $attribute |
|
59
|
|
|
* @param array<mixed,mixed> $response |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
|
64
|
|
|
*/ |
|
65
|
30 |
|
public function validateResponseItemHasAttribute(array $items, string $attribute, array $response): void |
|
66
|
|
|
{ |
|
67
|
30 |
|
foreach ($items as $item) { |
|
68
|
30 |
|
if (isset($item[$attribute]) === false) { |
|
69
|
6 |
|
throw new BadRequestException($response); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
24 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Validate response array has correct indexes |
|
76
|
|
|
* |
|
77
|
|
|
* @param array<mixed,mixed> $response |
|
78
|
|
|
* @param int $count |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
|
83
|
|
|
*/ |
|
84
|
83 |
|
public function validateIndexes(array $response, int $count): void |
|
85
|
|
|
{ |
|
86
|
83 |
|
if (array_keys($response) !== range(0, $count - 1)) { |
|
87
|
15 |
|
throw new BadRequestException($response); |
|
88
|
|
|
} |
|
89
|
68 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|