|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core\Jugglers; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Circular; |
|
6
|
|
|
use AlecRabbit\Spinner\Core\Calculator; |
|
7
|
|
|
use AlecRabbit\Spinner\Core\Coloring\Style; |
|
8
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
|
9
|
|
|
use AlecRabbit\Spinner\Settings\Settings; |
|
10
|
|
|
|
|
11
|
|
|
class FrameJuggler extends AbstractJuggler |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Circular */ |
|
14
|
|
|
protected $frames; |
|
15
|
|
|
|
|
16
|
19 |
|
public function __construct(Settings $settings, Style $style) |
|
17
|
|
|
{ |
|
18
|
19 |
|
$frames = $settings->getFrames(); |
|
19
|
19 |
|
$this->assertFrames($frames); |
|
20
|
19 |
|
$this->frames = new Circular($frames); |
|
21
|
19 |
|
$this->init($style); |
|
22
|
19 |
|
$this->currentFrameErasingLength = |
|
23
|
19 |
|
Calculator::computeErasingLength($frames) + strlen($this->spacer) + $this->formatErasingShift; |
|
24
|
19 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $frames |
|
28
|
|
|
*/ |
|
29
|
19 |
|
protected function assertFrames(array $frames): void |
|
30
|
|
|
{ |
|
31
|
19 |
|
if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) { |
|
32
|
|
|
throw new \InvalidArgumentException( |
|
33
|
|
|
sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
19 |
|
foreach ($frames as $frame) { |
|
37
|
19 |
|
$this->assertFrame($frame); |
|
38
|
|
|
} |
|
39
|
19 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param mixed $frame |
|
43
|
|
|
*/ |
|
44
|
19 |
|
protected function assertFrame($frame): void |
|
45
|
|
|
{ |
|
46
|
19 |
|
if (!\is_string($frame)) { |
|
47
|
|
|
throw new \InvalidArgumentException('All frames should be of string type.'); |
|
48
|
|
|
} |
|
49
|
19 |
|
if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) { |
|
50
|
|
|
throw new \InvalidArgumentException( |
|
51
|
|
|
sprintf( |
|
52
|
|
|
'Single frame max length [%s] exceeded [%s]', |
|
53
|
|
|
Defaults::MAX_FRAME_LENGTH, |
|
54
|
|
|
$length |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
19 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
18 |
|
protected function getCurrentFrame(): string |
|
64
|
|
|
{ |
|
65
|
18 |
|
return $this->frames->value(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|