Passed
Push — master ( 823aee...b9b37f )
by Alec
13:15 queued 12s
created

AFrameRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\IFrameRenderer;
11
use AlecRabbit\Spinner\Core\Factory\A\ADefaultsAwareClass;
12
use AlecRabbit\Spinner\Core\FrameCollection;
13
use AlecRabbit\Spinner\Core\Pattern\Contract\IPattern;
14
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
15
use Generator;
16
use Stringable;
17
18
abstract class AFrameRenderer extends ADefaultsAwareClass implements IFrameRenderer
19
{
20
    public function __construct(
21
        protected IPattern $pattern
22
    ) {
23
    }
24
25
    /**
26
     * @throws InvalidArgumentException
27
     */
28
    public function render(): IFrameCollection
29
    {
30
        $cb =
31
            /**
32
             * @return Generator<IFrame>
33
             * @throws InvalidArgumentException
34
             */
35
            function (): Generator {
36
                /** @var IFrame|Stringable|string|int|array<string,int|null> $entry */
37
                foreach ($this->pattern->getPattern() as $entry) {
38
                    if ($entry instanceof IFrame) {
39
                        yield $entry;
40
                        continue;
41
                    }
42
                    yield $this->create($entry);
43
                }
44
            };
45
46
        return
47
            new FrameCollection($cb());
48
    }
49
50
    /**
51
     * @throws InvalidArgumentException
52
     */
53
    protected function create(Stringable|string|int|array $entry): IFrame
54
    {
55
        if ($entry instanceof Stringable) {
0 ignored issues
show
introduced by
$entry is never a sub-type of Stringable.
Loading history...
56
            $entry = (string)$entry;
57
        }
58
59
        if (is_string($entry) || is_int($entry)) {
0 ignored issues
show
introduced by
The condition is_int($entry) is always false.
Loading history...
60
            return
61
                $this->createFrame($entry);
62
        }
63
64
        return $this->createFromArray($entry);
65
    }
66
67
    /**
68
     * @throws InvalidArgumentException
69
     */
70
    abstract protected function createFrame(int|string $entry): IFrame;
71
72
    /**
73
     * @throws InvalidArgumentException
74
     */
75
    abstract protected function createFromArray(array $entry): IFrame;
76
}
77