1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core\Jugglers; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Calculator; |
6
|
|
|
use AlecRabbit\Spinner\Core\Coloring\Style; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
8
|
|
|
use AlecRabbit\Spinner\Settings\Settings; |
9
|
|
|
|
10
|
|
|
class MessageJuggler extends AbstractJuggler |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $message; |
14
|
|
|
/** @var int */ |
15
|
|
|
protected $erasingLength; |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $frameString; |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $currentSuffix; |
20
|
|
|
/** @var string */ |
21
|
|
|
protected $suffixFromSettings; |
22
|
|
|
|
23
|
|
|
/** {@inheritDoc} */ |
24
|
22 |
|
public function __construct(Settings $settings, Style $style) |
25
|
|
|
{ |
26
|
22 |
|
$this->init($style); |
27
|
22 |
|
$this->suffixFromSettings = $settings->getMessageSuffix(); |
28
|
22 |
|
$this->updateMessage( |
29
|
22 |
|
$settings->getMessage(), |
30
|
22 |
|
$settings->getMessageErasingLength() |
31
|
|
|
); |
32
|
22 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param null|string $message |
36
|
|
|
* @param null|int $erasingLength |
37
|
|
|
*/ |
38
|
22 |
|
protected function updateMessage(?string $message, ?int $erasingLength): void |
39
|
|
|
{ |
40
|
22 |
|
$message = $message ?? Defaults::EMPTY_STRING; |
41
|
22 |
|
$this->message = $this->refineMessage($message); |
42
|
22 |
|
if (Defaults::EMPTY_STRING === $this->message) { |
43
|
|
|
$this->erasingLength = 0; |
44
|
|
|
$this->currentSuffix = Defaults::EMPTY_STRING; |
45
|
|
|
} else { |
46
|
22 |
|
$erasingLength = Calculator::refineErasingLen($this->message, $erasingLength); |
47
|
22 |
|
$this->erasingLength = $erasingLength; |
48
|
22 |
|
$this->currentSuffix = $this->suffixFromSettings; |
49
|
|
|
} |
50
|
22 |
|
$this->frameString = |
51
|
22 |
|
$this->message . $this->currentSuffix; |
52
|
|
|
|
53
|
22 |
|
$this->currentFrameErasingLength = |
54
|
22 |
|
strlen($this->currentSuffix . $this->spacer) + $this->erasingLength + $this->formatErasingShift; |
55
|
22 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $message |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
22 |
|
protected function refineMessage(string $message): string |
62
|
|
|
{ |
63
|
22 |
|
return ucfirst($message); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $message |
68
|
|
|
* @param null|int $erasingLength |
69
|
|
|
*/ |
70
|
4 |
|
public function setMessage(string $message, ?int $erasingLength = null): void |
71
|
|
|
{ |
72
|
4 |
|
$this->updateMessage($message, $erasingLength); |
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritDoc} */ |
76
|
21 |
|
protected function getCurrentFrame(): string |
77
|
|
|
{ |
78
|
21 |
|
return $this->frameString; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|