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