QuerySerializer.php 2.2 KB

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