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