Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Validator.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api;
3
4 use Aws;
5 use stdClass;
6
7 /**
8 * Validates a schema against a hash of input.
9 */
10 class Validator
11 {
12 private $path = [];
13 private $errors = [];
14 private $constraints = [];
15
16 private static $defaultConstraints = [
17 'required' => true,
18 'min' => true,
19 'max' => false,
20 'pattern' => false
21 ];
22
23 /**
24 * @param array $constraints Associative array of constraints to enforce.
25 * Accepts the following keys: "required", "min",
26 * "max", and "pattern". If a key is not
27 * provided, the constraint will assume false.
28 */
29 public function __construct(?array $constraints = null)
30 {
31 static $assumedFalseValues = [
32 'required' => false,
33 'min' => false,
34 'max' => false,
35 'pattern' => false
36 ];
37 $this->constraints = empty($constraints)
38 ? self::$defaultConstraints
39 : $constraints + $assumedFalseValues;
40 }
41
42 /**
43 * Validates the given input against the schema.
44 *
45 * @param string $name Operation name
46 * @param Shape $shape Shape to validate
47 * @param array $input Input to validate
48 *
49 * @throws \InvalidArgumentException if the input is invalid.
50 */
51 public function validate($name, Shape $shape, array $input)
52 {
53 $this->dispatch($shape, $input);
54
55 if ($this->errors) {
56 $message = sprintf(
57 "Found %d error%s while validating the input provided for the "
58 . "%s operation:\n%s",
59 count($this->errors),
60 count($this->errors) > 1 ? 's' : '',
61 $name,
62 implode("\n", $this->errors)
63 );
64 $this->errors = [];
65
66 throw new \InvalidArgumentException($message);
67 }
68 }
69
70 private function dispatch(Shape $shape, $value)
71 {
72 static $methods = [
73 'structure' => 'check_structure',
74 'list' => 'check_list',
75 'map' => 'check_map',
76 'blob' => 'check_blob',
77 'boolean' => 'check_boolean',
78 'integer' => 'check_numeric',
79 'float' => 'check_numeric',
80 'long' => 'check_numeric',
81 'string' => 'check_string',
82 'byte' => 'check_string',
83 'char' => 'check_string'
84 ];
85
86 $type = $shape->getType();
87 if (isset($methods[$type])) {
88 $this->{$methods[$type]}($shape, $value);
89 }
90 }
91
92 private function check_structure(StructureShape $shape, $value)
93 {
94 $isDocument = (isset($shape['document']) && $shape['document']);
95 $isUnion = (isset($shape['union']) && $shape['union']);
96 if ($isDocument) {
97 if (!$this->checkDocumentType($value)) {
98 $this->addError("is not a valid document type");
99 return;
100 }
101 } elseif ($isUnion) {
102 if (!$this->checkUnion($value)) {
103 $this->addError("is a union type and must have exactly one non null value");
104 return;
105 }
106 } elseif (!$this->checkAssociativeArray($value)) {
107 return;
108 }
109
110 if ($this->constraints['required'] && $shape['required']) {
111 foreach ($shape['required'] as $req) {
112 if (!isset($value[$req])) {
113 $this->path[] = $req;
114 $this->addError('is missing and is a required parameter');
115 array_pop($this->path);
116 }
117 }
118 }
119 if (!$isDocument) {
120 foreach ($value as $name => $v) {
121 if ($shape->hasMember($name)) {
122 $this->path[] = $name;
123 $this->dispatch(
124 $shape->getMember($name),
125 isset($value[$name]) ? $value[$name] : null
126 );
127 array_pop($this->path);
128 }
129 }
130 }
131 }
132
133 private function check_list(ListShape $shape, $value)
134 {
135 if (!is_array($value)) {
136 $this->addError('must be an array. Found '
137 . Aws\describe_type($value));
138 return;
139 }
140
141 $this->validateRange($shape, count($value), "list element count");
142
143 $items = $shape->getMember();
144 foreach ($value as $index => $v) {
145 $this->path[] = $index;
146 $this->dispatch($items, $v);
147 array_pop($this->path);
148 }
149 }
150
151 private function check_map(MapShape $shape, $value)
152 {
153 if (!$this->checkAssociativeArray($value)) {
154 return;
155 }
156
157 $values = $shape->getValue();
158 foreach ($value as $key => $v) {
159 $this->path[] = $key;
160 $this->dispatch($values, $v);
161 array_pop($this->path);
162 }
163 }
164
165 private function check_blob(Shape $shape, $value)
166 {
167 static $valid = [
168 'string' => true,
169 'integer' => true,
170 'double' => true,
171 'resource' => true
172 ];
173
174 $type = gettype($value);
175 if (!isset($valid[$type])) {
176 if ($type != 'object' || !method_exists($value, '__toString')) {
177 $this->addError('must be an fopen resource, a '
178 . 'GuzzleHttp\Stream\StreamInterface object, or something '
179 . 'that can be cast to a string. Found '
180 . Aws\describe_type($value));
181 }
182 }
183 }
184
185 private function check_numeric(Shape $shape, $value)
186 {
187 if (!is_numeric($value)) {
188 $this->addError('must be numeric. Found '
189 . Aws\describe_type($value));
190 return;
191 }
192
193 $this->validateRange($shape, $value, "numeric value");
194 }
195
196 private function check_boolean(Shape $shape, $value)
197 {
198 if (!is_bool($value)) {
199 $this->addError('must be a boolean. Found '
200 . Aws\describe_type($value));
201 }
202 }
203
204 private function check_string(Shape $shape, $value)
205 {
206 if ($shape['jsonvalue']) {
207 if (!self::canJsonEncode($value)) {
208 $this->addError('must be a value encodable with \'json_encode\'.'
209 . ' Found ' . Aws\describe_type($value));
210 }
211 return;
212 }
213
214 if (!$this->checkCanString($value)) {
215 $this->addError('must be a string or an object that implements '
216 . '__toString(). Found ' . Aws\describe_type($value));
217 return;
218 }
219
220 $value = isset($value) ? $value : '';
221 $this->validateRange($shape, strlen($value), "string length");
222
223 if ($this->constraints['pattern']) {
224 $pattern = $shape['pattern'];
225 if ($pattern && !preg_match("/$pattern/", $value)) {
226 $this->addError("Pattern /$pattern/ failed to match '$value'");
227 }
228 }
229 }
230
231 private function validateRange(Shape $shape, $length, $descriptor)
232 {
233 if ($this->constraints['min']) {
234 $min = $shape['min'];
235 if ($min && $length < $min) {
236 $this->addError("expected $descriptor to be >= $min, but "
237 . "found $descriptor of $length");
238 }
239 }
240
241 if ($this->constraints['max']) {
242 $max = $shape['max'];
243 if ($max && $length > $max) {
244 $this->addError("expected $descriptor to be <= $max, but "
245 . "found $descriptor of $length");
246 }
247 }
248 }
249
250 private function checkArray($arr)
251 {
252 return array_is_list($arr) || Aws\is_associative($arr);
253 }
254
255 private function checkCanString($value)
256 {
257 static $valid = [
258 'string' => true,
259 'integer' => true,
260 'double' => true,
261 'NULL' => true,
262 ];
263
264 $type = gettype($value);
265
266 return isset($valid[$type]) ||
267 ($type == 'object' && method_exists($value, '__toString'));
268 }
269
270 private function checkAssociativeArray($value)
271 {
272 $isAssociative = false;
273
274 if (is_array($value)) {
275 $expectedIndex = 0;
276 $key = key($value);
277
278 do {
279 $isAssociative = $key !== $expectedIndex++;
280 next($value);
281 $key = key($value);
282 } while (!$isAssociative && null !== $key);
283 }
284
285 if (!$isAssociative) {
286 $this->addError('must be an associative array. Found '
287 . Aws\describe_type($value));
288 return false;
289 }
290
291 return true;
292 }
293
294 private function checkDocumentType($value)
295 {
296 // To allow objects like value, which
297 // can be used within a member which type is `Document`
298 if ($value instanceof stdClass) {
299 $value = (array) $value;
300 }
301
302 if (is_array($value)) {
303 $typeOfFirstKey = gettype(key($value));
304 foreach ($value as $key => $val) {
305 if (!$this->checkDocumentType($val) || gettype($key) != $typeOfFirstKey) {
306 return false;
307 }
308 }
309 return $this->checkArray($value);
310 }
311 return is_null($value)
312 || is_numeric($value)
313 || is_string($value)
314 || is_bool($value);
315 }
316
317 private function checkUnion($value)
318 {
319 if (is_array($value)) {
320 $nonNullCount = 0;
321 foreach ($value as $key => $val) {
322 if (!is_null($val) && !(strpos($key, "@") === 0)) {
323 $nonNullCount++;
324 }
325 }
326 return $nonNullCount == 1;
327 }
328 return !is_null($value);
329 }
330
331 private function addError($message)
332 {
333 $this->errors[] =
334 implode('', array_map(function ($s) { return "[{$s}]"; }, $this->path))
335 . ' '
336 . $message;
337 }
338
339 private function canJsonEncode($data)
340 {
341 return !is_resource($data);
342 }
343 }