| 1 |
efrain |
1 |
<?php
|
|
|
2 |
namespace Aws;
|
|
|
3 |
|
|
|
4 |
use Aws\Api\Service;
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* Validates the required input parameters of commands are non empty
|
|
|
8 |
*
|
|
|
9 |
* @internal
|
|
|
10 |
*/
|
|
|
11 |
class InputValidationMiddleware
|
|
|
12 |
{
|
|
|
13 |
|
|
|
14 |
/** @var callable */
|
|
|
15 |
private $nextHandler;
|
|
|
16 |
|
|
|
17 |
/** @var array */
|
|
|
18 |
private $mandatoryAttributeList;
|
|
|
19 |
|
|
|
20 |
/** @var Service */
|
|
|
21 |
private $service;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Create a middleware wrapper function.
|
|
|
25 |
*
|
|
|
26 |
* @param Service $service
|
|
|
27 |
* @param array $mandatoryAttributeList
|
|
|
28 |
* @return callable */
|
|
|
29 |
public static function wrap(Service $service, $mandatoryAttributeList) {
|
|
|
30 |
if (!is_array($mandatoryAttributeList) ||
|
|
|
31 |
array_filter($mandatoryAttributeList, 'is_string') !== $mandatoryAttributeList
|
|
|
32 |
) {
|
|
|
33 |
throw new \InvalidArgumentException(
|
|
|
34 |
"The mandatory attribute list must be an array of strings"
|
|
|
35 |
);
|
|
|
36 |
}
|
|
|
37 |
return function (callable $handler) use ($service, $mandatoryAttributeList) {
|
|
|
38 |
return new self($handler, $service, $mandatoryAttributeList);
|
|
|
39 |
};
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function __construct(
|
|
|
43 |
callable $nextHandler,
|
|
|
44 |
Service $service,
|
|
|
45 |
$mandatoryAttributeList
|
|
|
46 |
) {
|
|
|
47 |
$this->service = $service;
|
|
|
48 |
$this->nextHandler = $nextHandler;
|
|
|
49 |
$this->mandatoryAttributeList = $mandatoryAttributeList;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public function __invoke(CommandInterface $cmd) {
|
|
|
53 |
$nextHandler = $this->nextHandler;
|
|
|
54 |
$op = $this->service->getOperation($cmd->getName())->toArray();
|
|
|
55 |
if (!empty($op['input']['shape'])) {
|
|
|
56 |
$service = $this->service->toArray();
|
|
|
57 |
if (!empty($input = $service['shapes'][$op['input']['shape']])) {
|
|
|
58 |
if (!empty($input['required'])) {
|
|
|
59 |
foreach ($input['required'] as $key => $member) {
|
|
|
60 |
if (in_array($member, $this->mandatoryAttributeList)) {
|
|
|
61 |
$argument = is_string($cmd[$member]) ? trim($cmd[$member]) : $cmd[$member];
|
|
|
62 |
if ($argument === '' || $argument === null) {
|
|
|
63 |
$commandName = $cmd->getName();
|
|
|
64 |
throw new \InvalidArgumentException(
|
|
|
65 |
"The {$commandName} operation requires non-empty parameter: {$member}"
|
|
|
66 |
);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
return $nextHandler($cmd);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
}
|