| 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\Api\Shape; |
| 7 |
use Dudlewebs\WPMCS\s3\Aws\Api\TimestampShape; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Exception\InvalidJsonException; |
| 9 |
/** |
| 10 |
* Formats the JSON body of a JSON-REST or JSON-RPC operation. |
| 11 |
* @internal |
| 12 |
*/ |
| 13 |
class JsonBody |
| 14 |
{ |
| 15 |
private $api; |
| 16 |
public function __construct(Service $api) |
| 17 |
{ |
| 18 |
$this->api = $api; |
| 19 |
} |
| 20 |
/** |
| 21 |
* Gets the JSON Content-Type header for a service API |
| 22 |
* |
| 23 |
* @param Service $service |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public static function getContentType(Service $service) |
| 28 |
{ |
| 29 |
if ($service->getMetadata('protocol') === 'rest-json') { |
| 30 |
return 'application/json'; |
| 31 |
} |
| 32 |
$jsonVersion = $service->getMetadata('jsonVersion'); |
| 33 |
if (empty($jsonVersion)) { |
| 34 |
throw new \InvalidArgumentException('invalid json'); |
| 35 |
} else { |
| 36 |
return 'application/x-amz-json-' . @\number_format($service->getMetadata('jsonVersion'), 1); |
| 37 |
} |
| 38 |
} |
| 39 |
/** |
| 40 |
* Builds the JSON body based on an array of arguments. |
| 41 |
* |
| 42 |
* @param Shape $shape Operation being constructed |
| 43 |
* @param array $args Associative array of arguments |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public function build(Shape $shape, array $args) |
| 48 |
{ |
| 49 |
$result = \json_encode($this->format($shape, $args)); |
| 50 |
return $result == '[]' ? '{}' : $result; |
| 51 |
} |
| 52 |
private function format(Shape $shape, $value) |
| 53 |
{ |
| 54 |
switch ($shape['type']) { |
| 55 |
case 'structure': |
| 56 |
$data = []; |
| 57 |
if (isset($shape['document']) && $shape['document']) { |
| 58 |
return $value; |
| 59 |
} |
| 60 |
foreach ($value as $k => $v) { |
| 61 |
if ($v !== null && $shape->hasMember($k)) { |
| 62 |
$valueShape = $shape->getMember($k); |
| 63 |
$data[$valueShape['locationName'] ?: $k] = $this->format($valueShape, $v); |
| 64 |
} |
| 65 |
} |
| 66 |
if (empty($data)) { |
| 67 |
return new \stdClass(); |
| 68 |
} |
| 69 |
return $data; |
| 70 |
case 'list': |
| 71 |
$items = $shape->getMember(); |
| 72 |
foreach ($value as $k => $v) { |
| 73 |
$value[$k] = $this->format($items, $v); |
| 74 |
} |
| 75 |
return $value; |
| 76 |
case 'map': |
| 77 |
if (empty($value)) { |
| 78 |
return new \stdClass(); |
| 79 |
} |
| 80 |
$values = $shape->getValue(); |
| 81 |
foreach ($value as $k => $v) { |
| 82 |
$value[$k] = $this->format($values, $v); |
| 83 |
} |
| 84 |
return $value; |
| 85 |
case 'blob': |
| 86 |
return \base64_encode($value); |
| 87 |
case 'timestamp': |
| 88 |
$timestampFormat = !empty($shape['timestampFormat']) ? $shape['timestampFormat'] : 'unixTimestamp'; |
| 89 |
return TimestampShape::format($value, $timestampFormat); |
| 90 |
default: |
| 91 |
return $value; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|