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(); |
11
|
|
|
|
12
|
|
|
public function equals(DataAbstract $dataAbstract): bool |
13
|
|
|
{ |
14
|
|
|
return (string)$this === (string)$dataAbstract; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self |
18
|
|
|
{ |
19
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_TXT)) { |
20
|
|
|
return new TXTData($data); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_NS)) { |
24
|
|
|
return new NSData(new Hostname($data)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_CNAME)) { |
28
|
|
|
return new CNAMEData(new Hostname($data)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$parsed = self::parseDataToArray($data); |
32
|
|
|
|
33
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_MX)) { |
34
|
|
|
return new MXData(new Hostname($parsed[1]), (int)$parsed[0]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_SOA)) { |
38
|
|
|
return new SOAData( |
39
|
|
|
new Hostname($parsed[0]), |
40
|
|
|
new Hostname($parsed[1]), |
41
|
|
|
(int)$parsed[2] ?? 0, |
42
|
|
|
(int)$parsed[3] ?? 0, |
43
|
|
|
(int)$parsed[4] ?? 0, |
44
|
|
|
(int)$parsed[5] ?? 0, |
45
|
|
|
(int)$parsed[6] ?? 0 |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
if ($recordType->isA(DNSRecordType::TYPE_CAA)) { |
52
|
|
|
return new CAAData($parsed[0], $parsed[1], $parsed[2]); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw new InvalidArgumentException("{$data} could not be created with type {$recordType}"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static function parseDataToArray(string $data): array |
59
|
|
|
{ |
60
|
|
|
return explode(' ', $data); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|