|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace AlecRabbit\Spinner\Core\Builder; |
|
7
|
|
|
|
|
8
|
|
|
use AlecRabbit\Spinner\Contract\IFrame; |
|
9
|
|
|
use AlecRabbit\Spinner\Contract\Pattern\IPattern; |
|
10
|
|
|
use AlecRabbit\Spinner\Core\Builder\Settings\Contract\IWidgetSettingsBuilder; |
|
11
|
|
|
use AlecRabbit\Spinner\Core\Pattern\Contract\IStylePattern; |
|
12
|
|
|
use AlecRabbit\Spinner\Core\Settings\Contract\IWidgetSettings; |
|
13
|
|
|
use AlecRabbit\Spinner\Core\Settings\WidgetSettings; |
|
14
|
|
|
use AlecRabbit\Spinner\Exception\LogicException; |
|
15
|
|
|
|
|
16
|
|
|
final class WidgetSettingsBuilder implements IWidgetSettingsBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
private ?IFrame $leadingSpacer = null; |
|
19
|
|
|
private ?IFrame $trailingSpacer = null; |
|
20
|
|
|
private ?IStylePattern $stylePattern = null; |
|
21
|
|
|
private ?IPattern $charPattern = null; |
|
22
|
|
|
|
|
23
|
|
|
public function build(): IWidgetSettings |
|
24
|
|
|
{ |
|
25
|
|
|
$this->validate(); |
|
26
|
|
|
|
|
27
|
|
|
return |
|
28
|
|
|
new WidgetSettings( |
|
29
|
|
|
leadingSpacer: $this->leadingSpacer, |
|
|
|
|
|
|
30
|
|
|
trailingSpacer: $this->trailingSpacer, |
|
|
|
|
|
|
31
|
|
|
stylePattern: $this->stylePattern, |
|
|
|
|
|
|
32
|
|
|
charPattern: $this->charPattern, |
|
|
|
|
|
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws LogicException |
|
38
|
|
|
*/ |
|
39
|
|
|
private function validate(): void |
|
40
|
|
|
{ |
|
41
|
|
|
match (true) { |
|
42
|
|
|
$this->leadingSpacer === null => throw new LogicException('Leading spacer is not set.'), |
|
43
|
|
|
$this->trailingSpacer === null => throw new LogicException('Trailing spacer is not set.'), |
|
44
|
|
|
$this->stylePattern === null => throw new LogicException('Style pattern is not set.'), |
|
45
|
|
|
$this->charPattern === null => throw new LogicException('Char pattern is not set.'), |
|
46
|
|
|
default => null, |
|
47
|
|
|
}; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function withLeadingSpacer(IFrame $frame): IWidgetSettingsBuilder |
|
51
|
|
|
{ |
|
52
|
|
|
$clone = clone $this; |
|
53
|
|
|
$clone->leadingSpacer = $frame; |
|
54
|
|
|
return $clone; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function withTrailingSpacer(IFrame $frame): IWidgetSettingsBuilder |
|
58
|
|
|
{ |
|
59
|
|
|
$clone = clone $this; |
|
60
|
|
|
$clone->trailingSpacer = $frame; |
|
61
|
|
|
return $clone; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function withStylePattern(IStylePattern $pattern): IWidgetSettingsBuilder |
|
65
|
|
|
{ |
|
66
|
|
|
$clone = clone $this; |
|
67
|
|
|
$clone->stylePattern = $pattern; |
|
68
|
|
|
return $clone; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function withCharPattern(IPattern $pattern): IWidgetSettingsBuilder |
|
72
|
|
|
{ |
|
73
|
|
|
$clone = clone $this; |
|
74
|
|
|
$clone->charPattern = $pattern; |
|
75
|
|
|
return $clone; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|