generated from spawnia/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Response.php
186 lines (154 loc) · 5.47 KB
/
Response.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
namespace Spawnia\Sailor;
use GraphQL\Executor\ExecutionResult;
use Psr\Http\Message\ResponseInterface;
use Safe\Exceptions\JsonException;
/**
* Represents a response sent by a GraphQL server.
*
* During instantiation, the response structure is validated.
* That does guarantee the server at least sent a syntactically
* correct response, although it does not guarantee the content
* matches the sent query.
*/
class Response
{
/**
* The result of the execution of the requested operation.
*/
public ?\stdClass $data;
/**
* A non‐empty list of errors, where each error is a map.
*
* @var array<int, \stdClass>|null
*/
public ?array $errors;
/**
* This entry, if set, must have a map as its value.
*/
public ?\stdClass $extensions;
public static function fromResponseInterface(ResponseInterface $response): self
{
if ($response->getStatusCode() !== 200) {
throw new InvalidResponseException("Response must have status code 200, got: {$response->getStatusCode()}");
}
return self::fromJson(
$response->getBody()->getContents()
);
}
public static function fromExecutionResult(ExecutionResult $executionResult): self
{
return self::fromJson(
\Safe\json_encode($executionResult->toArray())
);
}
public static function fromJson(string $json): self
{
try {
$response = \Safe\json_decode($json);
} catch (JsonException $jsonException) {
throw new InvalidResponseException("Received a response that is invalid JSON: {$json}", 0, $jsonException);
}
if (! $response instanceof \stdClass) {
throw new InvalidResponseException("A response to a GraphQL operation must be a map, got: {$json}");
}
return self::fromStdClass($response);
}
public static function fromStdClass(\stdClass $rawResponse): self
{
$hasData = property_exists($rawResponse, 'data');
$hasErrors = property_exists($rawResponse, 'errors');
if (! $hasData && ! $hasErrors) {
throw new InvalidResponseException('A valid GraphQL response must contain either "data" or "errors", got: '.\Safe\json_encode($rawResponse));
}
$instance = new self;
if ($hasErrors) {
$errors = $rawResponse->errors;
self::validateErrors($errors);
$instance->errors = $errors;
}
if ($hasData) {
$data = $rawResponse->data;
self::validateData($data);
$instance->data = $data;
}
if (property_exists($rawResponse, 'extensions')) {
$extensions = $rawResponse->extensions;
self::validateExtensions($extensions);
$instance->extensions = $extensions;
}
return $instance;
}
/**
* Throw an exception if errors are present in the result.
*
* @return $this
*
* @throws \Spawnia\Sailor\ResultErrorsException
*/
public function assertErrorFree(): self
{
if (isset($this->errors)) {
throw new ResultErrorsException($this->errors);
}
return $this;
}
/**
* Ensure that the "errors" are in a spec-compliant format.
*
* @param mixed $errors Whatever came from the API under the key "errors".
*
* @throws \Exception
*/
protected static function validateErrors($errors): void
{
if (! is_array($errors)) {
throw new InvalidResponseException('The response entry "errors" must be a list if present, got: '.\Safe\json_encode($errors));
}
if (count($errors) === 0) {
throw new InvalidResponseException('The response entry "errors" must not be empty if present, got: '.\Safe\json_encode($errors));
}
foreach ($errors as $error) {
if (! $error instanceof \stdClass) {
throw new InvalidResponseException('Each error in the response must be a map, got: '.\Safe\json_encode($error));
}
if (! property_exists($error, 'message')) {
throw new InvalidResponseException('Each error in the response must contain a key "message", got: '.\Safe\json_encode($error));
}
if (! is_string($error->message)) {
throw new InvalidResponseException('Each error in the response must contain a key "message" that is a string, got: '.\Safe\json_encode($error));
}
}
}
/**
* Ensure that the "data" is in a spec-compliant format.
*
* @param mixed $data Whatever came from the API under the key "data".
*
* @throws \Exception
*/
protected static function validateData($data): void
{
if (
$data instanceof \stdClass
|| $data === null
) {
return;
}
throw new InvalidResponseException('The response entry "data" must be a map or "null", got: '.\Safe\json_encode($data));
}
/**
* Ensure that the "extensions" are in a spec-compliant format.
*
* @param mixed $extensions Whatever came from the API under the key "extensions".
*
* @throws \Exception
*/
protected static function validateExtensions($extensions): void
{
if (! $extensions instanceof \stdClass) {
throw new InvalidResponseException('The response entry "extensions" must be a map.');
}
}
}