1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Settings; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Calculator; |
6
|
|
|
use AlecRabbit\Spinner\Core\Sentinel; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
8
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\S; |
9
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\SettingsInterface; |
10
|
|
|
|
11
|
|
|
class Settings implements SettingsInterface |
12
|
|
|
{ |
13
|
|
|
/** @var Property[] */ |
14
|
|
|
protected $properties; |
15
|
|
|
|
16
|
45 |
|
public function __construct() |
17
|
|
|
{ |
18
|
45 |
|
$this->properties = $this->initialize(); |
19
|
45 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return Property[] |
23
|
|
|
*/ |
24
|
45 |
|
private function initialize(): array |
25
|
|
|
{ |
26
|
45 |
|
$properties = []; |
27
|
45 |
|
foreach (Defaults::DEFAULT_SETTINGS as $name => $value) { |
28
|
45 |
|
$properties[$name] = new Property($value); |
29
|
|
|
} |
30
|
45 |
|
return $properties; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** {@inheritDoc} */ |
34
|
30 |
|
public function getInterval(): float |
35
|
|
|
{ |
36
|
|
|
return |
37
|
30 |
|
$this->properties[S::INTERVAL]->getValue(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** {@inheritDoc} */ |
41
|
28 |
|
public function setInterval(float $interval): self |
42
|
|
|
{ |
43
|
28 |
|
$this->properties[S::INTERVAL]->setValue($interval); |
44
|
28 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** {@inheritDoc} */ |
48
|
28 |
|
public function isHideCursor(): bool |
49
|
|
|
{ |
50
|
|
|
return |
51
|
28 |
|
$this->properties[S::HIDE_CURSOR]->getValue(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** {@inheritDoc} */ |
55
|
1 |
|
public function setHideCursor($value = true): self |
56
|
|
|
{ |
57
|
1 |
|
$this->properties[S::HIDE_CURSOR]->setValue($value); |
58
|
1 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** {@inheritDoc} */ |
62
|
28 |
|
public function isEnabled(): bool |
63
|
|
|
{ |
64
|
|
|
return |
65
|
28 |
|
$this->properties[S::ENABLED]->getValue(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** {@inheritDoc} */ |
69
|
1 |
|
public function setEnabled(bool $enabled = true): self |
70
|
|
|
{ |
71
|
1 |
|
$this->properties[S::ENABLED]->setValue($enabled); |
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritDoc} */ |
76
|
31 |
|
public function getMessage(): ?string |
77
|
|
|
{ |
78
|
|
|
return |
79
|
31 |
|
$this->properties[S::MESSAGE]->getValue(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** {@inheritDoc} */ |
83
|
26 |
|
public function setMessage(?string $message): self |
84
|
|
|
{ |
85
|
26 |
|
$this->properties[S::MESSAGE]->setValue($message); |
86
|
26 |
|
if (Defaults::EMPTY_STRING === $message || null === $message) { |
87
|
5 |
|
$erasingLength = 0; |
88
|
|
|
} else { |
89
|
26 |
|
$erasingLength = Calculator::computeErasingLength($message); |
90
|
|
|
} |
91
|
26 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->setValue($erasingLength); |
92
|
26 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** {@inheritDoc} */ |
96
|
2 |
|
public function setMessageSuffix(string $suffix): self |
97
|
|
|
{ |
98
|
2 |
|
$this->properties[S::MESSAGE_SUFFIX]->setValue($suffix); |
99
|
2 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** {@inheritDoc} */ |
103
|
28 |
|
public function getMessageSuffix(): string |
104
|
|
|
{ |
105
|
|
|
return |
106
|
28 |
|
$this->properties[S::MESSAGE_SUFFIX]->getValue(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** {@inheritDoc} */ |
110
|
30 |
|
public function getInlinePaddingStr(): string |
111
|
|
|
{ |
112
|
|
|
return |
113
|
30 |
|
$this->properties[S::INLINE_PADDING_STR]->getValue(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** {@inheritDoc} */ |
117
|
3 |
|
public function setInlinePaddingStr(string $inlinePaddingStr): self |
118
|
|
|
{ |
119
|
3 |
|
$this->properties[S::INLINE_PADDING_STR]->setValue($inlinePaddingStr); |
120
|
3 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** {@inheritDoc} */ |
124
|
30 |
|
public function getFrames(): array |
125
|
|
|
{ |
126
|
|
|
return |
127
|
30 |
|
$this->properties[S::FRAMES]->getValue(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** {@inheritDoc} */ |
131
|
40 |
|
public function setFrames(array $frames): self |
132
|
|
|
{ |
133
|
40 |
|
Sentinel::assertFrames($frames); |
134
|
28 |
|
$this->properties[S::FRAMES]->setValue($frames); |
135
|
28 |
|
$this->properties[S::ERASING_SHIFT]->setValue(Calculator::computeErasingLengths($frames)); |
136
|
28 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** {@inheritDoc} */ |
140
|
30 |
|
public function getStyles(): array |
141
|
|
|
{ |
142
|
|
|
return |
143
|
30 |
|
$this->properties[S::STYLES]->getValue(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** {@inheritDoc} */ |
147
|
28 |
|
public function setStyles(array $styles): self |
148
|
|
|
{ |
149
|
28 |
|
$this->properties[S::STYLES]->setValue($styles); |
150
|
28 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** {@inheritDoc} */ |
154
|
3 |
|
public function getMessageErasingLength(): int |
155
|
|
|
{ |
156
|
|
|
return |
157
|
3 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->getValue(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** {@inheritDoc} */ |
161
|
3 |
|
public function getSpacer(): string |
162
|
|
|
{ |
163
|
|
|
return |
164
|
3 |
|
$this->properties[S::SPACER]->getValue(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** {@inheritDoc} */ |
168
|
1 |
|
public function setSpacer(string $spacer): self |
169
|
|
|
{ |
170
|
1 |
|
$this->properties[S::SPACER]->setValue($spacer); |
171
|
1 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** {@inheritDoc} */ |
175
|
6 |
|
public function merge(self $settings): self |
176
|
|
|
{ |
177
|
6 |
|
$keys = array_keys($this->properties); |
178
|
6 |
|
foreach ($keys as $key) { |
179
|
6 |
|
if ($settings->properties[$key]->isNotDefault()) { |
180
|
5 |
|
$this->properties[$key] = $settings->properties[$key]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
6 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** {@inheritDoc} */ |
187
|
12 |
|
public function getInitialPercent(): ?float |
188
|
|
|
{ |
189
|
12 |
|
return $this->properties[S::INITIAL_PERCENT]->getValue(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** {@inheritDoc} */ |
193
|
12 |
|
public function setInitialPercent(?float $percent): self |
194
|
|
|
{ |
195
|
12 |
|
$this->properties[S::INITIAL_PERCENT]->setValue($percent); |
196
|
12 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** {@inheritDoc} */ |
200
|
28 |
|
public function getJugglersOrder(): array |
201
|
|
|
{ |
202
|
28 |
|
return $this->properties[S::JUGGLERS_ORDER]->getValue(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** {@inheritDoc} */ |
206
|
3 |
|
public function setJugglersOrder(array $order): self |
207
|
|
|
{ |
208
|
3 |
|
$order = array_unique($order); |
209
|
3 |
|
Sentinel::assertJugglersOrder($order); |
210
|
1 |
|
$this->properties[S::JUGGLERS_ORDER]->setValue($order); |
211
|
1 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|