1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\JsonRpc; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\JsonRpc\Exception\ResponseParseException; |
6
|
|
|
|
7
|
|
|
/** @internal */ |
8
|
|
|
final class ResponseBodyValidator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \stdClass $response |
12
|
|
|
* @throws ResponseParseException |
13
|
|
|
*/ |
14
|
21 |
|
public function validate(\stdClass $response) |
15
|
|
|
{ |
16
|
21 |
|
if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) { |
17
|
1 |
|
throw ResponseParseException::bothErrorAndResultPresent(); |
18
|
|
|
} |
19
|
|
|
|
20
|
20 |
|
if (!property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && !property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) { |
21
|
4 |
|
throw ResponseParseException::noErrorOrResultPresent(); |
22
|
|
|
} |
23
|
|
|
|
24
|
16 |
|
if (!property_exists($response, JsonRpcResponseInterface::ID_FIELD)) { |
25
|
1 |
|
throw ResponseParseException::noIdSpecified(); |
26
|
|
|
} |
27
|
|
|
|
28
|
15 |
|
if (!property_exists($response, JsonRpcResponseInterface::VERSION_FIELD)) { |
29
|
1 |
|
throw ResponseParseException::noVersionSpecified(); |
30
|
|
|
} |
31
|
|
|
|
32
|
14 |
|
if ($response->jsonrpc !== JsonRpcClient::VERSION) { |
33
|
1 |
|
throw ResponseParseException::inconsistentVersionReceived(); |
34
|
|
|
} |
35
|
|
|
|
36
|
13 |
|
if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD)) { |
37
|
9 |
|
if (!is_object($response->{JsonRpcResponseInterface::ERROR_FIELD})) { |
38
|
1 |
|
throw ResponseParseException::errorIsNotAnObject(); |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_CODE_FIELD)) { |
42
|
1 |
|
throw ResponseParseException::noErrorCodePresent(); |
43
|
|
|
} |
44
|
|
|
|
45
|
7 |
|
$error = $response->{JsonRpcResponseInterface::ERROR_FIELD}->{JsonRpcErrorInterface::ERROR_CODE_FIELD}; |
46
|
7 |
|
if (!is_int($error)) { |
47
|
1 |
|
throw ResponseParseException::unexpectedType('error.code', 'integer', gettype($error)); |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_MESSAGE_FIELD)) { |
51
|
1 |
|
throw ResponseParseException::noErrorMessagePresent(); |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
$message = $response->{JsonRpcResponseInterface::ERROR_FIELD}->{JsonRpcErrorInterface::ERROR_MESSAGE_FIELD}; |
55
|
5 |
|
if (!is_string($message)) { |
56
|
1 |
|
throw ResponseParseException::unexpectedType('error.message', 'string', gettype($message)); |
57
|
|
|
} |
58
|
4 |
|
} |
59
|
8 |
|
} |
60
|
|
|
} |
61
|
|
|
|