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