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 $domain = null; |
14
|
|
|
private ?string $receiver = null; |
15
|
|
|
private ?SendableRange $sendableRange = null; |
16
|
|
|
private ?BackendsConfig $backends = null; |
17
|
|
|
|
18
|
4 |
|
public function setDomain(string $domain): self |
19
|
|
|
{ |
20
|
4 |
|
$parseUrl = parse_url($domain); |
21
|
4 |
|
$this->domain = $parseUrl['host'] ?? $domain; |
22
|
4 |
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
public function setReceiver(string $receiver): self |
26
|
|
|
{ |
27
|
3 |
|
$this->receiver = $receiver; |
28
|
3 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
public function setSendableRange(int $min, int $max): self |
32
|
|
|
{ |
33
|
3 |
|
$this->sendableRange = SendableRange::withMinMax($min, $max); |
34
|
3 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
public function addBackend(BackendConfigInterface $backendConfig): self |
38
|
|
|
{ |
39
|
3 |
|
$this->backends ??= new BackendsConfig(); |
40
|
3 |
|
$this->backends->add($backendConfig); |
41
|
3 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
8 |
|
public function jsonSerialize(): array |
45
|
|
|
{ |
46
|
8 |
|
$result = []; |
47
|
8 |
|
if ($this->backends !== null) { |
48
|
3 |
|
$result['backends'] = $this->backends->jsonSerialize(); |
49
|
|
|
} |
50
|
8 |
|
if ($this->domain !== null) { |
51
|
4 |
|
$result['domain'] = $this->domain; |
52
|
|
|
} |
53
|
8 |
|
if ($this->receiver !== null) { |
54
|
3 |
|
$result['receiver'] = $this->receiver; |
55
|
|
|
} |
56
|
8 |
|
if ($this->sendableRange !== null) { |
57
|
3 |
|
$result['sendable-range'] = $this->sendableRange; |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
return $result; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|