| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Aws\Api\ListShape; |
| 5 |
use Aws\Api\MapShape; |
| 6 |
use Aws\Api\Service; |
| 7 |
use Aws\Api\Shape; |
| 8 |
use Aws\Api\StructureShape; |
| 9 |
use Closure; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class QueryCompatibleInputMiddleware |
| 19 |
{ |
| 20 |
|
| 21 |
private $nextHandler; |
| 22 |
|
| 23 |
|
| 24 |
private $service; |
| 25 |
|
| 26 |
|
| 27 |
private $command; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static function wrap(Service $service) : Closure |
| 36 |
{ |
| 37 |
return static function (callable $handler) use ($service) { |
| 38 |
return new self($handler, $service); |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
public function __construct(callable $nextHandler, Service $service) |
| 43 |
{ |
| 44 |
$this->service = $service; |
| 45 |
$this->nextHandler = $nextHandler; |
| 46 |
} |
| 47 |
|
| 48 |
public function __invoke(CommandInterface $cmd) |
| 49 |
{ |
| 50 |
$this->command = $cmd; |
| 51 |
$nextHandler = $this->nextHandler; |
| 52 |
$op = $this->service->getOperation($cmd->getName()); |
| 53 |
$inputMembers = $op->getInput()->getMembers(); |
| 54 |
$input = $cmd->toArray(); |
| 55 |
|
| 56 |
foreach ($input as $param => $value) { |
| 57 |
if (isset($inputMembers[$param])) { |
| 58 |
$shape = $inputMembers[$param]; |
| 59 |
$this->processInput($value, $shape, [$param]); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $nextHandler($this->command); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private function processInput($input, $shape, array $path) : void |
| 77 |
{ |
| 78 |
switch ($shape->getType()) { |
| 79 |
case 'structure': |
| 80 |
$this->processStructure($input, $shape, $path); |
| 81 |
break; |
| 82 |
case 'list': |
| 83 |
$this->processList($input, $shape, $path); |
| 84 |
break; |
| 85 |
case 'map': |
| 86 |
$this->processMap($input, $shape, $path); |
| 87 |
break; |
| 88 |
default: |
| 89 |
$this->processScalar($input, $shape, $path); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function processStructure( |
| 101 |
array $input, |
| 102 |
StructureShape $shape, |
| 103 |
array $path |
| 104 |
) : void |
| 105 |
{ |
| 106 |
foreach ($input as $param => $value) { |
| 107 |
if ($shape->hasMember($param)) { |
| 108 |
$memberPath = array_merge($path, [$param]); |
| 109 |
$this->processInput($value, $shape->getMember($param), $memberPath); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private function processList( |
| 122 |
array $input, |
| 123 |
ListShape $shape, |
| 124 |
array $path |
| 125 |
) : void |
| 126 |
{ |
| 127 |
foreach ($input as $param => $value) { |
| 128 |
$memberPath = array_merge($path, [$param]); |
| 129 |
$this->processInput($value, $shape->getMember(), $memberPath); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function processMap(array $input, MapShape $shape, array $path) : void |
| 141 |
{ |
| 142 |
foreach ($input as $param => $value) { |
| 143 |
$memberPath = array_merge($path, [$param]); |
| 144 |
$this->processInput($value, $shape->getValue(), $memberPath); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private function processScalar($input, Shape $shape, array $path) : void |
| 156 |
{ |
| 157 |
$expectedType = $shape->getType(); |
| 158 |
|
| 159 |
if (!$this->isModeledType($input, $expectedType)) { |
| 160 |
trigger_error( |
| 161 |
"The provided type for `". implode(' -> ', $path) ."` value was `" |
| 162 |
. (gettype($input) === 'double' ? 'float' : gettype($input)) . "`." |
| 163 |
. " The modeled type is `{$expectedType}`.", |
| 164 |
E_USER_WARNING |
| 165 |
); |
| 166 |
$value = $this->castValue($input, $expectedType); |
| 167 |
$this->changeValueAtPath($path, $value); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private function changeValueAtPath(array $path, $newValue) : void |
| 180 |
{ |
| 181 |
$commandRef = &$this->command; |
| 182 |
|
| 183 |
foreach ($path as $segment) { |
| 184 |
if (!isset($commandRef[$segment])) { |
| 185 |
return; |
| 186 |
} |
| 187 |
$commandRef = &$commandRef[$segment]; |
| 188 |
} |
| 189 |
$commandRef = $newValue; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private function isModeledType($value, $type) : bool |
| 199 |
{ |
| 200 |
switch ($type) { |
| 201 |
case 'string': |
| 202 |
return is_string($value); |
| 203 |
case 'integer': |
| 204 |
case 'long': |
| 205 |
return is_int($value); |
| 206 |
case 'float': |
| 207 |
return is_float($value); |
| 208 |
default: |
| 209 |
return true; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private function castValue($value, $type) |
| 220 |
{ |
| 221 |
switch ($type) { |
| 222 |
case 'integer': |
| 223 |
return (int) $value; |
| 224 |
case 'long' : |
| 225 |
return $value + 0; |
| 226 |
case 'float': |
| 227 |
return (float) $value; |
| 228 |
case 'string': |
| 229 |
return (string) $value; |
| 230 |
default: |
| 231 |
return $value; |
| 232 |
} |
| 233 |
} |
| 234 |
} |