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