1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author: Viskov Sergey |
4
|
|
|
* @date : 4/12/16 |
5
|
|
|
* @time : 1:00 PM |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace LTDBeget\dns\configurator\zoneEntities\record; |
9
|
|
|
|
10
|
|
|
use LTDBeget\dns\configurator\errors\ValidationError; |
11
|
|
|
use LTDBeget\dns\configurator\zoneEntities\Node; |
12
|
|
|
use LTDBeget\dns\configurator\zoneEntities\record\base\Record; |
13
|
|
|
use LTDBeget\dns\enums\eErrorCode; |
14
|
|
|
use LTDBeget\dns\enums\eRecordType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class TxtRecord |
18
|
|
|
* |
19
|
|
|
* @package LTDBeget\dns\configurator\zoneEntities\record |
20
|
|
|
*/ |
21
|
|
|
class TxtRecord extends Record |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var String |
25
|
|
|
*/ |
26
|
|
|
protected $txtData; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* TxtRecord constructor. |
30
|
|
|
* |
31
|
|
|
* @param Node $node |
32
|
|
|
* @param int $ttl |
33
|
|
|
* @param string $txtData |
34
|
|
|
*/ |
35
|
1 |
|
public function __construct(Node $node, int $ttl, string $txtData) |
36
|
|
|
{ |
37
|
1 |
|
$this->txtData = $this->sanitizeTxtData($txtData); |
38
|
1 |
|
parent::__construct($node, eRecordType::TXT(), $ttl); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $txtData |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
private function sanitizeTxtData(string $txtData) : string |
46
|
|
|
{ |
47
|
1 |
|
$txtDataArray = explode('\"', trim($txtData, '"')); |
48
|
|
|
$txtDataArray = array_map(function ($value) { |
49
|
1 |
|
return str_replace('"', '\"', $value); |
50
|
1 |
|
}, $txtDataArray); |
51
|
|
|
|
52
|
1 |
|
return implode('\"', $txtDataArray); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
1 |
|
public function __toString() : string |
59
|
|
|
{ |
60
|
1 |
|
$spited_string = str_split($this->getTxtData(), 255); |
61
|
1 |
|
$spited_with_quotes = array_map(function ($value) { |
62
|
1 |
|
return "\"$value\""; |
63
|
1 |
|
}, $spited_string); |
64
|
|
|
|
65
|
1 |
|
$char_sets = implode("\n", $spited_with_quotes); |
66
|
|
|
|
67
|
1 |
|
if (count($spited_with_quotes) > 1) { |
68
|
|
|
$rdataString = " ( \n" . $char_sets . "\n ) \n"; |
69
|
|
|
} else { |
70
|
1 |
|
$rdataString = ' ' . $char_sets; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $this->getMainRecordPart() . $rdataString; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return String |
78
|
|
|
*/ |
79
|
1 |
|
public function getTxtData() : string |
80
|
|
|
{ |
81
|
1 |
|
return $this->txtData; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $txtData |
86
|
|
|
* @return TxtRecord |
87
|
|
|
*/ |
88
|
|
|
public function setTxtData(string $txtData) : TxtRecord |
89
|
|
|
{ |
90
|
|
|
return $this->setAttribute('txtData', $this->sanitizeTxtData($txtData)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
1 |
|
public function validate() : bool |
97
|
|
|
{ |
98
|
1 |
|
$errorStorage = $this->getNode()->getZone()->getErrorsStore(); |
99
|
|
|
|
100
|
1 |
|
if (strlen($this->getTxtData()) === 0) { |
101
|
|
|
$errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::EMPTY_TXT(), 'txtData')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
105
|
1 |
|
return parent::validate(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
1 |
|
protected function recordDataToArray() : array |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
1 |
|
'TXTDATA' => $this->getTxtData() |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
} |