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
|
516 |
|
public function validateStatus(int $statusCode, array $response = []): void |
23
|
|
|
{ |
24
|
|
|
// unauthorized |
25
|
516 |
|
if ($statusCode === 401 || $statusCode === 403) { |
26
|
2 |
|
throw new UnauthorizedException(null, $statusCode); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// request error |
30
|
514 |
|
if ($statusCode >= 400) { |
31
|
356 |
|
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
|
498 |
|
public function validateResponseStatus( |
44
|
|
|
array $responseItem, |
45
|
|
|
?array $response = null, |
46
|
|
|
bool $shouldHaveStatus = true, |
47
|
|
|
): void { |
48
|
498 |
|
if ($shouldHaveStatus === false && isset($responseItem['status']) === false) { |
49
|
12 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
492 |
|
$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 validateResponseItemHasAttribute(array $items, string $attribute, array $response): void |
64
|
|
|
{ |
65
|
3 |
|
foreach ($items as $item) { |
66
|
3 |
|
if (isset($item[$attribute]) === false) { |
67
|
1 |
|
throw new BadRequestException($response); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validate response array has correct indexes |
74
|
|
|
* |
75
|
|
|
* @param array<mixed,mixed> $response |
76
|
|
|
* |
77
|
|
|
* @throws \Inspirum\Balikobot\Exception\BadRequestException |
78
|
|
|
*/ |
79
|
38 |
|
public function validateIndexes(array $response, int $count): void |
80
|
|
|
{ |
81
|
38 |
|
if (array_is_list($response) === false || count($response) !== $count) { |
|
|
|
|
82
|
12 |
|
throw new BadRequestException($response); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|