1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RemotelyLiving\PHPDNS\Entities; |
4
|
|
|
|
5
|
|
|
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable; |
6
|
|
|
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable; |
7
|
|
|
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
use function count; |
10
|
|
|
use function explode; |
11
|
|
|
use function trim; |
12
|
|
|
|
13
|
|
|
abstract class DataAbstract implements Arrayable, Serializable, \Stringable |
14
|
|
|
{ |
15
|
|
|
abstract public function __toString(): string; |
16
|
|
|
|
17
|
|
|
abstract public function toArray(): array; |
18
|
|
|
|
19
|
|
|
public function __serialize(): array |
20
|
|
|
{ |
21
|
|
|
return $this->toArray(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function equals(DataAbstract $dataAbstract): bool |
25
|
|
|
{ |
26
|
|
|
return (string)$this === (string)$dataAbstract; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws \RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException |
31
|
|
|
*/ |
32
|
|
|
public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self |
33
|
|
|
{ |
34
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_TXT)) { |
35
|
|
|
return new TXTData(trim($data, '"')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_NS)) { |
39
|
|
|
return new NSData(new Hostname($data)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_CNAME)) { |
43
|
|
|
return new CNAMEData(new Hostname($data)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$parsed = self::parseDataToArray($data); |
47
|
|
|
|
48
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_MX)) { |
49
|
|
|
return new MXData(new Hostname($parsed[1]), (int)$parsed[0]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_SOA)) { |
53
|
|
|
return new SOAData( |
54
|
|
|
new Hostname($parsed[0]), |
55
|
|
|
new Hostname($parsed[1]), |
56
|
|
|
(int)($parsed[2] ?? 0), |
57
|
|
|
(int)($parsed[3] ?? 0), |
58
|
|
|
(int)($parsed[4] ?? 0), |
59
|
|
|
(int)($parsed[5] ?? 0), |
60
|
|
|
(int)($parsed[6] ?? 0) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_CAA) && count($parsed) === 3) { |
65
|
|
|
return new CAAData((int)$parsed[0], (string)$parsed[1], $parsed[2]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_SRV)) { |
69
|
|
|
return new SRVData( |
70
|
|
|
(int)$parsed[0] ?: 0, |
71
|
|
|
(int) $parsed[1] ?: 0, |
72
|
|
|
(int) $parsed[2] ?: 0, |
73
|
|
|
new Hostname($parsed[3]) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_PTR)) { |
78
|
|
|
return new PTRData(new Hostname($data)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new InvalidArgumentException("{$data} could not be created with type {$recordType}"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function jsonSerialize(): array |
85
|
|
|
{ |
86
|
|
|
return $this->toArray(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function init(): void |
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
private static function parseDataToArray(string $data): array |
93
|
|
|
{ |
94
|
|
|
return explode(' ', $data); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|