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