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\Jugglers\Contracts\JugglerInterface; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
8
|
|
|
|
9
|
|
|
class MessageJuggler implements JugglerInterface |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $messagePrefix = Defaults::EMPTY_STRING; |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $message; |
15
|
|
|
/** @var string */ |
16
|
|
|
protected $messageSuffix = Defaults::EMPTY_STRING; |
17
|
|
|
/** @var string */ |
18
|
|
|
protected $spacer = Defaults::ONE_SPACE_SYMBOL; |
19
|
|
|
/** @var int */ |
20
|
|
|
protected $erasingLength; |
21
|
|
|
/** @var string */ |
22
|
|
|
protected $frameString; |
23
|
|
|
/** @var int */ |
24
|
|
|
protected $frameStringErasingLength; |
25
|
|
|
/** @var int */ |
26
|
|
|
protected $erasingLengthDelta; |
27
|
|
|
|
28
|
22 |
|
public function __construct(string $message, int $erasingLength = null) |
29
|
|
|
{ |
30
|
22 |
|
$this->updateMessage($message, $erasingLength); |
31
|
22 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $message |
35
|
|
|
* @param null|int $erasingLength |
36
|
|
|
*/ |
37
|
22 |
|
protected function updateMessage(string $message, ?int $erasingLength): void |
38
|
|
|
{ |
39
|
22 |
|
$this->message = $this->refineMessage($message); |
40
|
22 |
|
if (Defaults::EMPTY_STRING === $this->message) { |
41
|
1 |
|
$this->erasingLengthDelta = $this->getMessageFullLength(); |
42
|
1 |
|
$this->erasingLength = 0; |
43
|
1 |
|
$this->spacer = Defaults::EMPTY_STRING; |
44
|
1 |
|
$this->messagePrefix = Defaults::DEFAULT_PREFIX; |
45
|
1 |
|
$this->messageSuffix = Defaults::EMPTY_STRING; |
46
|
|
|
} else { |
47
|
22 |
|
$erasingLength = $this->refineErasingLen($this->message, $erasingLength); |
48
|
22 |
|
$this->erasingLengthDelta = $this->getMessageFullLength() - $erasingLength; |
49
|
22 |
|
$this->erasingLength = $erasingLength; |
50
|
22 |
|
$this->spacer = Defaults::ONE_SPACE_SYMBOL; |
51
|
22 |
|
$this->messagePrefix = Defaults::DEFAULT_PREFIX; |
52
|
22 |
|
$this->messageSuffix = Defaults::DEFAULT_SUFFIX; |
53
|
|
|
} |
54
|
22 |
|
$this->frameString = |
55
|
22 |
|
$this->messagePrefix . $this->message . $this->messageSuffix . $this->spacer; |
56
|
|
|
|
57
|
22 |
|
$this->frameStringErasingLength = |
58
|
22 |
|
strlen($this->spacer . $this->messagePrefix . $this->messageSuffix) + $this->erasingLength; |
59
|
|
|
// strlen($this->spacer) + $this->getMessageFullLength(); |
60
|
22 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
22 |
|
protected function getMessageFullLength(): int |
66
|
|
|
{ |
67
|
22 |
|
return strlen($this->messagePrefix) + $this->erasingLength + strlen($this->messageSuffix); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $message |
72
|
|
|
* @param null|int $erasingLength |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
22 |
|
protected function refineErasingLen(string $message, ?int $erasingLength): int |
76
|
|
|
{ |
77
|
22 |
|
if (null === $erasingLength) { |
78
|
5 |
|
return Calculator::computeErasingLength([$message]); |
79
|
|
|
} |
80
|
21 |
|
return $erasingLength; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $message |
85
|
|
|
* @param null|int $erasingLength |
86
|
|
|
*/ |
87
|
5 |
|
public function setMessage(string $message, ?int $erasingLength = null): void |
88
|
|
|
{ |
89
|
5 |
|
$this->updateMessage($message, $erasingLength); |
90
|
5 |
|
} |
91
|
|
|
|
92
|
|
|
/** {@inheritDoc} */ |
93
|
21 |
|
public function getFrame(): string |
94
|
|
|
{ |
95
|
21 |
|
return $this->frameString; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** {@inheritDoc} */ |
99
|
21 |
|
public function getFrameErasingLength(): int |
100
|
|
|
{ |
101
|
21 |
|
return $this->frameStringErasingLength; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
21 |
|
public function getErasingLengthDelta(): int |
108
|
|
|
{ |
109
|
21 |
|
return $this->erasingLengthDelta; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $message |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
22 |
|
protected function refineMessage(string $message): string |
117
|
|
|
{ |
118
|
22 |
|
return ucfirst($message); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|