|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hiapi\Core\Http\Psr15\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use hiapi\Core\Endpoint\Endpoint; |
|
7
|
|
|
use hiapi\Core\Endpoint\EndpointProcessor; |
|
8
|
|
|
use hiapi\Core\Http\Psr7\Response\FatResponse; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
13
|
|
|
use yii\base\Arrayable; |
|
14
|
|
|
use yii\base\Model; |
|
15
|
|
|
|
|
16
|
|
|
class RunEndpointBusMiddleware implements MiddlewareInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var EndpointProcessor |
|
20
|
|
|
*/ |
|
21
|
|
|
private $endpointProcessor; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(EndpointProcessor $endpointProcessor) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->endpointProcessor = $endpointProcessor; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var Model $command */ |
|
34
|
|
|
$command = $request->getAttribute(CommandForEndpointMiddleware::class); |
|
35
|
|
|
/** @var Endpoint $endpoint */ |
|
36
|
|
|
$endpoint = $request->getAttribute(ResolveEndpointMiddleware::class); |
|
37
|
|
|
|
|
38
|
|
|
$result = $this->endpointProcessor->__invoke($command, $endpoint); |
|
39
|
|
|
|
|
40
|
|
|
$transformedResult = $this->transformResult($result); |
|
41
|
|
|
return FatResponse::create($transformedResult, $request); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* TODO: Re-implement as a ResourceTransformer, that can serialize Result to an Http\Response |
|
46
|
|
|
* TODO: It should be useful to serialize file to StreamResponse, or search result to Reponse with |
|
47
|
|
|
* TODO: an appropriate headers. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array|ArrayCollection|object|mixed $result |
|
50
|
|
|
* @return array|string|null|boolean |
|
51
|
|
|
*/ |
|
52
|
|
|
private function transformResult($result) |
|
53
|
|
|
{ |
|
54
|
|
|
if ($result instanceof ResponseInterface) { |
|
55
|
|
|
return $result; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($result instanceof ArrayCollection) { |
|
59
|
|
|
return array_map(function ($item) { |
|
60
|
|
|
return $this->transformResult($item); |
|
61
|
|
|
}, $result->toArray()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($result instanceof Arrayable) { |
|
|
|
|
|
|
65
|
|
|
return $result->toArray(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.