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
|
|
|
* @param string|null $type |
42
|
|
|
* @param string|null $data |
43
|
|
|
*/ |
44
|
2 |
|
public function __construct(?string $type = null, ?string $data = null) |
45
|
|
|
{ |
46
|
2 |
|
if (null !== $type) { |
47
|
2 |
|
$this->setType($type); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
if (null !== $data) { |
51
|
2 |
|
$this->setData($data); |
52
|
|
|
} |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $type |
57
|
|
|
*/ |
58
|
2 |
|
public function setType(string $type): void |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
2 |
|
$this->typeCode = Types::getTypeCode($type); |
62
|
|
|
} catch (UnsupportedTypeException $e) { |
63
|
|
|
$this->typeCode = 0; |
64
|
|
|
} |
65
|
2 |
|
$this->type = $type; |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function getType(): string |
72
|
|
|
{ |
73
|
2 |
|
return $this->type; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $typeCode |
78
|
|
|
*/ |
79
|
|
|
public function setTypeCode(int $typeCode): void |
80
|
|
|
{ |
81
|
|
|
$this->typeCode = $typeCode; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function getTypeCode(): int |
88
|
|
|
{ |
89
|
1 |
|
return $this->typeCode; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $data |
94
|
|
|
*/ |
95
|
2 |
|
public function setData(string $data): void |
96
|
|
|
{ |
97
|
2 |
|
$this->data = $data; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
2 |
|
public function getData(): ?string |
104
|
|
|
{ |
105
|
2 |
|
return $this->data; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
public function toText(): string |
112
|
|
|
{ |
113
|
1 |
|
return $this->getData() ?? ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function toWire(): string |
120
|
|
|
{ |
121
|
|
|
return $this->data ?? ''; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function fromText(string $text): void |
128
|
|
|
{ |
129
|
|
|
$this->setData($text); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
136
|
|
|
{ |
137
|
|
|
$this->setData(substr($rdata, $offset, $rdLength ?? strlen($rdata))); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|