QuerySerializer.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Aws\Api\Serializer;
  3. use Aws\Api\Service;
  4. use Aws\CommandInterface;
  5. use GuzzleHttp\Psr7\Request;
  6. use Psr\Http\Message\RequestInterface;
  7. /**
  8. * Serializes a query protocol request.
  9. * @internal
  10. */
  11. class QuerySerializer
  12. {
  13. private $endpoint;
  14. private $api;
  15. private $paramBuilder;
  16. public function __construct(
  17. Service $api,
  18. $endpoint,
  19. callable $paramBuilder = null
  20. ) {
  21. $this->api = $api;
  22. $this->endpoint = $endpoint;
  23. $this->paramBuilder = $paramBuilder ?: new QueryParamBuilder();
  24. }
  25. /**
  26. * When invoked with an AWS command, returns a serialization array
  27. * containing "method", "uri", "headers", and "body" key value pairs.
  28. *
  29. * @param CommandInterface $command
  30. *
  31. * @return RequestInterface
  32. */
  33. public function __invoke(CommandInterface $command)
  34. {
  35. $operation = $this->api->getOperation($command->getName());
  36. $body = [
  37. 'Action' => $command->getName(),
  38. 'Version' => $this->api->getMetadata('apiVersion')
  39. ];
  40. $params = $command->toArray();
  41. // Only build up the parameters when there are parameters to build
  42. if ($params) {
  43. $body += call_user_func(
  44. $this->paramBuilder,
  45. $operation->getInput(),
  46. $params
  47. );
  48. }
  49. $body = http_build_query($body, null, '&', PHP_QUERY_RFC3986);
  50. return new Request(
  51. 'POST',
  52. $this->endpoint,
  53. [
  54. 'Content-Length' => strlen($body),
  55. 'Content-Type' => 'application/x-www-form-urlencoded'
  56. ],
  57. $body
  58. );
  59. }
  60. }