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