|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace hiapi\Core\commands; |
|
5
|
|
|
|
|
6
|
|
|
use hiapi\commands\BulkCommand; |
|
7
|
|
|
use hiapi\Core\Endpoint\Endpoint; |
|
8
|
|
|
use hiapi\endpoints\Module\InOutControl\VO\Collection; |
|
9
|
|
|
use hiapi\exceptions\ConfigurationException; |
|
10
|
|
|
use hiapi\exceptions\HiapiException; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use yii\base\Model; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Creates command by given data: name and request. |
|
16
|
|
|
* - takes data from POST or (if it is empty) from GET request, |
|
17
|
|
|
* - trims all the values and tries to load them to the command. |
|
18
|
|
|
*/ |
|
19
|
|
|
class CommandFactory |
|
20
|
|
|
{ |
|
21
|
|
|
public function createByEndpoint(Endpoint $endpoint, ServerRequestInterface $request): Model |
|
22
|
|
|
{ |
|
23
|
|
|
$data = $this->extractData($request); |
|
24
|
|
|
$command = $this->createCommand($endpoint); |
|
25
|
|
|
$successLoad = $command->load($data, ''); |
|
26
|
|
|
if (!$successLoad && !empty($data)) { |
|
27
|
|
|
throw new HiapiException('Failed to load command data'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $command; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function createCommand(Endpoint $endpoint): Model |
|
34
|
|
|
{ |
|
35
|
|
|
$inputType = $endpoint->inputType; |
|
36
|
|
|
if ($inputType instanceof Collection) { |
|
|
|
|
|
|
37
|
|
|
$command = BulkCommand::of($inputType->getEntriesClass()); |
|
38
|
|
|
} else { |
|
39
|
|
|
$command = new $inputType(); |
|
40
|
|
|
} |
|
41
|
|
|
if (!$command instanceof Model) { |
|
|
|
|
|
|
42
|
|
|
throw new ConfigurationException('This middleware can load only commands of Model class'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $command; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function extractData(ServerRequestInterface $request): array |
|
49
|
|
|
{ |
|
50
|
|
|
$data = array_merge($request->getParsedBody(), $request->getQueryParams()); |
|
51
|
|
|
array_walk_recursive($data, static function (&$value) { |
|
52
|
|
|
if (\is_string($value)) { |
|
53
|
|
|
$value = trim($value); |
|
54
|
|
|
} |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
return $data; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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.