KeyValuePriorityList::hasId()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Utility\Lists;
5
6
use Generator;
7
use Habemus\Exception\NotFoundException;
8
use LogicException;
9
10
/*
11
 * The order of elements with identical priority is undefined
12
 */
13
trait KeyValuePriorityList
14
{
15
    /**
16
     * Fisrt level ordered by priority. Second level indexed by id.
17
     * @var array
18
     */
19
    private $elements = [];
20
21
    /**
22
     * keeps array ordered by priority after insertion.
23
     */
24
    public function set($id, $value, int $priority = 0): void
25
    {
26
        $this->setId($id, $value, $priority);
27
    }
28
29
    public function get($id)
30
    {
31
        return $this->getId($id);
32
    }
33
34
    public function has($id): bool
35
    {
36
        return $this->hasId($id);
37
    }
38
39
    public function delete($id): void
40
    {
41
        $this->deleteId($id);
42
    }
43
44
    private function getId($id)
45
    {
46
        foreach ($this->elements as $priority) {
47
            if (array_key_exists($id, $priority)) {
48
                return $priority[$id];
49
            }
50
        }
51
52
        throw NotFoundException::noEntryWasFound((string) $id);
53
    }
54
55
    private function setId($id, $value, int $priority = 0): void
56
    {
57
        $this->deleteId($id);
58
        if (!array_key_exists($priority, $this->elements)) {
59
            $this->elements[$priority] = [];
60
        }
61
62
        $this->elements[$priority][$id] = $value;
63
        ksort($this->elements);
64
    }
65
66
    private function hasId($id)
67
    {
68
        foreach ($this->elements as $priority) {
69
            if (array_key_exists($id, $priority)) {
70
                return true;
71
            }
72
        }
73
74
        return false;
75
    }
76
77
    private function deleteId($id): void
78
    {
79
        foreach ($this->elements as $priority => $items) {
80
            if (array_key_exists($id, $items)) {
81
                unset($this->elements[$priority][$id]);
82
                break;
83
            }
84
        }
85
    }
86
87
    public function getIterator(): Generator
88
    {
89
        foreach ($this->elements as $elements) {
90
            foreach ($elements as $id => $element) {
91
                yield $id => $element;
92
            }
93
        }
94
    }
95
96
    public function count(): int
97
    {
98
        $count = 0;
99
        foreach ($this->elements as $elements) {
100
            $count += count($elements);
101
        }
102
103
        return $count;
104
    }
105
106
    public function isEmpty(): bool
107
    {
108
        return $this->count() == 0;
109
    }
110
111
    public function toArray(): array
112
    {
113
        return iterator_to_array($this->getIterator());
114
    }
115
116
    public function getLowestPriority(): ?int
117
    {
118
        if (empty($this->elements)) {
119
            return null;
120
        }
121
        return max(array_keys(array_filter($this->elements)));
122
    }
123
124
    public function getHighestPriority(): ?int
125
    {
126
        if (empty($this->elements)) {
127
            return null;
128
        }
129
        return min(array_keys(array_filter($this->elements)));
130
    }
131
}
132