Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Parser/AbstractRestParser.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Parser;
3
4 use Aws\Api\DateTimeResult;
5 use Aws\Api\Shape;
6 use Aws\Api\StructureShape;
7 use Aws\Result;
8 use Aws\CommandInterface;
9 use Psr\Http\Message\ResponseInterface;
10
11 /**
12 * @internal
13 */
14 abstract class AbstractRestParser extends AbstractParser
15 {
16 use PayloadParserTrait;
17
18 /**
19 * Parses a payload from a response.
20 *
21 * @param ResponseInterface $response Response to parse.
22 * @param StructureShape $member Member to parse
23 * @param array $result Result value
24 *
25 * @return mixed
26 */
27 abstract protected function payload(
28 ResponseInterface $response,
29 StructureShape $member,
30 array &$result
31 );
32
33 public function __invoke(
34 CommandInterface $command,
35 ResponseInterface $response
36 ) {
37 $output = $this->api->getOperation($command->getName())->getOutput();
38 $result = [];
39
40 if ($payload = $output['payload']) {
41 $this->extractPayload($payload, $output, $response, $result);
42 } else {
43 $response = AbstractParser::getResponseWithCachingStream($response);
44
45 if ($response->getBody()->getSize() === null) {
46 $rawBody = AbstractParser::getBodyContents($response);
47 $isEmpty = empty($rawBody);
48 } else {
49 $isEmpty = $response->getBody()->getSize() === 0;
50 }
51
52 if (!$isEmpty && count($output->getMembers()) > 0
53 ) {
54 // if no payload was found, then parse the contents of the body
55 $this->payload($response, $output, $result);
56 }
57 }
58
59 foreach ($output->getMembers() as $name => $member) {
60 switch ($member['location']) {
61 case 'header':
62 $this->extractHeader($name, $member, $response, $result);
63 break;
64 case 'headers':
65 $this->extractHeaders($name, $member, $response, $result);
66 break;
67 case 'statusCode':
68 $this->extractStatus($name, $response, $result);
69 break;
70 }
71 }
72
73 return new Result($result);
74 }
75
76 private function extractPayload(
77 $payload,
78 StructureShape $output,
79 ResponseInterface $response,
80 array &$result
81 ) {
82 $member = $output->getMember($payload);
83 $body = $response->getBody();
84 if (!empty($member['eventstream'])) {
85 $result[$payload] = new EventParsingIterator(
86 $body,
87 $member,
88 $this
89 );
90
91 return;
92 }
93
94 $response = AbstractParser::getResponseWithCachingStream($response);
95
96 if ($member instanceof StructureShape) {
97 //Unions must have at least one member set to a non-null value
98 // If the body is empty, we can assume it is unset
99 if ($response->getBody()->getSize() === null) {
100 $rawBody = AbstractParser::getBodyContents($response);
101 $isEmpty = empty($rawBody);
102 } else {
103 $isEmpty = $response->getBody()->getSize() === 0;
104 }
105
106 if (!empty($member['union']) && $isEmpty) {
107 return;
108 }
109
110 $result[$payload] = [];
111 $this->payload($response, $member, $result[$payload]);
112 } else {
113 // Always set the payload to the body stream, regardless of content
114 $result[$payload] = $body;
115 }
116 }
117
118 /**
119 * Extract a single header from the response into the result.
120 */
121 private function extractHeader(
122 $name,
123 Shape $shape,
124 ResponseInterface $response,
125 &$result
126 ) {
127 $value = $response->getHeaderLine($shape['locationName'] ?: $name);
128 // Empty headers should not be deserialized
129 if ($value === null || $value === '') {
130 return;
131 }
132
133 switch ($shape->getType()) {
134 case 'float':
135 case 'double':
136 $value = match ($value) {
137 'NaN', 'Infinity', '-Infinity' => $value,
138 default => (float) $value
139 };
140 break;
141 case 'long':
142 case 'integer':
143 $value = (int) $value;
144 break;
145 case 'boolean':
146 $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
147 break;
148 case 'blob':
149 $value = base64_decode($value);
150 break;
151 case 'timestamp':
152 try {
153 $value = DateTimeResult::fromTimestamp(
154 $value,
155 !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : null
156 );
157 break;
158 } catch (\Exception $e) {
159 // If the value cannot be parsed, then do not add it to the
160 // output structure.
161 return;
162 }
163 case 'string':
164 try {
165 if ($shape['jsonvalue']) {
166 $value = $this->parseJson(base64_decode($value), $response);
167 }
168
169 // If value is not set, do not add to output structure.
170 if (!isset($value)) {
171 return;
172 }
173 break;
174 } catch (\Exception $e) {
175 //If the value cannot be parsed, then do not add it to the
176 //output structure.
177 return;
178 }
179 case 'list':
180 $listMember = $shape->getMember();
181 $type = $listMember->getType();
182
183 // Only boolean lists require special handling
184 // other types can be returned as-is
185 if ($type !== 'boolean') {
186 break;
187 }
188
189 $items = array_map('trim', explode(',', $value));
190 $value = array_map(
191 static fn($item) => filter_var($item, FILTER_VALIDATE_BOOLEAN),
192 $items
193 );
194
195 break;
196 }
197
198 $result[$name] = $value;
199 }
200
201 /**
202 * Extract a map of headers with an optional prefix from the response.
203 */
204 private function extractHeaders(
205 $name,
206 Shape $shape,
207 ResponseInterface $response,
208 &$result
209 ) {
210 // Check if the headers are prefixed by a location name
211 $result[$name] = [];
212 $prefix = $shape['locationName'];
213 $prefixLen = $prefix !== null ? strlen($prefix) : 0;
214
215 foreach ($response->getHeaders() as $k => $values) {
216 if (!$prefixLen) {
217 $result[$name][$k] = implode(', ', $values);
218 } elseif (stripos($k, $prefix) === 0) {
219 $result[$name][substr($k, $prefixLen)] = implode(', ', $values);
220 }
221 }
222 }
223
224 /**
225 * Places the status code of the response into the result array.
226 */
227 private function extractStatus(
228 $name,
229 ResponseInterface $response,
230 array &$result
231 ) {
232 $result[$name] = (int) $response->getStatusCode();
233 }
234 }