|
1
|
|
|
<?php |
|
2
|
|
|
namespace hiapi\Core\Http\Psr15\Middleware; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use hiapi\commands\BulkCommand; |
|
6
|
|
|
use hiapi\Core\Endpoint\Endpoint; |
|
7
|
|
|
use hiapi\endpoints\Module\InOutControl\VO\Collection; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use yii\base\Model; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CommandForEndpointMiddleware takes data from POST or (if it is empty) from GET request, |
|
16
|
|
|
* trims all the values and tries to load them to the command. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class CommandForEndpointMiddleware implements MiddlewareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
26
|
|
|
{ |
|
27
|
|
|
$data = array_merge($request->getParsedBody() ?: $request->getQueryParams()); |
|
28
|
|
|
array_walk_recursive($data, static function (&$value) { |
|
29
|
|
|
if (\is_string($value)) { |
|
30
|
|
|
$value = trim($value); |
|
31
|
|
|
} |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
$command = $this->createCommand($request); |
|
35
|
|
|
$successLoad = $command->load($data, ''); |
|
36
|
|
|
if (!$successLoad && !empty($data)) { |
|
37
|
|
|
// TODO: specific exception |
|
38
|
|
|
throw new Exception('Failed to load command'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $handler->handle( |
|
42
|
|
|
$request->withAttribute(self::class, $command) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function createCommand(ServerRequestInterface $request): Model |
|
47
|
|
|
{ |
|
48
|
|
|
/** @var Endpoint $endpoint */ |
|
49
|
|
|
$endpoint = $request->getAttribute(ResolveEndpointMiddleware::class); |
|
50
|
|
|
|
|
51
|
|
|
$inputType = $endpoint->getInputType(); |
|
52
|
|
|
if ($inputType instanceof Collection) { |
|
53
|
|
|
$command = BulkCommand::of($inputType->getEntriesClass()); |
|
54
|
|
|
} else { |
|
55
|
|
|
$command = new $inputType(); |
|
56
|
|
|
} |
|
57
|
|
|
if (!$command instanceof Model) { |
|
|
|
|
|
|
58
|
|
|
// TODO: specific exception |
|
59
|
|
|
throw new Exception('This middleware can load only commands of Model class'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $command; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.