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