1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Used to create RData of types that have not yet been implemented in the library. |
18
|
|
|
*/ |
19
|
|
|
class PolymorphicRdata implements RdataInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The RData type. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $data; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $typeCode = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* PolymorphicRdata constructor. |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct(?string $type = null, ?string $data = null) |
42
|
|
|
{ |
43
|
2 |
|
if (null !== $type) { |
44
|
2 |
|
$this->setType($type); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
if (null !== $data) { |
48
|
2 |
|
$this->setData($data); |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
public function setType(string $type): void |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
2 |
|
$this->typeCode = Types::getTypeCode($type); |
56
|
|
|
} catch (UnsupportedTypeException $e) { |
57
|
|
|
$this->typeCode = 0; |
58
|
|
|
} |
59
|
2 |
|
$this->type = $type; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function getType(): string |
66
|
|
|
{ |
67
|
2 |
|
return $this->type; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function setTypeCode(int $typeCode): void |
71
|
|
|
{ |
72
|
|
|
$this->typeCode = $typeCode; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function getTypeCode(): int |
79
|
|
|
{ |
80
|
1 |
|
return $this->typeCode; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function setData(string $data): void |
84
|
|
|
{ |
85
|
2 |
|
$this->data = $data; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
public function getData(): ?string |
89
|
|
|
{ |
90
|
2 |
|
return $this->data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
1 |
|
public function toText(): string |
97
|
|
|
{ |
98
|
1 |
|
return $this->getData() ?? ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function toWire(): string |
105
|
|
|
{ |
106
|
|
|
return $this->data ?? ''; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function fromText(string $text): void |
113
|
|
|
{ |
114
|
|
|
$this->setData($text); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
121
|
|
|
{ |
122
|
|
|
$this->setData(substr($rdata, $offset, $rdLength ?? strlen($rdata))); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|