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