1
|
|
|
<?php |
2
|
|
|
namespace RemotelyLiving\PHPDNS\Entities; |
3
|
|
|
|
4
|
|
|
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable; |
5
|
|
|
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable; |
6
|
|
|
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class DNSRecordCollection extends EntityAbstract implements \ArrayAccess, \Iterator, \Countable, Arrayable, Serializable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \ArrayIterator |
12
|
|
|
*/ |
13
|
|
|
private $records; |
14
|
|
|
|
15
|
|
|
public function __construct(DNSRecord ...$records) |
16
|
|
|
{ |
17
|
|
|
$this->records = new \ArrayIterator($records); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function toArray(): array |
21
|
|
|
{ |
22
|
|
|
return $this->records->getArrayCopy(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function pickFirst(): ?DNSRecord |
26
|
|
|
{ |
27
|
|
|
$copy = $this->records->getArrayCopy(); |
28
|
|
|
|
29
|
|
|
return array_shift($copy); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function filteredByType(DNSRecordType $type): self |
33
|
|
|
{ |
34
|
|
|
return new self(...array_filter($this->records->getArrayCopy(), function (DNSRecord $record) use ($type) { |
35
|
|
|
return $record->getType()->equals($type); |
36
|
|
|
})); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function has(DNSRecord $lookupRecord): bool |
40
|
|
|
{ |
41
|
|
|
foreach ($this->records->getArrayCopy() as $record) { |
42
|
|
|
if ($lookupRecord->equals($record)) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function current(): ?DNSRecord |
51
|
|
|
{ |
52
|
|
|
return $this->records->current(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function next(): void |
56
|
|
|
{ |
57
|
|
|
$this->records->next(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int|string|bool |
62
|
|
|
*/ |
63
|
|
|
public function key() |
64
|
|
|
{ |
65
|
|
|
return $this->records->key(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function valid(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->records->valid(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function rewind(): void |
74
|
|
|
{ |
75
|
|
|
$this->records->rewind(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function offsetExists($offset): bool |
79
|
|
|
{ |
80
|
|
|
return $this->records->offsetExists($offset); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function offsetGet($offset): DNSRecord |
84
|
|
|
{ |
85
|
|
|
return $this->records->offsetGet($offset); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws \RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
public function offsetSet($offset, $value): void |
92
|
|
|
{ |
93
|
|
|
if (!$value instanceof DNSRecord) { |
94
|
|
|
throw new InvalidArgumentException('Invalid value'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->records->offsetSet($offset, /** @scrutinizer ignore-type */ $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function offsetUnset($offset): void |
101
|
|
|
{ |
102
|
|
|
$this->records->offsetUnset($offset); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function count(): int |
106
|
|
|
{ |
107
|
|
|
return $this->records->count(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isEmpty(): bool |
111
|
|
|
{ |
112
|
|
|
return $this->count() === 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function serialize(): string |
116
|
|
|
{ |
117
|
|
|
return \serialize($this->records->getArrayCopy()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $serialized |
122
|
|
|
*/ |
123
|
|
|
public function unserialize($serialized): void |
124
|
|
|
{ |
125
|
|
|
$this->records = new \ArrayIterator(\unserialize($serialized)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function jsonSerialize(): array |
129
|
|
|
{ |
130
|
|
|
return $this->toArray(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function withUniqueValuesExcluded(): self |
134
|
|
|
{ |
135
|
|
|
return $this->filterValues(function (DNSRecord $candidateRecord, DNSRecordCollection $remaining): bool { |
136
|
|
|
return $remaining->has($candidateRecord); |
137
|
|
|
})->withUniqueValues(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function withUniqueValues(): self |
141
|
|
|
{ |
142
|
|
|
return $this->filterValues(function (DNSRecord $candidateRecord, DNSRecordCollection $remaining): bool { |
143
|
|
|
return !$remaining->has($candidateRecord); |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function filterValues(callable $eval): self |
148
|
|
|
{ |
149
|
|
|
$filtered = new self(); |
150
|
|
|
$records = $this->records->getArrayCopy(); |
151
|
|
|
|
152
|
|
|
/** @var \RemotelyLiving\PHPDNS\Entities\DNSRecord $record */ |
153
|
|
|
while ($record = array_shift($records)) { |
154
|
|
|
if ($eval($record, new self(...$records))) { |
155
|
|
|
$filtered[] = $record; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $filtered; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|