1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Settings; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Spinner\Core\Calculator; |
6
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
7
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\S; |
8
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\SettingsInterface; |
9
|
|
|
|
10
|
|
|
class Settings implements SettingsInterface |
11
|
|
|
{ |
12
|
|
|
/** @var Property[] */ |
13
|
|
|
protected $properties; |
14
|
|
|
|
15
|
30 |
|
public function __construct() |
16
|
|
|
{ |
17
|
30 |
|
$this->properties = $this->initialize(); |
18
|
30 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return Property[] |
22
|
|
|
*/ |
23
|
30 |
|
private function initialize(): array |
24
|
|
|
{ |
25
|
30 |
|
$properties = []; |
26
|
30 |
|
foreach (Defaults::DEFAULT_SETTINGS as $name => $value) { |
27
|
30 |
|
$properties[$name] = new Property($value); |
28
|
|
|
} |
29
|
30 |
|
return $properties; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** {@inheritDoc} */ |
33
|
28 |
|
public function getInterval(): float |
34
|
|
|
{ |
35
|
|
|
return |
36
|
28 |
|
$this->properties[S::INTERVAL]->getValue(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** {@inheritDoc} */ |
40
|
26 |
|
public function setInterval(float $interval): self |
41
|
|
|
{ |
42
|
26 |
|
$this->properties[S::INTERVAL]->setValue($interval); |
43
|
26 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** {@inheritDoc} */ |
47
|
28 |
|
public function getMessage(): ?string |
48
|
|
|
{ |
49
|
|
|
return |
50
|
28 |
|
$this->properties[S::MESSAGE]->getValue(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** {@inheritDoc} */ |
54
|
24 |
|
public function setMessage(?string $message, ?int $erasingLength = null): self |
55
|
|
|
{ |
56
|
24 |
|
$this->properties[S::MESSAGE]->setValue($message); |
57
|
24 |
|
if (Defaults::EMPTY_STRING === $message || null === $message) { |
58
|
4 |
|
$erasingLength = 0; |
59
|
|
|
} else { |
60
|
24 |
|
$erasingLength = Calculator::refineErasingLen($message, $erasingLength); |
61
|
|
|
} |
62
|
24 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->setValue($erasingLength); |
63
|
24 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** {@inheritDoc} */ |
67
|
2 |
|
public function setMessageSuffix(string $suffix): self |
68
|
|
|
{ |
69
|
2 |
|
$this->properties[S::MESSAGE_SUFFIX]->setValue($suffix); |
70
|
2 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** {@inheritDoc} */ |
74
|
25 |
|
public function getMessageSuffix(): string |
75
|
|
|
{ |
76
|
|
|
return |
77
|
25 |
|
$this->properties[S::MESSAGE_SUFFIX]->getValue(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** {@inheritDoc} */ |
81
|
3 |
|
public function getInlinePaddingStr(): string |
82
|
|
|
{ |
83
|
|
|
return |
84
|
3 |
|
$this->properties[S::INLINE_PADDING_STR]->getValue(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** {@inheritDoc} */ |
88
|
3 |
|
public function setInlinePaddingStr(string $inlinePaddingStr): self |
89
|
|
|
{ |
90
|
3 |
|
$this->properties[S::INLINE_PADDING_STR]->setValue($inlinePaddingStr); |
91
|
3 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** {@inheritDoc} */ |
95
|
29 |
|
public function getFrames(): array |
96
|
|
|
{ |
97
|
|
|
return |
98
|
29 |
|
$this->properties[S::FRAMES]->getValue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** {@inheritDoc} */ |
102
|
28 |
|
public function setFrames(array $frames): self |
103
|
|
|
{ |
104
|
28 |
|
if (Defaults::MAX_FRAMES_COUNT < ($count = count($frames))) { |
105
|
1 |
|
throw new \InvalidArgumentException( |
106
|
1 |
|
sprintf('MAX_SYMBOLS_COUNT limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count) |
107
|
|
|
); |
108
|
|
|
} |
109
|
27 |
|
$this->properties[S::FRAMES]->setValue($frames); |
110
|
27 |
|
$this->properties[S::ERASING_SHIFT]->setValue(Calculator::computeErasingLength($frames)); |
111
|
27 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** {@inheritDoc} */ |
115
|
28 |
|
public function getStyles(): array |
116
|
|
|
{ |
117
|
|
|
return |
118
|
28 |
|
$this->properties[S::STYLES]->getValue(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** {@inheritDoc} */ |
122
|
26 |
|
public function setStyles(array $styles): self |
123
|
|
|
{ |
124
|
26 |
|
$this->properties[S::STYLES]->setValue($styles); |
125
|
26 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** {@inheritDoc} */ |
129
|
25 |
|
public function getMessageErasingLength(): int |
130
|
|
|
{ |
131
|
|
|
return |
132
|
25 |
|
$this->properties[S::MESSAGE_ERASING_LENGTH]->getValue(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** {@inheritDoc} */ |
136
|
3 |
|
public function getSpacer(): string |
137
|
|
|
{ |
138
|
|
|
return |
139
|
3 |
|
$this->properties[S::SPACER]->getValue(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** {@inheritDoc} */ |
143
|
1 |
|
public function setSpacer(string $spacer): self |
144
|
|
|
{ |
145
|
1 |
|
$this->properties[S::SPACER]->setValue($spacer); |
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** {@inheritDoc} */ |
150
|
6 |
|
public function merge(self $settings): self |
151
|
|
|
{ |
152
|
6 |
|
$keys = array_keys($this->properties); |
153
|
6 |
|
foreach ($keys as $key) { |
154
|
6 |
|
if ($settings->properties[$key]->isNotDefault()) { |
155
|
5 |
|
$this->properties[$key] = $settings->properties[$key]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
6 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** {@inheritDoc} */ |
162
|
11 |
|
public function getInitialPercent(): ?float |
163
|
|
|
{ |
164
|
11 |
|
return $this->properties[S::INITIAL_PERCENT]->getValue(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** {@inheritDoc} */ |
168
|
11 |
|
public function setInitialPercent(?float $percent): self |
169
|
|
|
{ |
170
|
11 |
|
$this->properties[S::INITIAL_PERCENT]->setValue($percent); |
171
|
11 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|