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