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