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 MessageJuggler extends AbstractJuggler |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $message; |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $spacer = Defaults::ONE_SPACE_SYMBOL; |
15
|
|
|
/** @var int */ |
16
|
|
|
protected $erasingLength; |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $frameString; |
19
|
|
|
|
20
|
22 |
|
public function __construct(string $message, int $erasingLength = null, Circular $style = null) |
21
|
|
|
{ |
22
|
22 |
|
$this->style = $style ?? new Circular(['%s',]); |
23
|
22 |
|
$this->updateMessage($message, $erasingLength); |
24
|
22 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $message |
28
|
|
|
* @param null|int $erasingLength |
29
|
|
|
*/ |
30
|
22 |
|
protected function updateMessage(string $message, ?int $erasingLength): void |
31
|
|
|
{ |
32
|
22 |
|
$this->message = $this->refineMessage($message); |
33
|
22 |
|
if (Defaults::EMPTY_STRING === $this->message) { |
34
|
1 |
|
$this->erasingLength = 0; |
35
|
1 |
|
$this->spacer = Defaults::EMPTY_STRING; |
36
|
1 |
|
$this->prefix = Defaults::DEFAULT_PREFIX; |
37
|
1 |
|
$this->suffix = Defaults::EMPTY_STRING; |
38
|
|
|
} else { |
39
|
22 |
|
$erasingLength = $this->refineErasingLen($this->message, $erasingLength); |
40
|
22 |
|
$this->erasingLength = $erasingLength; |
41
|
22 |
|
$this->spacer = Defaults::ONE_SPACE_SYMBOL; |
42
|
22 |
|
$this->prefix = Defaults::DEFAULT_PREFIX; |
43
|
22 |
|
$this->suffix = Defaults::DEFAULT_SUFFIX; |
44
|
|
|
} |
45
|
22 |
|
$this->frameString = |
46
|
22 |
|
$this->prefix . $this->message . $this->suffix . $this->spacer; |
47
|
|
|
|
48
|
22 |
|
$this->currentFrameErasingLength = |
49
|
22 |
|
strlen($this->spacer . $this->prefix . $this->suffix) + $this->erasingLength; |
50
|
22 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $message |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
22 |
|
protected function refineMessage(string $message): string |
57
|
|
|
{ |
58
|
22 |
|
return ucfirst($message); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $message |
63
|
|
|
* @param null|int $erasingLength |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
22 |
|
protected function refineErasingLen(string $message, ?int $erasingLength): int |
67
|
|
|
{ |
68
|
22 |
|
if (null === $erasingLength) { |
69
|
5 |
|
return Calculator::computeErasingLength([$message]); |
70
|
|
|
} |
71
|
21 |
|
return $erasingLength; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $message |
76
|
|
|
* @param null|int $erasingLength |
77
|
|
|
*/ |
78
|
5 |
|
public function setMessage(string $message, ?int $erasingLength = null): void |
79
|
|
|
{ |
80
|
5 |
|
$this->updateMessage($message, $erasingLength); |
81
|
5 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
21 |
|
protected function getCurrentFrame(): string |
87
|
|
|
{ |
88
|
21 |
|
return $this->frameString; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|