Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\Api\Parser;
3
4 use Aws\Api\Service;
5 use Aws\Api\StructureShape;
6 use Psr\Http\Message\ResponseInterface;
7 use Psr\Http\Message\StreamInterface;
8
9 /**
10 * @internal Implements REST-JSON parsing (e.g., Glacier, Elastic Transcoder)
11 */
12 class RestJsonParser extends AbstractRestParser
13 {
14 use PayloadParserTrait;
15
16 /**
17 * @param Service $api Service description
18 * @param JsonParser $parser JSON body builder
19 */
20 public function __construct(Service $api, ?JsonParser $parser = null)
21 {
22 parent::__construct($api);
23 $this->parser = $parser ?: new JsonParser();
24 }
25
26 protected function payload(
27 ResponseInterface $response,
28 StructureShape $member,
29 array &$result
30 ) {
31 $rawBody = AbstractParser::getBodyContents($response);
32
33 // Parse JSON if we have content
34 if (!empty($rawBody)) {
35 $parsedJson = $this->parseJson($rawBody, $response);
36 } else {
37 // An empty response body should be deserialized as null
38 $result = null;
39 return;
40 }
41
42 $parsedBody = $this->parser->parse($member, $parsedJson);
43 if (is_string($parsedBody) && $member['document']) {
44 // Document types can be strings: replace entire result
45 $result = $parsedBody;
46 } else {
47 // Merge array/object results into existing result
48 $result = array_merge($result, (array) $parsedBody);
49 }
50 }
51
52 public function parseMemberFromStream(
53 StreamInterface $stream,
54 StructureShape $member,
55 $response
56 ) {
57 $jsonBody = $this->parseJson($stream, $response);
58 if ($jsonBody) {
59 return $this->parser->parse($member, $jsonBody);
60 }
61 return [];
62 }
63 }