|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RemotelyLiving\PHPDNS\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use RemotelyLiving\PHPDNS\Entities\Interfaces\DNSRecordInterface; |
|
6
|
|
|
|
|
7
|
|
|
use function serialize; |
|
8
|
|
|
use function unserialize; |
|
9
|
|
|
|
|
10
|
|
|
final class DNSRecord extends EntityAbstract implements DNSRecordInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private const DATA = 'data'; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( |
|
18
|
|
|
private DNSRecordType $recordType, |
|
19
|
|
|
private Hostname $hostname, |
|
20
|
|
|
private int $TTL, |
|
21
|
|
|
private ?IPAddress $IPAddress = null, |
|
22
|
|
|
private string $class = 'IN', |
|
23
|
|
|
private ?DataAbstract $data = null |
|
24
|
|
|
) { |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function createFromPrimitives( |
|
28
|
|
|
string $recordType, |
|
29
|
|
|
string $hostname, |
|
30
|
|
|
int $ttl, |
|
31
|
|
|
string $IPAddress = null, |
|
32
|
|
|
string $class = 'IN', |
|
33
|
|
|
string $data = null |
|
34
|
|
|
): DNSRecord { |
|
35
|
|
|
$type = DNSRecordType::createFromString($recordType); |
|
36
|
|
|
$hostname = Hostname::createFromString($hostname); |
|
37
|
|
|
$data = ($data !== null) |
|
38
|
|
|
? DataAbstract::createFromTypeAndString($type, $data) |
|
39
|
|
|
: null; |
|
40
|
|
|
|
|
41
|
|
|
return new self( |
|
42
|
|
|
$type, |
|
43
|
|
|
$hostname, |
|
44
|
|
|
$ttl, |
|
45
|
|
|
$IPAddress ? IPAddress::createFromString($IPAddress) : null, |
|
46
|
|
|
$class, |
|
47
|
|
|
$data |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getType(): DNSRecordType |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->recordType; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getHostname(): Hostname |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->hostname; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getTTL(): int |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->TTL; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getIPAddress(): ?IPAddress |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->IPAddress; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getClass(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->class; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getData(): ?DataAbstract |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setData(DataAbstract $data): self |
|
82
|
|
|
{ |
|
83
|
|
|
|
|
84
|
|
|
$this->data = $data; |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function setTTL(int $ttl): DNSRecordInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$this->TTL = $ttl; |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function toArray(): array |
|
95
|
|
|
{ |
|
96
|
|
|
$formatted = [ |
|
97
|
|
|
'hostname' => (string)$this->hostname, |
|
98
|
|
|
'type' => (string)$this->recordType, |
|
99
|
|
|
'TTL' => $this->TTL, |
|
100
|
|
|
'class' => $this->class, |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
if ($this->IPAddress !== null) { |
|
104
|
|
|
$formatted['IPAddress'] = (string)$this->IPAddress; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($this->data !== null) { |
|
108
|
|
|
$formatted[self::DATA] = (string)$this->data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $formatted; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function equals(DNSRecordInterface $record): bool |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->hostname->equals($record->getHostname()) |
|
117
|
|
|
&& $this->recordType->equals($record->getType()) |
|
118
|
|
|
&& (string)$this->data === (string)$record->getData() // could be null |
|
119
|
|
|
&& (string)$this->IPAddress === (string)$record->getIPAddress(); // could be null |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function __serialize(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->toArray(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function __unserialize(array $unserialized): void |
|
128
|
|
|
{ |
|
129
|
|
|
$rawIPAddres = $unserialized['IPAddress'] ?? null; |
|
130
|
|
|
$this->recordType = DNSRecordType::createFromString($unserialized['type']); |
|
131
|
|
|
$this->hostname = Hostname::createFromString($unserialized['hostname']); |
|
132
|
|
|
$this->TTL = (int) $unserialized['TTL']; |
|
133
|
|
|
$this->IPAddress = $rawIPAddres ? IPAddress::createFromString($rawIPAddres) : null; |
|
134
|
|
|
$this->class = $unserialized['class']; |
|
135
|
|
|
$this->data = (isset($unserialized[self::DATA])) |
|
136
|
|
|
? DataAbstract::createFromTypeAndString($this->recordType, $unserialized[self::DATA]) |
|
137
|
|
|
: null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function jsonSerialize(): array |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->toArray(); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|