1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* This file is part of the BEAR.SsrModule package. |
6
|
|
|
* |
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
8
|
|
|
*/ |
9
|
|
|
namespace BEAR\SsrModule; |
10
|
|
|
|
11
|
|
|
use BEAR\Resource\RenderInterface; |
12
|
|
|
use BEAR\Resource\ResourceObject; |
13
|
|
|
use BEAR\SsrModule\Exception\MetaKeyNotExistsException; |
14
|
|
|
use BEAR\SsrModule\Exception\StatusKeyNotExistsException; |
15
|
|
|
use Koriym\Baracoa\BaracoaInterface; |
16
|
|
|
|
17
|
|
|
final class Ssr implements RenderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var BaracoaInterface |
21
|
|
|
*/ |
22
|
|
|
private $baracoa; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $appName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $stateKeys; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $metaKeys; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param BaracoaInterface $baracoa |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct(BaracoaInterface $baracoa, string $appName, array $stateKeys = [], array $metasKeys = []) |
43
|
|
|
{ |
44
|
4 |
|
$this->baracoa = $baracoa; |
45
|
4 |
|
$this->appName = $appName; |
46
|
4 |
|
$this->stateKeys = $stateKeys; |
47
|
4 |
|
$this->metaKeys = $metasKeys; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
4 |
|
public function render(ResourceObject $ro) |
51
|
|
|
{ |
52
|
4 |
|
$state = $this->filter($this->stateKeys, (array) $ro->body, StatusKeyNotExistsException::class); |
53
|
3 |
|
$metas = $this->filter($this->metaKeys, (array) $ro->body, MetaKeyNotExistsException::class); |
54
|
2 |
|
$html = $this->baracoa->render($this->appName, $state, $metas); |
55
|
1 |
|
$ro->view = $html; |
56
|
|
|
|
57
|
1 |
|
return $html; |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
private function filter(array $keys, array $body, string $exception) : array |
61
|
|
|
{ |
62
|
4 |
|
if ($keys === ['*']) { |
63
|
1 |
|
return $body; |
64
|
|
|
} |
65
|
4 |
|
$errorKeys = array_diff(array_values($keys), array_keys($body)); |
66
|
4 |
|
if ($errorKeys) { |
|
|
|
|
67
|
2 |
|
throw new $exception(implode(',', $errorKeys)); |
68
|
|
|
} |
69
|
3 |
|
$filterd = array_filter((array) $body, function ($key) use ($keys) { |
70
|
2 |
|
return in_array($key, $keys, true); |
71
|
3 |
|
}, ARRAY_FILTER_USE_KEY); |
72
|
|
|
|
73
|
3 |
|
return $filterd; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.