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
|
|
|
* @see https://tools.ietf.org/html/rfc1035#section-3.3.9 |
18
|
|
|
*/ |
19
|
|
|
class MX implements RdataInterface |
20
|
|
|
{ |
21
|
|
|
use RdataTrait; |
22
|
|
|
|
23
|
|
|
const TYPE = 'MX'; |
24
|
|
|
const TYPE_CODE = 15; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int|null |
28
|
|
|
*/ |
29
|
|
|
private $preference; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
private $exchange; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $exchange |
38
|
|
|
*/ |
39
|
21 |
|
public function setExchange(string $exchange): void |
40
|
|
|
{ |
41
|
21 |
|
$this->exchange = $exchange; |
42
|
21 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
3 |
|
public function getExchange(): ?string |
48
|
|
|
{ |
49
|
3 |
|
return $this->exchange; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $preference |
54
|
|
|
*/ |
55
|
21 |
|
public function setPreference(int $preference): void |
56
|
|
|
{ |
57
|
21 |
|
$this->preference = $preference; |
58
|
21 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int|null |
62
|
|
|
*/ |
63
|
2 |
|
public function getPreference(): ?int |
64
|
|
|
{ |
65
|
2 |
|
return $this->preference; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
* |
71
|
|
|
* @throws \InvalidArgumentException throws exception if preference or exchange have not been set |
72
|
|
|
*/ |
73
|
8 |
|
public function toText(): string |
74
|
|
|
{ |
75
|
8 |
|
if (null === $this->preference) { |
76
|
1 |
|
throw new \InvalidArgumentException('No preference has been set on MX object.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
7 |
|
if (null === $this->exchange) { |
80
|
1 |
|
throw new \InvalidArgumentException('No exchange has been set on MX object.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
return $this->preference.' '.$this->exchange; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function toWire(): string |
90
|
|
|
{ |
91
|
1 |
|
if (null === $this->preference) { |
92
|
|
|
throw new \InvalidArgumentException('No preference has been set on MX object.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
if (null === $this->exchange) { |
96
|
|
|
throw new \InvalidArgumentException('No exchange has been set on MX object.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return pack('n', $this->preference).self::encodeName($this->exchange); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
7 |
|
public static function fromText(string $text): RdataInterface |
106
|
|
|
{ |
107
|
7 |
|
$rdata = explode(' ', $text); |
108
|
7 |
|
$mx = new self(); |
109
|
7 |
|
$mx->setPreference((int) $rdata[0]); |
110
|
7 |
|
$mx->setExchange($rdata[1]); |
111
|
|
|
|
112
|
7 |
|
return $mx; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
119
|
|
|
{ |
120
|
1 |
|
$offset = 2; |
121
|
1 |
|
$mx = new self(); |
122
|
1 |
|
$mx->setPreference(unpack('n', $rdata)[1]); |
123
|
1 |
|
$mx->setExchange(self::decodeName($rdata, $offset)); |
124
|
|
|
|
125
|
1 |
|
return $mx; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|