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; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Rdata\RdataInterface; |
17
|
|
|
|
18
|
|
|
class ResourceRecord |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
private $class = Classes::INTERNET; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var RdataInterface|null |
27
|
|
|
*/ |
28
|
|
|
private $rdata; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int|null |
32
|
|
|
*/ |
33
|
|
|
private $ttl; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
private $comment; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $name |
47
|
|
|
* @param RdataInterface $rdata |
48
|
|
|
* @param int $ttl |
49
|
|
|
* @param string $class |
50
|
|
|
* @param string $comment |
51
|
|
|
* |
52
|
|
|
* @return ResourceRecord |
53
|
|
|
*/ |
54
|
15 |
|
public static function create(string $name, RdataInterface $rdata, int $ttl = null, string $class = Classes::INTERNET, string $comment = null): ResourceRecord |
55
|
|
|
{ |
56
|
15 |
|
$rr = new self(); |
57
|
15 |
|
$rr->setName($name); |
58
|
15 |
|
$rr->setRdata($rdata); |
59
|
15 |
|
$rr->setTtl($ttl); |
60
|
15 |
|
$rr->setClass($class); |
61
|
15 |
|
$rr->setComment($comment); |
62
|
|
|
|
63
|
15 |
|
return $rr; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the class for the resource record |
68
|
|
|
* Usually one of IN, HS, or CH. |
69
|
|
|
* |
70
|
|
|
* @param string $class |
71
|
|
|
* |
72
|
|
|
* @throws \UnexpectedValueException |
73
|
|
|
*/ |
74
|
32 |
|
public function setClass(?string $class): void |
75
|
|
|
{ |
76
|
32 |
|
if (null !== $class && !Classes::isValid($class)) { |
77
|
1 |
|
throw new \UnexpectedValueException(sprintf('No such class as "%s"', $class)); |
78
|
|
|
} |
79
|
|
|
|
80
|
32 |
|
$this->class = $class; |
81
|
32 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the name for the resource record. |
85
|
|
|
* Eg. "subdomain.example.com.". |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
*/ |
89
|
32 |
|
public function setName(string $name): void |
90
|
|
|
{ |
91
|
32 |
|
$this->name = $name; |
92
|
32 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param RdataInterface $rdata |
96
|
|
|
*/ |
97
|
30 |
|
public function setRdata(?RdataInterface $rdata): void |
98
|
|
|
{ |
99
|
30 |
|
$this->rdata = $rdata; |
100
|
30 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
27 |
|
public function getClass(): ?string |
106
|
|
|
{ |
107
|
27 |
|
return $this->class; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int|null |
112
|
|
|
*/ |
113
|
|
|
public function getClassId(): ?int |
114
|
|
|
{ |
115
|
|
|
if (!isset($this->class)) { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return Classes::getClassId($this->class); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the time to live. |
124
|
|
|
* |
125
|
|
|
* @param int $ttl |
126
|
|
|
*/ |
127
|
33 |
|
public function setTtl(?int $ttl): void |
128
|
|
|
{ |
129
|
33 |
|
$this->ttl = $ttl; |
130
|
33 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
26 |
|
public function getName(): ?string |
136
|
|
|
{ |
137
|
26 |
|
return $this->name; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
23 |
|
public function getType(): ?string |
144
|
|
|
{ |
145
|
23 |
|
if (null === $this->rdata) { |
146
|
2 |
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
22 |
|
return $this->rdata->getType(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return RdataInterface |
154
|
|
|
*/ |
155
|
21 |
|
public function getRdata(): ?RdataInterface |
156
|
|
|
{ |
157
|
21 |
|
return $this->rdata; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
23 |
|
public function getTtl(): ?int |
164
|
|
|
{ |
165
|
23 |
|
return $this->ttl; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set a comment for the record. |
170
|
|
|
* |
171
|
|
|
* @param string $comment |
172
|
|
|
*/ |
173
|
33 |
|
public function setComment(?string $comment): void |
174
|
|
|
{ |
175
|
33 |
|
$this->comment = $comment; |
176
|
33 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the record's comment. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
10 |
|
public function getComment(): ?string |
184
|
|
|
{ |
185
|
10 |
|
return $this->comment; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|