PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / InputValidationMiddleware.php

InputValidationMiddleware.php in Media Cloud Sync 1.2.12, at includes/sdk/s3/Aws/InputValidationMiddleware.php

63 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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