|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Contracts\SettingsInterface; |
|
6
|
|
|
use AlecRabbit\Spinner\Core\Contracts\StylesInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Settings |
|
10
|
|
|
* |
|
11
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
|
12
|
|
|
*/ |
|
13
|
|
|
class Settings implements SettingsInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var float */ |
|
16
|
|
|
protected $interval; |
|
17
|
|
|
/** @var int */ |
|
18
|
|
|
protected $erasingShift; |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $message; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $messagePrefix; |
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $messageSuffix; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
protected $inlinePaddingStr; |
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
protected $frames; |
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
protected $styles; |
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
protected $messageErasingLen; |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
protected $spacer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Settings constructor. |
|
38
|
|
|
*/ |
|
39
|
29 |
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
29 |
|
$this->initializeWithDefaults(); |
|
42
|
29 |
|
} |
|
43
|
|
|
|
|
44
|
29 |
|
protected function initializeWithDefaults(): SettingsInterface |
|
45
|
|
|
{ |
|
46
|
|
|
return |
|
47
|
|
|
$this |
|
48
|
29 |
|
->setMessageSuffix(null) |
|
49
|
29 |
|
->setSpacer(null) |
|
50
|
29 |
|
->setFrames(null) |
|
51
|
29 |
|
->setStyles(null) |
|
52
|
29 |
|
->setMessage(null) |
|
53
|
29 |
|
->setMessagePrefix(null) |
|
54
|
29 |
|
->setInterval(null) |
|
55
|
29 |
|
->setInlinePaddingStr(null); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** {@inheritDoc} */ |
|
59
|
28 |
|
public function getInterval(): float |
|
60
|
|
|
{ |
|
61
|
28 |
|
return $this->interval; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** {@inheritDoc} */ |
|
65
|
29 |
|
public function setInterval(?float $interval): SettingsInterface |
|
66
|
|
|
{ |
|
67
|
29 |
|
$this->interval = $interval ?? SettingsInterface::DEFAULT_INTERVAL; |
|
68
|
29 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
// |
|
71
|
|
|
|
|
72
|
|
|
/** {@inheritDoc} */ |
|
73
|
28 |
|
public function getErasingShift(): int |
|
74
|
|
|
{ |
|
75
|
28 |
|
return $this->erasingShift; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** {@inheritDoc} */ |
|
79
|
28 |
|
public function getMessage(): string |
|
80
|
|
|
{ |
|
81
|
28 |
|
return $this->message; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** {@inheritDoc} */ |
|
85
|
29 |
|
public function setMessage(?string $message, ?int $erasingLen = null): SettingsInterface |
|
86
|
|
|
{ |
|
87
|
29 |
|
$this->message = $message ?? SettingsInterface::EMPTY; |
|
88
|
29 |
|
$this->messageErasingLen = $this->refineErasingLen($message, $erasingLen); |
|
89
|
29 |
|
if (SettingsInterface::EMPTY === $this->message) { |
|
90
|
29 |
|
$this->setMessageSuffix(SettingsInterface::EMPTY); |
|
91
|
|
|
} else { |
|
92
|
23 |
|
$this->setMessageSuffix(SettingsInterface::DEFAULT_SUFFIX); |
|
93
|
|
|
} |
|
94
|
29 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param null|string $string |
|
99
|
|
|
* @param null|int $erasingLen |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
29 |
|
protected function refineErasingLen(?string $string, ?int $erasingLen): int |
|
103
|
|
|
{ |
|
104
|
29 |
|
if (null === $erasingLen) { |
|
105
|
29 |
|
return $this->computeErasingLen([$string]); |
|
106
|
|
|
} |
|
107
|
1 |
|
return $erasingLen; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $strings |
|
112
|
|
|
* @return int |
|
113
|
|
|
*/ |
|
114
|
29 |
|
protected function computeErasingLen(array $strings): int |
|
115
|
|
|
{ |
|
116
|
29 |
|
if (empty($strings)) { |
|
117
|
29 |
|
return 0; |
|
118
|
|
|
} |
|
119
|
29 |
|
return $this->compErasingLen($strings); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
29 |
|
private function compErasingLen(array $strings): int |
|
123
|
|
|
{ |
|
124
|
|
|
// TODO check if all elements have the same erasingLen |
|
125
|
|
|
|
|
126
|
29 |
|
if (null === $symbol = $strings[0]) { |
|
127
|
29 |
|
return 0; |
|
128
|
|
|
} |
|
129
|
24 |
|
$mbSymbolLen = mb_strlen($symbol); |
|
130
|
24 |
|
$oneCharLen = strlen($symbol) / $mbSymbolLen; |
|
131
|
24 |
|
if (4 === $oneCharLen) { |
|
132
|
3 |
|
return 2 * $mbSymbolLen; |
|
133
|
|
|
} |
|
134
|
24 |
|
return 1 * $mbSymbolLen; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** {@inheritDoc} */ |
|
138
|
28 |
|
public function getMessagePrefix(): string |
|
139
|
|
|
{ |
|
140
|
28 |
|
return $this->messagePrefix; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** {@inheritDoc} */ |
|
144
|
29 |
|
public function setMessagePrefix(?string $messagePrefix): SettingsInterface |
|
145
|
|
|
{ |
|
146
|
29 |
|
$this->messagePrefix = $messagePrefix ?? SettingsInterface::ONE_SPACE_SYMBOL; |
|
147
|
29 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** {@inheritDoc} */ |
|
151
|
28 |
|
public function getMessageSuffix(): string |
|
152
|
|
|
{ |
|
153
|
28 |
|
return $this->messageSuffix; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** {@inheritDoc} */ |
|
157
|
29 |
|
public function setMessageSuffix(?string $messageSuffix): SettingsInterface |
|
158
|
|
|
{ |
|
159
|
29 |
|
$this->messageSuffix = $messageSuffix ?? SettingsInterface::DEFAULT_SUFFIX; |
|
160
|
29 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** {@inheritDoc} */ |
|
164
|
28 |
|
public function getInlinePaddingStr(): string |
|
165
|
|
|
{ |
|
166
|
28 |
|
return $this->inlinePaddingStr; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** {@inheritDoc} */ |
|
170
|
29 |
|
public function setInlinePaddingStr(?string $inlinePaddingStr): SettingsInterface |
|
171
|
|
|
{ |
|
172
|
29 |
|
$this->inlinePaddingStr = $inlinePaddingStr ?? SettingsInterface::EMPTY; |
|
173
|
29 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** {@inheritDoc} */ |
|
177
|
28 |
|
public function getFrames(): array |
|
178
|
|
|
{ |
|
179
|
28 |
|
return $this->frames; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** {@inheritDoc} */ |
|
183
|
29 |
|
public function setFrames(?array $symbols): SettingsInterface |
|
184
|
|
|
{ |
|
185
|
29 |
|
if (null !== $symbols && count($symbols) > SettingsInterface::MAX_FRAMES_COUNT) { |
|
186
|
1 |
|
throw new \InvalidArgumentException( |
|
187
|
1 |
|
sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded.', SettingsInterface::MAX_FRAMES_COUNT) |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
29 |
|
$this->frames = $symbols ?? static::DEFAULT_FRAMES; |
|
191
|
29 |
|
$this->erasingShift = $this->computeErasingLen($this->frames); |
|
192
|
29 |
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** {@inheritDoc} */ |
|
196
|
28 |
|
public function getStyles(): array |
|
197
|
|
|
{ |
|
198
|
28 |
|
return $this->styles; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** {@inheritDoc} */ |
|
202
|
29 |
|
public function setStyles(?array $styles): SettingsInterface |
|
203
|
|
|
{ |
|
204
|
29 |
|
$this->styles = $this->mergeStyles(StylesInterface::DEFAULT_STYLES, $styles ?? []); |
|
205
|
29 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param array $defaultStyles |
|
210
|
|
|
* @param array $styles |
|
211
|
|
|
* @return array |
|
212
|
|
|
* todo move to another class? |
|
213
|
|
|
*/ |
|
214
|
29 |
|
protected function mergeStyles(array $defaultStyles, array $styles): array |
|
215
|
|
|
{ |
|
216
|
29 |
|
foreach ($defaultStyles as $key => $defaults) { |
|
217
|
29 |
|
if (\array_key_exists($key, $styles)) { |
|
218
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
|
219
|
25 |
|
$defaultStyles[$key] = array_merge($defaultStyles[$key], $styles[$key]); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
29 |
|
return $defaultStyles; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
2 |
|
public function merge(?SettingsInterface $settings): SettingsInterface |
|
226
|
|
|
{ |
|
227
|
2 |
|
if ($settings instanceof SettingsInterface) { |
|
228
|
2 |
|
$this->interval = $settings->getInterval(); |
|
229
|
2 |
|
$this->erasingShift = $settings->getErasingShift(); |
|
230
|
2 |
|
$this->message = $settings->getMessage(); |
|
231
|
2 |
|
$this->messagePrefix = $settings->getMessagePrefix(); |
|
232
|
2 |
|
$this->messageSuffix = $settings->getMessageSuffix(); |
|
233
|
2 |
|
$this->inlinePaddingStr = $settings->getInlinePaddingStr(); |
|
234
|
2 |
|
$this->frames = $settings->getFrames(); |
|
235
|
2 |
|
$this->styles = $settings->getStyles(); |
|
236
|
2 |
|
$this->messageErasingLen = $settings->getMessageErasingLen(); |
|
237
|
2 |
|
$this->spacer = $settings->getSpacer(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
2 |
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return int |
|
245
|
|
|
*/ |
|
246
|
28 |
|
public function getMessageErasingLen(): int |
|
247
|
|
|
{ |
|
248
|
28 |
|
return $this->messageErasingLen; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
28 |
|
public function getSpacer(): string |
|
252
|
|
|
{ |
|
253
|
28 |
|
return $this->spacer; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
29 |
|
public function setSpacer(?string $spacer): SettingsInterface |
|
257
|
|
|
{ |
|
258
|
29 |
|
$this->spacer = $spacer ?? SettingsInterface::EMPTY; |
|
259
|
29 |
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|