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
QueryParamBuilder.php
158 lines
| 1 | <?php |
| 2 | namespace Aws\Api\Serializer; |
| 3 | |
| 4 | use Aws\Api\StructureShape; |
| 5 | use Aws\Api\ListShape; |
| 6 | use Aws\Api\MapShape; |
| 7 | use Aws\Api\Shape; |
| 8 | use Aws\Api\TimestampShape; |
| 9 | |
| 10 | /** |
| 11 | * @internal |
| 12 | */ |
| 13 | class QueryParamBuilder |
| 14 | { |
| 15 | private $methods; |
| 16 | |
| 17 | protected function queryName(Shape $shape, $default = null) |
| 18 | { |
| 19 | if (null !== $shape['queryName']) { |
| 20 | return $shape['queryName']; |
| 21 | } |
| 22 | |
| 23 | if (null !== $shape['locationName']) { |
| 24 | return $shape['locationName']; |
| 25 | } |
| 26 | |
| 27 | if ($this->isFlat($shape) && !empty($shape['member']['locationName'])) { |
| 28 | return $shape['member']['locationName']; |
| 29 | } |
| 30 | |
| 31 | return $default; |
| 32 | } |
| 33 | |
| 34 | protected function isFlat(Shape $shape) |
| 35 | { |
| 36 | return $shape['flattened'] === true; |
| 37 | } |
| 38 | |
| 39 | public function __invoke(StructureShape $shape, array $params) |
| 40 | { |
| 41 | if (!$this->methods) { |
| 42 | $this->methods = array_fill_keys(get_class_methods($this), true); |
| 43 | } |
| 44 | |
| 45 | $query = []; |
| 46 | $this->format_structure($shape, $params, '', $query); |
| 47 | |
| 48 | return $query; |
| 49 | } |
| 50 | |
| 51 | protected function format(Shape $shape, $value, $prefix, array &$query) |
| 52 | { |
| 53 | $type = 'format_' . $shape['type']; |
| 54 | if (isset($this->methods[$type])) { |
| 55 | $this->{$type}($shape, $value, $prefix, $query); |
| 56 | } else { |
| 57 | $query[$prefix] = (string) $value; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | protected function format_structure( |
| 62 | StructureShape $shape, |
| 63 | array $value, |
| 64 | $prefix, |
| 65 | &$query |
| 66 | ) { |
| 67 | if ($prefix) { |
| 68 | $prefix .= '.'; |
| 69 | } |
| 70 | |
| 71 | foreach ($value as $k => $v) { |
| 72 | if ($shape->hasMember($k)) { |
| 73 | $member = $shape->getMember($k); |
| 74 | $this->format( |
| 75 | $member, |
| 76 | $v, |
| 77 | $prefix . $this->queryName($member, $k), |
| 78 | $query |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected function format_list( |
| 85 | ListShape $shape, |
| 86 | array $value, |
| 87 | $prefix, |
| 88 | &$query |
| 89 | ) { |
| 90 | // Handle empty list serialization |
| 91 | if (!$value) { |
| 92 | $query[$prefix] = ''; |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $items = $shape->getMember(); |
| 97 | |
| 98 | if (!$this->isFlat($shape)) { |
| 99 | $locationName = $shape->getMember()['locationName'] ?: 'member'; |
| 100 | $prefix .= ".$locationName"; |
| 101 | } elseif ($name = $this->queryName($items)) { |
| 102 | $parts = explode('.', $prefix); |
| 103 | $parts[count($parts) - 1] = $name; |
| 104 | $prefix = implode('.', $parts); |
| 105 | } |
| 106 | |
| 107 | foreach ($value as $k => $v) { |
| 108 | $this->format($items, $v, $prefix . '.' . ($k + 1), $query); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | protected function format_map( |
| 113 | MapShape $shape, |
| 114 | array $value, |
| 115 | $prefix, |
| 116 | array &$query |
| 117 | ) { |
| 118 | $vals = $shape->getValue(); |
| 119 | $keys = $shape->getKey(); |
| 120 | |
| 121 | if (!$this->isFlat($shape)) { |
| 122 | $prefix .= '.entry'; |
| 123 | } |
| 124 | |
| 125 | $i = 0; |
| 126 | $keyName = '%s.%d.' . $this->queryName($keys, 'key'); |
| 127 | $valueName = '%s.%s.' . $this->queryName($vals, 'value'); |
| 128 | |
| 129 | foreach ($value as $k => $v) { |
| 130 | $i++; |
| 131 | $this->format($keys, $k, sprintf($keyName, $prefix, $i), $query); |
| 132 | $this->format($vals, $v, sprintf($valueName, $prefix, $i), $query); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | protected function format_blob(Shape $shape, $value, $prefix, array &$query) |
| 137 | { |
| 138 | $query[$prefix] = base64_encode($value); |
| 139 | } |
| 140 | |
| 141 | protected function format_timestamp( |
| 142 | TimestampShape $shape, |
| 143 | $value, |
| 144 | $prefix, |
| 145 | array &$query |
| 146 | ) { |
| 147 | $timestampFormat = !empty($shape['timestampFormat']) |
| 148 | ? $shape['timestampFormat'] |
| 149 | : 'iso8601'; |
| 150 | $query[$prefix] = TimestampShape::format($value, $timestampFormat); |
| 151 | } |
| 152 | |
| 153 | protected function format_boolean(Shape $shape, $value, $prefix, array &$query) |
| 154 | { |
| 155 | $query[$prefix] = ($value) ? 'true' : 'false'; |
| 156 | } |
| 157 | } |
| 158 |