| 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 |
|
| 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 |
|
| 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 |
|
| 102 |
} elseif ($name = $shape['locationName'] ?? $this->queryName($items)) { |
| 103 |
$parts = explode('.', $prefix); |
| 104 |
$parts[count($parts) - 1] = $name; |
| 105 |
$prefix = implode('.', $parts); |
| 106 |
} |
| 107 |
|
| 108 |
foreach ($value as $k => $v) { |
| 109 |
$this->format($items, $v, $prefix . '.' . ($k + 1), $query); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
protected function format_map( |
| 114 |
MapShape $shape, |
| 115 |
array $value, |
| 116 |
$prefix, |
| 117 |
array &$query |
| 118 |
) { |
| 119 |
$vals = $shape->getValue(); |
| 120 |
$keys = $shape->getKey(); |
| 121 |
|
| 122 |
if (!$this->isFlat($shape)) { |
| 123 |
$prefix .= '.entry'; |
| 124 |
} |
| 125 |
|
| 126 |
$i = 0; |
| 127 |
$keyName = '%s.%d.' . $this->queryName($keys, 'key'); |
| 128 |
$valueName = '%s.%s.' . $this->queryName($vals, 'value'); |
| 129 |
|
| 130 |
foreach ($value as $k => $v) { |
| 131 |
$i++; |
| 132 |
$this->format($keys, $k, sprintf($keyName, $prefix, $i), $query); |
| 133 |
$this->format($vals, $v, sprintf($valueName, $prefix, $i), $query); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
protected function format_blob(Shape $shape, $value, $prefix, array &$query) |
| 138 |
{ |
| 139 |
$query[$prefix] = base64_encode($value); |
| 140 |
} |
| 141 |
|
| 142 |
protected function format_timestamp( |
| 143 |
TimestampShape $shape, |
| 144 |
$value, |
| 145 |
$prefix, |
| 146 |
array &$query |
| 147 |
) { |
| 148 |
$timestampFormat = !empty($shape['timestampFormat']) |
| 149 |
? $shape['timestampFormat'] |
| 150 |
: 'iso8601'; |
| 151 |
$query[$prefix] = TimestampShape::format($value, $timestampFormat); |
| 152 |
} |
| 153 |
|
| 154 |
protected function format_boolean(Shape $shape, $value, $prefix, array &$query) |
| 155 |
{ |
| 156 |
$query[$prefix] = ($value) ? 'true' : 'false'; |
| 157 |
} |
| 158 |
} |