|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Client\Response; |
|
6
|
|
|
|
|
7
|
|
|
use Inspirum\Balikobot\Exception\BadRequestException; |
|
8
|
|
|
use Inspirum\Balikobot\Exception\UnauthorizedException; |
|
9
|
|
|
use function array_is_list; |
|
|
|
|
|
|
10
|
|
|
use function count; |
|
11
|
|
|
use function max; |
|
12
|
|
|
|
|
13
|
|
|
final class Validator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Validate status code |
|
17
|
|
|
* |
|
18
|
|
|
* @param array<mixed,mixed> $response |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \Inspirum\Balikobot\Exception\Exception |
|
21
|
|
|
*/ |
|
22
|
530 |
|
public function validateStatus(int $statusCode, array $response = []): void |
|
23
|
|
|
{ |
|
24
|
|
|
// unauthorized |
|
25
|
530 |
|
if ($statusCode === 401 || $statusCode === 403) { |
|
26
|
2 |
|
throw new UnauthorizedException(null, $statusCode); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// request error |
|
30
|
528 |
|
if ($statusCode >= 400) { |
|
31
|
337 |
|
throw new BadRequestException($response, (int) max($statusCode, $response['status'] ?? 0)); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Validate response item status |
|
37
|
|
|
* |
|
38
|
|
|
* @param array<mixed,mixed> $responseItem |
|
39
|
|
|
* @param array<mixed,mixed>|null $response |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \Inspirum\Balikobot\Exception\Exception |
|
42
|
|
|
*/ |
|
43
|
512 |
|
public function validateResponseStatus( |
|
44
|
|
|
array $responseItem, |
|
45
|
|
|
?array $response = null, |
|
46
|
|
|
bool $shouldHaveStatus = true, |
|
47
|
|
|
): void { |
|
48
|
512 |
|
if ($shouldHaveStatus === false && isset($responseItem['status']) === false) { |
|
49
|
12 |
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
506 |
|
$this->validateStatus((int) ($responseItem['status'] ?? 500), $response ?? $responseItem); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Validate that every response item has given attribute |
|
57
|
|
|
* |
|
58
|
|
|
* @param array<array<string,mixed>> $items |
|
59
|
|
|
* @param array<mixed,mixed> $response |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \Inspirum\Balikobot\Exception\BadRequestException |
|
62
|
|
|
*/ |
|
63
|
3 |
|
public function validateResponseItemsHasAttribute(array $items, string $attribute, array $response): void |
|
64
|
|
|
{ |
|
65
|
3 |
|
foreach ($items as $item) { |
|
66
|
3 |
|
$this->validateResponseItemHasAttribute($item, $attribute, $response); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Validate that response item has given attribute |
|
72
|
|
|
* |
|
73
|
|
|
* @param array<string,mixed> $item |
|
74
|
|
|
* @param array<mixed,mixed> $response |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \Inspirum\Balikobot\Exception\BadRequestException |
|
77
|
|
|
*/ |
|
78
|
8 |
|
public function validateResponseItemHasAttribute(array $item, string $attribute, array $response): void |
|
79
|
|
|
{ |
|
80
|
8 |
|
if (isset($item[$attribute]) === false) { |
|
81
|
2 |
|
throw new BadRequestException($response); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Validate that response item has some attributes |
|
87
|
|
|
* |
|
88
|
|
|
* @param array<string,mixed> $item |
|
89
|
|
|
* @param array<string> $attributes |
|
90
|
|
|
* @param array<mixed,mixed> $response |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \Inspirum\Balikobot\Exception\BadRequestException |
|
93
|
|
|
*/ |
|
94
|
7 |
|
public function validateResponseItemHasSomeAttributes(array $item, array $attributes, array $response): void |
|
95
|
|
|
{ |
|
96
|
7 |
|
foreach ($attributes as $attribute) { |
|
97
|
7 |
|
if (isset($item[$attribute])) { |
|
98
|
6 |
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
throw new BadRequestException($response); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Validate response array has correct indexes |
|
107
|
|
|
* |
|
108
|
|
|
* @param array<mixed,mixed> $response |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \Inspirum\Balikobot\Exception\BadRequestException |
|
111
|
|
|
*/ |
|
112
|
38 |
|
public function validateIndexes(array $response, int $count): void |
|
113
|
|
|
{ |
|
114
|
38 |
|
if (array_is_list($response) === false || count($response) !== $count) { |
|
|
|
|
|
|
115
|
12 |
|
throw new BadRequestException($response); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|