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; |
9
|
|
|
|
10
|
|
|
use LTDBeget\dns\configurator\errors\ValidationError; |
11
|
|
|
use LTDBeget\dns\configurator\traits\RecordsIterateTrait; |
12
|
|
|
use LTDBeget\dns\configurator\validators\CnameNumberCheck; |
13
|
|
|
use LTDBeget\dns\configurator\validators\ConflictTypesValidator; |
14
|
|
|
use LTDBeget\dns\configurator\validators\DnsZoneDomainNameValidator; |
15
|
|
|
use LTDBeget\dns\configurator\validators\OutOfZoneDataValidator; |
16
|
|
|
use LTDBeget\dns\configurator\validators\SoaNumberCheck; |
17
|
|
|
use LTDBeget\dns\configurator\Zone; |
18
|
|
|
use LTDBeget\dns\configurator\zoneEntities\record\base\Record; |
19
|
|
|
use LTDBeget\dns\enums\eErrorCode; |
20
|
|
|
use LTDBeget\dns\enums\eRecordNotification; |
21
|
|
|
use LTDBeget\dns\enums\eRecordType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Node |
25
|
|
|
* |
26
|
|
|
* @package LTDBeget\dns\configurator\zoneEntities |
27
|
|
|
*/ |
28
|
|
|
class Node |
29
|
|
|
{ |
30
|
|
|
use RecordsIterateTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Zone |
34
|
|
|
*/ |
35
|
|
|
private $zone; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $name; |
40
|
|
|
/** |
41
|
|
|
* @var RecordAppender |
42
|
|
|
*/ |
43
|
|
|
private $recordAppender; |
44
|
|
|
/** |
45
|
|
|
* @var RecordsStore |
46
|
|
|
*/ |
47
|
|
|
private $recordsStore; |
48
|
|
|
/** |
49
|
|
|
* @var RecordsStore |
50
|
|
|
*/ |
51
|
|
|
private $removedRecordsStore; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Zone $zone |
55
|
|
|
* @param $name |
56
|
|
|
*/ |
57
|
1 |
|
public function __construct(Zone $zone, string $name) |
58
|
|
|
{ |
59
|
1 |
|
$this->zone = $zone; |
60
|
1 |
|
$this->name = mb_strtolower(trim($name)); |
61
|
1 |
|
$this->recordAppender = new RecordAppender($this); |
62
|
1 |
|
$this->recordsStore = new RecordsStore(); |
63
|
1 |
|
$this->removedRecordsStore = new RecordsStore(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return RecordAppender |
68
|
|
|
*/ |
69
|
1 |
|
public function getRecordAppender() : RecordAppender |
70
|
|
|
{ |
71
|
1 |
|
return $this->recordAppender; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return \Generator|Record[] |
76
|
|
|
*/ |
77
|
|
|
public function iterateRemoved() : \Generator |
78
|
|
|
{ |
79
|
|
|
foreach ($this->getRemovedRecordsStore()->iterate() as $record) { |
80
|
|
|
yield $record; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return RecordsStore |
86
|
|
|
*/ |
87
|
|
|
protected function getRemovedRecordsStore() |
88
|
|
|
{ |
89
|
|
|
return $this->removedRecordsStore; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param eRecordType|NULL $type |
94
|
|
|
*/ |
95
|
|
|
public function removeRecords(eRecordType $type = NULL) |
96
|
|
|
{ |
97
|
|
|
foreach ($this->iterateRecords($type) as $record) { |
98
|
|
|
$record->remove(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param eRecordType|null $type |
104
|
|
|
* @return \Generator|Record[] |
105
|
|
|
*/ |
106
|
1 |
|
public function iterateRecords(eRecordType $type = NULL) : \Generator |
107
|
|
|
{ |
108
|
1 |
|
foreach ($this->getRecordsStore()->iterate($type) as $record) { |
109
|
1 |
|
yield $record; |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return RecordsStore |
115
|
|
|
*/ |
116
|
1 |
|
protected function getRecordsStore() : RecordsStore |
117
|
|
|
{ |
118
|
1 |
|
return $this->recordsStore; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isEmptyNode() : bool |
125
|
|
|
{ |
126
|
|
|
return $this->getRecordsStore()->count() === 0; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @internal |
131
|
|
|
* @param Record $record |
132
|
|
|
* @param eRecordNotification $notification |
133
|
|
|
* @return Node |
134
|
|
|
*/ |
135
|
1 |
|
public function notify(Record $record, eRecordNotification $notification) : Node |
136
|
|
|
{ |
137
|
|
|
switch ($notification) { |
138
|
1 |
|
case eRecordNotification::ADD: |
139
|
1 |
|
$this->recordsStore->append($record); |
140
|
1 |
|
break; |
141
|
|
|
case eRecordNotification::REMOVE: |
142
|
|
|
$this->recordsStore->remove($record); |
143
|
|
|
$this->removedRecordsStore->append($record); |
144
|
|
|
break; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
public function sort() |
151
|
|
|
{ |
152
|
1 |
|
$this->getRecordsStore()->sort(); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @internal |
157
|
|
|
* Full validate zone via build in validators |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
1 |
|
public function validate() : bool |
161
|
|
|
{ |
162
|
1 |
|
$errorsStore = $this->getZone()->getErrorsStore(); |
163
|
|
|
|
164
|
1 |
|
if (!ConflictTypesValidator::validate($this)) { |
165
|
|
|
$errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::CONFLICT_RECORD_TYPES_ERROR())); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
if (!CnameNumberCheck::validate($this)) { |
169
|
|
|
$errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::MULTIPLE_CNAME_ERROR())); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
if ($this->getName() === '@' && !SoaNumberCheck::validate($this)) { |
173
|
|
|
$errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::SOA_ERROR())); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
if (!OutOfZoneDataValidator::validate($this)) { |
177
|
|
|
$errorsStore->add(ValidationError::makeNodeError($this, eErrorCode::OUT_OF_ZONE_DATE())); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
$isValidNodeName = DnsZoneDomainNameValidator::validate($this->getName()); |
181
|
1 |
|
foreach ($this->iterateRecords() as $record) { |
182
|
1 |
|
if(!$isValidNodeName) { |
183
|
|
|
$errorsStore->add(ValidationError::makeRecordError($record, eErrorCode::WRONG_NODE_NAME(), 'name')); |
184
|
|
|
} |
185
|
|
|
/** @noinspection PhpInternalEntityUsedInspection */ |
186
|
1 |
|
$record->validate(); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
return !$errorsStore->isHasErrors(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return Zone |
194
|
|
|
*/ |
195
|
1 |
|
public function getZone() : Zone |
196
|
|
|
{ |
197
|
1 |
|
return $this->zone; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
1 |
|
public function getName() : string |
204
|
|
|
{ |
205
|
1 |
|
return $this->name; |
206
|
|
|
} |
207
|
|
|
} |