1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Settings; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
6
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\S; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\SettingsInterface; |
8
|
|
|
|
9
|
|
|
class Settings implements SettingsInterface |
10
|
|
|
{ |
11
|
|
|
/** @var Property[] */ |
12
|
|
|
protected $properties; |
13
|
|
|
|
14
|
30 |
|
public function __construct() |
15
|
|
|
{ |
16
|
30 |
|
$this->properties = $this->initialize(); |
17
|
30 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return Property[] |
21
|
|
|
*/ |
22
|
30 |
|
private function initialize(): array |
23
|
|
|
{ |
24
|
30 |
|
$properties = []; |
25
|
30 |
|
foreach (Defaults::DEFAULT_SETTINGS as $name => $value) { |
26
|
30 |
|
$properties[$name] = new Property($value); |
27
|
|
|
} |
28
|
30 |
|
return $properties; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** {@inheritDoc} */ |
32
|
28 |
|
public function getInterval(): float |
33
|
|
|
{ |
34
|
|
|
return |
35
|
28 |
|
$this->properties[S::INTERVAL]->getValue(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** {@inheritDoc} */ |
39
|
22 |
|
public function setInterval(float $interval): self |
40
|
|
|
{ |
41
|
22 |
|
$this->properties[S::INTERVAL]->setValue($interval); |
42
|
22 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** {@inheritDoc} */ |
46
|
28 |
|
public function getErasingShift(): int |
47
|
|
|
{ |
48
|
|
|
return |
49
|
28 |
|
$this->properties[S::ERASING_SHIFT]->getValue(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** {@inheritDoc} */ |
53
|
28 |
|
public function getMessage(): string |
54
|
|
|
{ |
55
|
|
|
return |
56
|
28 |
|
$this->properties[S::MESSAGE]->getValue(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** {@inheritDoc} */ |
60
|
23 |
|
public function setMessage(string $message, int $erasingLength = null): self |
61
|
|
|
{ |
62
|
23 |
|
$this->properties[S::MESSAGE]->setValue($message); |
63
|
23 |
|
if (Defaults::EMPTY === $message) { |
64
|
1 |
|
$erasingLength = 0; |
65
|
1 |
|
$this->setMessageSuffix(Defaults::EMPTY); |
66
|
|
|
} else { |
67
|
23 |
|
$erasingLength = $this->refineErasingLen($message, $erasingLength); |
68
|
23 |
|
$this->setMessageSuffix(Defaults::DEFAULT_SUFFIX); |
69
|
|
|
} |
70
|
|
|
|
71
|
23 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->setValue($erasingLength); |
72
|
23 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $string |
77
|
|
|
* @param null|int $erasingLen |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
23 |
|
protected function refineErasingLen(string $string, ?int $erasingLen): int |
81
|
|
|
{ |
82
|
23 |
|
if (null === $erasingLen) { |
83
|
23 |
|
return $this->computeErasingLen([$string]); |
84
|
|
|
} |
85
|
1 |
|
return $erasingLen; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $strings |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
26 |
|
protected function computeErasingLen(array $strings): int |
93
|
|
|
{ |
94
|
26 |
|
if (empty($strings)) { |
95
|
5 |
|
return 0; |
96
|
|
|
} |
97
|
25 |
|
return $this->compErasingLen($strings); |
98
|
|
|
} |
99
|
|
|
|
100
|
25 |
|
private function compErasingLen(array $strings): int |
101
|
|
|
{ |
102
|
|
|
// TODO check if all elements have the same erasingLen |
103
|
25 |
|
if (null === $symbol = $strings[0]) { |
104
|
1 |
|
return 0; |
105
|
|
|
} |
106
|
24 |
|
$mbSymbolLen = mb_strlen($symbol); |
107
|
24 |
|
$oneCharLen = strlen($symbol) / $mbSymbolLen; |
108
|
24 |
|
if (4 === $oneCharLen) { |
109
|
3 |
|
return 2 * $mbSymbolLen; |
110
|
|
|
} |
111
|
24 |
|
return 1 * $mbSymbolLen; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** {@inheritDoc} */ |
115
|
23 |
|
public function setMessageSuffix(string $suffix): self |
116
|
|
|
{ |
117
|
23 |
|
$this->properties[S::MESSAGE_SUFFIX]->setValue($suffix); |
118
|
23 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** {@inheritDoc} */ |
122
|
28 |
|
public function getMessagePrefix(): string |
123
|
|
|
{ |
124
|
|
|
return |
125
|
28 |
|
$this->properties[S::MESSAGE_PREFIX]->getValue(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** {@inheritDoc} */ |
129
|
6 |
|
public function setMessagePrefix(string $prefix): self |
130
|
|
|
{ |
131
|
6 |
|
$this->properties[S::MESSAGE_PREFIX]->setValue($prefix); |
132
|
6 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** {@inheritDoc} */ |
136
|
28 |
|
public function getMessageSuffix(): string |
137
|
|
|
{ |
138
|
|
|
return |
139
|
28 |
|
$this->properties[S::MESSAGE_SUFFIX]->getValue(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** {@inheritDoc} */ |
143
|
28 |
|
public function getInlinePaddingStr(): string |
144
|
|
|
{ |
145
|
|
|
return |
146
|
28 |
|
$this->properties[S::INLINE_PADDING_STR]->getValue(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** {@inheritDoc} */ |
150
|
3 |
|
public function setInlinePaddingStr(string $inlinePaddingStr): self |
151
|
|
|
{ |
152
|
3 |
|
$this->properties[S::INLINE_PADDING_STR]->setValue($inlinePaddingStr); |
153
|
3 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** {@inheritDoc} */ |
157
|
29 |
|
public function getFrames(): array |
158
|
|
|
{ |
159
|
|
|
return |
160
|
29 |
|
$this->properties[S::FRAMES]->getValue(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** {@inheritDoc} */ |
164
|
24 |
|
public function setFrames(array $frames): self |
165
|
|
|
{ |
166
|
24 |
|
if (Defaults::MAX_FRAMES_COUNT < ($count = count($frames))) { |
167
|
1 |
|
throw new \InvalidArgumentException( |
168
|
1 |
|
sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count) |
169
|
|
|
); |
170
|
|
|
} |
171
|
23 |
|
$this->properties[S::FRAMES]->setValue($frames); |
172
|
23 |
|
$this->properties[S::ERASING_SHIFT]->setValue($this->computeErasingLen($frames)); |
173
|
23 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** {@inheritDoc} */ |
177
|
28 |
|
public function getStyles(): array |
178
|
|
|
{ |
179
|
|
|
return |
180
|
28 |
|
$this->properties[S::STYLES]->getValue(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** {@inheritDoc} */ |
184
|
26 |
|
public function setStyles(array $styles): self |
185
|
|
|
{ |
186
|
26 |
|
$this->properties[S::STYLES]->setValue($styles); |
187
|
26 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** {@inheritDoc} */ |
191
|
28 |
|
public function getMessageErasingLen(): int |
192
|
|
|
{ |
193
|
|
|
return |
194
|
28 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->getValue(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** {@inheritDoc} */ |
198
|
28 |
|
public function getSpacer(): string |
199
|
|
|
{ |
200
|
|
|
return |
201
|
28 |
|
$this->properties[S::SPACER]->getValue(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** {@inheritDoc} */ |
205
|
1 |
|
public function setSpacer(string $spacer): self |
206
|
|
|
{ |
207
|
1 |
|
$this->properties[S::SPACER]->setValue($spacer); |
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** {@inheritDoc} */ |
212
|
2 |
|
public function merge(self $settings): self |
213
|
|
|
{ |
214
|
2 |
|
foreach ($this->properties as $name => $value) { |
215
|
2 |
|
if ($settings->properties[$name]->isNotDefault()) { |
216
|
1 |
|
$this->properties[$name] = $settings->properties[$name]; |
217
|
|
|
} |
218
|
|
|
} |
219
|
2 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|