1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Contracts\SettingsInterface; |
6
|
|
|
use AlecRabbit\Spinner\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 $prefix; |
23
|
|
|
/** @var string */ |
24
|
|
|
protected $suffix; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $inlinePaddingStr; |
27
|
|
|
/** @var array */ |
28
|
|
|
protected $symbols; |
29
|
|
|
/** @var array */ |
30
|
|
|
protected $styles; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Settings constructor. |
34
|
|
|
*/ |
35
|
10 |
|
public function __construct() |
36
|
|
|
{ |
37
|
10 |
|
$this->defaults(); |
38
|
10 |
|
} |
39
|
|
|
|
40
|
10 |
|
protected function defaults(): Settings |
41
|
|
|
{ |
42
|
|
|
return |
43
|
|
|
$this |
44
|
10 |
|
->setSuffix(null) |
45
|
10 |
|
->setSymbols(null) |
46
|
10 |
|
->setStyles(null) |
47
|
10 |
|
->setMessage(null) |
48
|
10 |
|
->setPrefix(null) |
49
|
10 |
|
->setInterval(null) |
50
|
10 |
|
->setErasingShift(null) |
51
|
10 |
|
->setInlinePaddingStr(null); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return float |
56
|
|
|
*/ |
57
|
10 |
|
public function getInterval(): float |
58
|
|
|
{ |
59
|
10 |
|
return $this->interval; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param null|float $interval |
64
|
|
|
* @return Settings |
65
|
|
|
*/ |
66
|
10 |
|
public function setInterval(?float $interval): Settings |
67
|
|
|
{ |
68
|
10 |
|
$this->interval = $interval ?? SettingsInterface::DEFAULT_INTERVAL; |
69
|
10 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
10 |
|
public function getErasingShift(): int |
76
|
|
|
{ |
77
|
10 |
|
return $this->erasingShift; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param null|int $erasingShift |
82
|
|
|
* @return Settings |
83
|
|
|
*/ |
84
|
10 |
|
public function setErasingShift(?int $erasingShift): Settings |
85
|
|
|
{ |
86
|
10 |
|
$this->erasingShift = $erasingShift ?? SettingsInterface::DEFAULT_ERASING_SHIFT; |
87
|
10 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
10 |
|
public function getMessage(): string |
94
|
|
|
{ |
95
|
10 |
|
return $this->message; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param null|string $string |
100
|
|
|
* @return Settings |
101
|
|
|
*/ |
102
|
10 |
|
public function setMessage(?string $string): Settings |
103
|
|
|
{ |
104
|
10 |
|
$this->message = $string ?? SettingsInterface::EMPTY; |
105
|
10 |
|
if (SettingsInterface::EMPTY === $this->message) { |
106
|
10 |
|
$this->setSuffix(SettingsInterface::EMPTY); |
107
|
|
|
} else { |
108
|
9 |
|
$this->setSuffix(SettingsInterface::DEFAULT_SUFFIX); |
109
|
|
|
} |
110
|
10 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
10 |
|
public function getPrefix(): string |
117
|
|
|
{ |
118
|
10 |
|
return $this->prefix; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param null|string $prefix |
123
|
|
|
* @return Settings |
124
|
|
|
*/ |
125
|
10 |
|
public function setPrefix(?string $prefix): Settings |
126
|
|
|
{ |
127
|
10 |
|
$this->prefix = $prefix ?? SettingsInterface::ONE_SPACE_SYMBOL; |
128
|
10 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
10 |
|
public function getSuffix(): string |
135
|
|
|
{ |
136
|
10 |
|
return $this->suffix; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param null|string $suffix |
141
|
|
|
* @return Settings |
142
|
|
|
*/ |
143
|
10 |
|
public function setSuffix(?string $suffix): Settings |
144
|
|
|
{ |
145
|
10 |
|
$this->suffix = $suffix ?? SettingsInterface::DEFAULT_SUFFIX; |
146
|
10 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
10 |
|
public function getInlinePaddingStr(): string |
153
|
|
|
{ |
154
|
10 |
|
return $this->inlinePaddingStr; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param null|string $inlinePaddingStr |
159
|
|
|
* @return Settings |
160
|
|
|
*/ |
161
|
10 |
|
public function setInlinePaddingStr(?string $inlinePaddingStr): Settings |
162
|
|
|
{ |
163
|
10 |
|
$this->inlinePaddingStr = $inlinePaddingStr ?? SettingsInterface::EMPTY; |
164
|
10 |
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
10 |
|
public function getSymbols(): array |
171
|
|
|
{ |
172
|
10 |
|
return $this->symbols; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param null|array $symbols |
177
|
|
|
* @return Settings |
178
|
|
|
*/ |
179
|
10 |
|
public function setSymbols(?array $symbols): Settings |
180
|
|
|
{ |
181
|
10 |
|
$this->symbols = $symbols ?? static::DEFAULT_SYMBOLS; |
182
|
10 |
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
10 |
|
public function getStyles(): array |
189
|
|
|
{ |
190
|
10 |
|
return $this->styles; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param null|array $styles |
195
|
|
|
* @return Settings |
196
|
|
|
*/ |
197
|
10 |
|
public function setStyles(?array $styles): Settings |
198
|
|
|
{ |
199
|
10 |
|
$this->styles = $this->mergeStyles(StylesInterface::DEFAULT_STYLES, $styles ?? []); |
200
|
10 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
10 |
|
protected function mergeStyles(array $default_styles, array $styles): array |
204
|
|
|
{ |
205
|
10 |
|
foreach ($default_styles as $key => $defaults) { |
206
|
10 |
|
if (\array_key_exists($key, $styles)) { |
207
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
208
|
10 |
|
$default_styles[$key] = array_merge($default_styles[$key], $styles[$key]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
10 |
|
return $default_styles; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|