KeyValueList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A delete() 0 3 1
A set() 0 3 1
A count() 0 3 1
A getIterator() 0 4 2
A get() 0 7 2
A isEmpty() 0 3 1
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
trait KeyValueList
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $elements = [];
16
17
    public function set($id, $value): void
18
    {
19
        $this->elements[$id] = $value;
20
    }
21
22
    public function get($id)
23
    {
24
        if (!$this->has($id)) {
25
            throw NotFoundException::noEntryWasFound((string) $id);
26
        }
27
28
        return $this->elements[$id];
29
    }
30
31
    public function has($id): bool
32
    {
33
        return array_key_exists($id, $this->elements);
34
    }
35
36
    public function delete($id): void
37
    {
38
        unset($this->elements[$id]);
39
    }
40
41
    public function getIterator(): Generator
42
    {
43
        foreach ($this->elements as $id => $element) {
44
            yield $id => $element;
45
        }
46
    }
47
48
    public function count(): int
49
    {
50
        return count($this->elements);
51
    }
52
53
    public function isEmpty(): bool
54
    {
55
        return $this->count() == 0;
56
    }
57
}
58