1 | <?php |
||
26 | class Zone |
||
27 | { |
||
28 | use RecordsIterateTrait; |
||
29 | |||
30 | /** |
||
31 | * @var String |
||
32 | */ |
||
33 | private $origin; |
||
34 | /** |
||
35 | * @var Node[] $nodes |
||
36 | */ |
||
37 | private $nodes = []; |
||
38 | /** |
||
39 | * @var ErrorsStore |
||
40 | */ |
||
41 | private $errorsStore; |
||
42 | |||
43 | /** |
||
44 | * @param $origin |
||
45 | */ |
||
46 | 1 | public function __construct(string $origin) |
|
47 | { |
||
48 | 1 | $this->origin = mb_strtolower(trim($origin)); |
|
49 | 1 | $this->errorsStore = new ErrorsStore(); |
|
50 | 1 | } |
|
51 | |||
52 | /** |
||
53 | * @param string $origin |
||
54 | * @param string $plainData |
||
55 | * @return Zone |
||
56 | */ |
||
57 | 1 | public static function fromString(string $origin, string $plainData) : Zone |
|
58 | { |
||
59 | 1 | return PlainDeserializer::deserialize(new static($origin), $plainData); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $origin |
||
64 | * @param array $arrayData |
||
65 | * @return Zone |
||
66 | */ |
||
67 | public static function fromArray(string $origin, array $arrayData) : Zone |
||
68 | { |
||
69 | return ArrayDeserializer::deserialize(new static($origin), $arrayData); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | 1 | public function __toString() : string |
|
76 | { |
||
77 | 1 | $this->sort(); |
|
78 | 1 | $records = []; |
|
79 | 1 | foreach ($this->iterateRecords() as $record) { |
|
80 | 1 | $records[] = (string) $record; |
|
81 | } |
||
82 | |||
83 | 1 | return implode("\n", $records); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Sort zone by node names and record types |
||
88 | */ |
||
89 | 1 | public function sort() |
|
90 | { |
||
91 | 1 | $nodes = $this->nodes; |
|
92 | /** @var Node[] $_nodes */ |
||
93 | 1 | $_nodes = []; |
|
94 | 1 | foreach (['@', 'www', '*'] as $node_name) { |
|
95 | 1 | if (isset($nodes[$node_name])) { |
|
96 | 1 | $_nodes[$node_name] = $nodes[$node_name]; |
|
97 | 1 | unset($nodes[$node_name]); |
|
98 | } |
||
99 | } |
||
100 | |||
101 | 1 | ksort($nodes); |
|
102 | 1 | $this->nodes = array_merge($_nodes, $nodes); |
|
103 | |||
104 | 1 | foreach ($this->iterateNodes() as $node) { |
|
105 | 1 | $node->sort(); |
|
106 | } |
||
107 | 1 | } |
|
108 | |||
109 | /** |
||
110 | * @return Node[] |
||
111 | */ |
||
112 | 1 | public function iterateNodes() |
|
113 | { |
||
114 | 1 | foreach ($this->nodes as $node) { |
|
115 | 1 | yield $node; |
|
116 | } |
||
117 | 1 | } |
|
118 | |||
119 | /** |
||
120 | * @param eRecordType|null $type |
||
121 | * @return Record[] |
||
122 | */ |
||
123 | 1 | public function iterateRecords(eRecordType $type = NULL) |
|
124 | { |
||
125 | 1 | foreach ($this->iterateNodes() as $node) { |
|
126 | 1 | foreach ($node->iterateRecords($type) as $record) { |
|
127 | 1 | yield $record; |
|
128 | } |
||
129 | } |
||
130 | 1 | } |
|
131 | |||
132 | /** |
||
133 | * @return array |
||
134 | */ |
||
135 | 1 | public function toArray() : array |
|
136 | { |
||
137 | 1 | $this->sort(); |
|
138 | 1 | $records = []; |
|
139 | 1 | foreach ($this->iterateRecords() as $record) { |
|
140 | 1 | $records[] = $record->toArray(); |
|
141 | } |
||
142 | |||
143 | 1 | return $records; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getNodeNames() : array |
||
153 | |||
154 | /** |
||
155 | * @param $name |
||
156 | * @return Node |
||
157 | */ |
||
158 | 1 | public function getNode(string $name) : Node |
|
168 | |||
169 | /** |
||
170 | * Check is node by given name already exists |
||
171 | * |
||
172 | * @param $name |
||
173 | * @return bool |
||
174 | */ |
||
175 | 1 | public function isNodeExist($name) : bool |
|
179 | |||
180 | /** |
||
181 | * @param eRecordType|NULL $type |
||
182 | */ |
||
183 | public function removeRecords(eRecordType $type = NULL) |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isEmptyZone() : bool |
||
205 | |||
206 | /** |
||
207 | * Full validate zone via build in validators |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | 1 | public function validate() : bool |
|
212 | { |
||
213 | 1 | $errorsStore = $this->getErrorsStore(); |
|
214 | 1 | $errorsStore->clear(); |
|
215 | |||
216 | 1 | foreach ($this->iterateNodes() as $node) { |
|
217 | /** @noinspection PhpInternalEntityUsedInspection */ |
||
218 | 1 | $node->validate(); |
|
219 | } |
||
220 | |||
221 | 1 | if (!HostnameValidator::validate($this->getOrigin())) { |
|
222 | $errorsStore->add(ValidationError::makeZoneError($this, eErrorCode::WRONG_ORIGIN())); |
||
223 | } |
||
224 | |||
225 | 1 | return !$errorsStore->isHasErrors(); |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return ErrorsStore |
||
230 | */ |
||
231 | 1 | public function getErrorsStore() |
|
235 | |||
236 | /** |
||
237 | * @return String |
||
238 | */ |
||
239 | 1 | public function getOrigin() : string |
|
243 | |||
244 | /** |
||
245 | * @return Zone |
||
246 | */ |
||
247 | public function clear() : Zone |
||
256 | |||
257 | /** |
||
258 | * @return Zone |
||
259 | */ |
||
260 | private function dropNodes() : Zone |
||
266 | } |