1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
// 10.03.23 |
5
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Core\A; |
7
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\IFrame; |
9
|
|
|
use AlecRabbit\Spinner\Contract\IFrameCollection; |
10
|
|
|
use AlecRabbit\Spinner\Contract\IFrameCollectionRenderer; |
11
|
|
|
use AlecRabbit\Spinner\Contract\IPattern; |
12
|
|
|
use AlecRabbit\Spinner\Core\Factory\A\ADefaultsAwareClass; |
13
|
|
|
use AlecRabbit\Spinner\Core\FrameCollection; |
14
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgumentException; |
15
|
|
|
use Generator; |
16
|
|
|
use Stringable; |
17
|
|
|
|
18
|
|
|
abstract class AFrameCollectionRenderer extends ADefaultsAwareClass implements IFrameCollectionRenderer |
19
|
|
|
{ |
20
|
|
|
protected ?IPattern $pattern = null; |
21
|
|
|
|
22
|
|
|
/** @inheritdoc */ |
23
|
|
|
public function pattern(IPattern $pattern): IFrameCollectionRenderer |
24
|
|
|
{ |
25
|
|
|
$clone = clone $this; |
26
|
|
|
$clone->pattern = $pattern; |
27
|
|
|
return $clone; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** @inheritdoc */ |
31
|
|
|
public function render(): IFrameCollection |
32
|
|
|
{ |
33
|
|
|
$cb = |
34
|
|
|
/** |
35
|
|
|
* @return Generator<IFrame> |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
function (): Generator { |
39
|
|
|
if (null === $this->pattern) { |
40
|
|
|
throw new InvalidArgumentException('Pattern is not set.'); |
41
|
|
|
} |
42
|
|
|
/** @var IFrame|Stringable|string|int|array<string,int|null> $entry */ |
43
|
|
|
foreach ($this->pattern->getPattern() as $entry) { |
44
|
|
|
if ($entry instanceof IFrame) { |
45
|
|
|
yield $entry; |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
yield $this->create($entry); |
49
|
|
|
} |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
return |
53
|
|
|
new FrameCollection($cb()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
protected function create(Stringable|string|int|array $entry): IFrame |
60
|
|
|
{ |
61
|
|
|
if ($entry instanceof Stringable) { |
|
|
|
|
62
|
|
|
$entry = (string)$entry; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (is_string($entry) || is_int($entry)) { |
|
|
|
|
66
|
|
|
return |
67
|
|
|
$this->createFrame($entry); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->createFromArray($entry); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
abstract protected function createFrame(int|string $entry): IFrame; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws InvalidArgumentException |
80
|
|
|
*/ |
81
|
|
|
abstract protected function createFromArray(array $entry): IFrame; |
82
|
|
|
} |
83
|
|
|
|