JsonRpcParser.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Aws\Api\Parser;
  3. use Aws\Api\StructureShape;
  4. use Aws\Api\Service;
  5. use Aws\Result;
  6. use Aws\CommandInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\StreamInterface;
  9. /**
  10. * @internal Implements JSON-RPC parsing (e.g., DynamoDB)
  11. */
  12. class JsonRpcParser extends AbstractParser
  13. {
  14. use PayloadParserTrait;
  15. /**
  16. * @param Service $api Service description
  17. * @param JsonParser $parser JSON body builder
  18. */
  19. public function __construct(Service $api, JsonParser $parser = null)
  20. {
  21. parent::__construct($api);
  22. $this->parser = $parser ?: new JsonParser();
  23. }
  24. public function __invoke(
  25. CommandInterface $command,
  26. ResponseInterface $response
  27. ) {
  28. $operation = $this->api->getOperation($command->getName());
  29. $result = null === $operation['output']
  30. ? null
  31. : $this->parseMemberFromStream(
  32. $response->getBody(),
  33. $operation->getOutput(),
  34. $response
  35. );
  36. return new Result($result ?: []);
  37. }
  38. public function parseMemberFromStream(
  39. StreamInterface $stream,
  40. StructureShape $member,
  41. $response
  42. ) {
  43. return $this->parser->parse($member, $this->parseJson($stream, $response));
  44. }
  45. }