Passed
Push — master ( d65c20...a7ba41 )
by Christian
02:11
created

DNSRecordCollection::withUniqueValuesExcluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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|\RemotelyLiving\PHPDNS\Entities\DNSRecord[]
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
    public function offsetSet($offset, $value): void
89
    {
90
        if (!$value instanceof DNSRecord) {
91
            throw new InvalidArgumentException('Invalid value');
92
        }
93
94
        $this->records->offsetSet($offset, /** @scrutinizer ignore-type */ $value);
95
    }
96
97
    public function offsetUnset($offset): void
98
    {
99
        $this->records->offsetUnset($offset);
100
    }
101
102
    public function count(): int
103
    {
104
        return $this->records->count();
105
    }
106
107
    public function isEmpty(): bool
108
    {
109
        return $this->count() === 0;
110
    }
111
112
    public function serialize(): string
113
    {
114
        return \serialize($this->records->getArrayCopy());
115
    }
116
117
    public function unserialize($records): void
118
    {
119
        $this->records = new \ArrayIterator(\unserialize($records));
120
    }
121
122
    public function withUniqueValuesExcluded(): self
123
    {
124
        return $this->filterValues(function (DNSRecord $candidateRecord, DNSRecordCollection $remaining): bool {
125
            return $remaining->has($candidateRecord);
126
        });
127
    }
128
129
    public function withUniqueValues(): self
130
    {
131
        return $this->filterValues(function (DNSRecord $candidateRecord, DNSRecordCollection $remaining): bool {
132
            return !$remaining->has($candidateRecord);
133
        });
134
    }
135
136
    private function filterValues(callable $eval): self
137
    {
138
        $filtered = new self();
139
        $records = $this->records->getArrayCopy();
140
141
        /** @var \RemotelyLiving\PHPDNS\Entities\DNSRecord $record */
142
        while ($record = array_shift($records)) {
143
            $remaining = new self(...$records);
144
            if ($eval($record, $remaining)) {
145
                $filtered[] = $record;
146
            }
147
        }
148
149
        return $filtered;
150
    }
151
}
152