QueryParser.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Aws\Api\Parser;
  3. use Aws\Api\Service;
  4. use Aws\Api\StructureShape;
  5. use Aws\Result;
  6. use Aws\CommandInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\StreamInterface;
  9. /**
  10. * @internal Parses query (XML) responses (e.g., EC2, SQS, and many others)
  11. */
  12. class QueryParser extends AbstractParser
  13. {
  14. use PayloadParserTrait;
  15. /** @var bool */
  16. private $honorResultWrapper;
  17. /**
  18. * @param Service $api Service description
  19. * @param XmlParser $xmlParser Optional XML parser
  20. * @param bool $honorResultWrapper Set to false to disable the peeling
  21. * back of result wrappers from the
  22. * output structure.
  23. */
  24. public function __construct(
  25. Service $api,
  26. XmlParser $xmlParser = null,
  27. $honorResultWrapper = true
  28. ) {
  29. parent::__construct($api);
  30. $this->parser = $xmlParser ?: new XmlParser();
  31. $this->honorResultWrapper = $honorResultWrapper;
  32. }
  33. public function __invoke(
  34. CommandInterface $command,
  35. ResponseInterface $response
  36. ) {
  37. $output = $this->api->getOperation($command->getName())->getOutput();
  38. $xml = $this->parseXml($response->getBody(), $response);
  39. if ($this->honorResultWrapper && $output['resultWrapper']) {
  40. $xml = $xml->{$output['resultWrapper']};
  41. }
  42. return new Result($this->parser->parse($output, $xml));
  43. }
  44. public function parseMemberFromStream(
  45. StreamInterface $stream,
  46. StructureShape $member,
  47. $response
  48. ) {
  49. $xml = $this->parseXml($stream, $response);
  50. return $this->parser->parse($member, $xml);
  51. }
  52. }