| 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 |
|
| 11 |
|
| 12 |
class RestJsonParser extends AbstractRestParser |
| 13 |
{ |
| 14 |
use PayloadParserTrait; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 34 |
if (!empty($rawBody)) { |
| 35 |
$parsedJson = $this->parseJson($rawBody, $response); |
| 36 |
} else { |
| 37 |
|
| 38 |
$result = null; |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$parsedBody = $this->parser->parse($member, $parsedJson); |
| 43 |
if (is_string($parsedBody) && $member['document']) { |
| 44 |
|
| 45 |
$result = $parsedBody; |
| 46 |
} else { |
| 47 |
|
| 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 |
} |