|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
// 10.03.23 |
|
5
|
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
7
|
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\IFrame; |
|
9
|
|
|
use AlecRabbit\Spinner\Core\A\AFrameRenderer; |
|
10
|
|
|
use AlecRabbit\Spinner\Core\Factory\FrameFactory; |
|
11
|
|
|
use AlecRabbit\Spinner\Exception\InvalidArgumentException; |
|
12
|
|
|
use Stringable; |
|
13
|
|
|
|
|
14
|
|
|
use function is_string; |
|
15
|
|
|
|
|
16
|
|
|
final class FrameRenderer extends AFrameRenderer |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
protected function createFromInt(int $entry): IFrame |
|
20
|
|
|
{ |
|
21
|
|
|
return |
|
22
|
|
|
$this->createFromString((string)$entry); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function createFromString(string $entry): IFrame |
|
26
|
|
|
{ |
|
27
|
|
|
return FrameFactory::create($entry, WidthDeterminer::determine($entry)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @throws InvalidArgumentException |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function createFromArray(array $entry): IFrame |
|
34
|
|
|
{ |
|
35
|
|
|
self::assertEntryArray($entry); |
|
36
|
|
|
return |
|
37
|
|
|
FrameFactory::create( |
|
38
|
|
|
(string)$entry[0], |
|
39
|
|
|
self::refineNullableInt($entry[1]) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static function assertEntryArray(array $entry): void |
|
47
|
|
|
{ |
|
48
|
|
|
// array size should be 2 |
|
49
|
|
|
$size = count($entry); |
|
50
|
|
|
if (2 !== $size) { |
|
51
|
|
|
throw new InvalidArgumentException( |
|
52
|
|
|
sprintf( |
|
53
|
|
|
'Entry array size should be 2, %d given', |
|
54
|
|
|
$size |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
// first element should be string |
|
59
|
|
|
$first = $entry[0]; |
|
60
|
|
|
if (!is_string($first) && !($first instanceof Stringable)) { |
|
61
|
|
|
throw new InvalidArgumentException( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'First element of entry array should be string|Stringable, %s given.', |
|
64
|
|
|
get_debug_type($first) |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
// second element should be non-negative integer |
|
69
|
|
|
$second = $entry[1] ?? null; |
|
70
|
|
|
if (null === $second) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
if (!is_int($second) || $second < 0) { |
|
74
|
|
|
throw new InvalidArgumentException( |
|
75
|
|
|
sprintf( |
|
76
|
|
|
'Second element of entry array should be non-negative integer, %s given.', |
|
77
|
|
|
get_debug_type($second) |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected static function refineNullableInt(mixed $value): ?int |
|
84
|
|
|
{ |
|
85
|
|
|
if (null === $value) { |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
return (int)$value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function createFrame(int|string $entry): IFrame |
|
92
|
|
|
{ |
|
93
|
|
|
return FrameFactory::create((string)$entry, WidthDeterminer::determine((string)$entry)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|