Passed
Pull Request — master (#30)
by Christian
03:53
created

DNSRecordCollection::withUniqueValuesExcluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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