KeyValueList::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus\Utility;
5
6
use Generator;
7
use LogicException;
8
9
trait KeyValueList
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $elements = [];
15
16
    public function set($id, $value): void
17
    {
18
        $this->elements[$id] = $value;
19
    }
20
21
    public function get($id)
22
    {
23
        if (!$this->has($id)) {
24
            throw new LogicException(
25
                sprintf("[%s] Item not found (%s).", get_class($this), $id)
26
            );
27
        }
28
29
        return $this->elements[$id];
30
    }
31
32
    public function has($id): bool
33
    {
34
        return array_key_exists($id, $this->elements);
35
    }
36
37
    public function delete($id): void
38
    {
39
        unset($this->elements[$id]);
40
    }
41
42
    public function getIterator(): Generator
43
    {
44
        foreach ($this->elements as $id => $element) {
45
            yield $id => $element;
46
        }
47
    }
48
49
    public function count(): int
50
    {
51
        return count($this->elements);
52
    }
53
54
    public function isEmpty(): bool
55
    {
56
        return $this->count() == 0;
57
    }
58
59
    public function toArray(): array
60
    {
61
        return $this->elements;
62
    }
63
}
64