1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpLightning\Config; |
6
|
|
|
|
7
|
|
|
use JsonSerializable; |
8
|
|
|
use PhpLightning\Config\Backend\BackendConfigInterface; |
9
|
|
|
use PhpLightning\Shared\Value\SendableRange; |
10
|
|
|
|
11
|
|
|
final class LightningConfig implements JsonSerializable |
12
|
|
|
{ |
13
|
|
|
private ?string $mode = null; |
14
|
|
|
private ?string $domain = null; |
15
|
|
|
private ?string $receiver = null; |
16
|
|
|
private ?SendableRange $sendableRange = null; |
17
|
|
|
private ?BackendsConfig $backends = null; |
18
|
|
|
|
19
|
8 |
|
public function __construct() |
20
|
|
|
{ |
21
|
8 |
|
} |
22
|
|
|
|
23
|
1 |
|
public function setMode(string $mode): self |
24
|
|
|
{ |
25
|
1 |
|
$this->mode = $mode; |
26
|
1 |
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function setDomain(string $domain): self |
30
|
|
|
{ |
31
|
2 |
|
$parseUrl = parse_url($domain); |
32
|
2 |
|
$this->domain = $parseUrl['host'] ?? $domain; |
33
|
2 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function setReceiver(string $receiver): self |
37
|
|
|
{ |
38
|
1 |
|
$this->receiver = $receiver; |
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function setSendableRange(int $min, int $max): self |
43
|
|
|
{ |
44
|
2 |
|
$this->sendableRange = SendableRange::withMinMax($min, $max); |
45
|
2 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public function addBackend(BackendConfigInterface $backendConfig): self |
49
|
|
|
{ |
50
|
2 |
|
$this->backends ??= new BackendsConfig(); |
51
|
2 |
|
$this->backends->add($backendConfig); |
52
|
2 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
public function jsonSerialize(): array |
56
|
|
|
{ |
57
|
8 |
|
$result = []; |
58
|
8 |
|
if ($this->mode !== null) { |
59
|
1 |
|
$result['mode'] = $this->mode; |
60
|
|
|
} |
61
|
8 |
|
if ($this->backends !== null) { |
62
|
2 |
|
$result['backends'] = $this->backends->jsonSerialize(); |
63
|
|
|
} |
64
|
8 |
|
if ($this->domain !== null) { |
65
|
2 |
|
$result['domain'] = $this->domain; |
66
|
|
|
} |
67
|
8 |
|
if ($this->receiver !== null) { |
68
|
1 |
|
$result['receiver'] = $this->receiver; |
69
|
|
|
} |
70
|
8 |
|
if ($this->sendableRange !== null) { |
71
|
2 |
|
$result['sendable-range'] = $this->sendableRange; |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|