Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/QueryCompatibleInputMiddleware.php

raw symbols SCIP
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 * Inspects command input values and casts them to their modeled type.
13 * This covers query compatible services which have migrated from query
14 * to JSON wire protocols.
15 *
16 * @internal
17 */
18 class QueryCompatibleInputMiddleware
19 {
20 /** @var callable */
21 private $nextHandler;
22
23 /** @var Service */
24 private $service;
25
26 /** @var CommandInterface */
27 private $command;
28
29 /**
30 * Create a middleware wrapper function.
31 *
32 * @param Service $service
33 * @return Closure
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 * Recurses a given input shape. if a given scalar input does not match its
68 * modeled type, it is cast to its modeled type.
69 *
70 * @param $input
71 * @param $shape
72 * @param array $path
73 *
74 * @return void
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 * @param array $input
95 * @param StructureShape $shape
96 * @param array $path
97 *
98 * @return void
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 * @param array $input
116 * @param ListShape $shape
117 * @param array $path
118 *
119 * @return void
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 * @param array $input
135 * @param MapShape $shape
136 * @param array $path
137 *
138 * @return void
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 * @param $input
150 * @param Shape $shape
151 * @param array $path
152 *
153 * @return void
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 * Modifies command in place
173 *
174 * @param array $path
175 * @param $newValue
176 *
177 * @return void
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 * @param $value
194 * @param $type
195 *
196 * @return bool
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 * @param $value
215 * @param $type
216 *
217 * @return float|int|mixed|string
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 }