Parameters::offsetGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher\Event;
6
7
final class Parameters implements ParametersInterface
8
{
9
    public function __construct(private array $params = [])
10
    {
11
        $this->setParams($params);
12
    }
13
14
    public function count(): int
15
    {
16
        return count($this->params);
17
    }
18
19
    public function isEmpty(): bool
20
    {
21 24
        return empty($this->params);
22
    }
23 24
24
    public function getParams(): array
25
    {
26
        return $this->params;
27
    }
28
29 3
    public function setParams(array $params): void
30
    {
31 3
        $this->removeParams();
32
        foreach ($params as $name => $value) {
33
            $this->setParam($name, $value);
34
        }
35
    }
36
37 2
    public function removeParams(array $params = []): void
38
    {
39 2
        if (empty($params)) {
40
            $params = $this->getKeys();
41
        }
42
43
        foreach ($params as $name) {
44
            $this->removeParam($name);
45 7
        }
46
    }
47 7
48
    /**
49
     * @return array<int, mixed>
50
     */
51
    public function getKeys(): array
52
    {
53 24
        return array_keys($this->params);
54
    }
55 24
56
    public function getValues(): array
57 24
    {
58 14
        return array_values($this->params);
59
    }
60
61
    public function offsetExists(mixed $offset): bool
62
    {
63
        return $this->hasParam($offset);
64
    }
65 24
66
    /**
67 24
     * @param string $name
68 24
     *
69
     * @return bool
70
     */
71 24
    public function hasParam(string $name): bool
72 1
    {
73
        return array_key_exists($name, $this->params);
74
    }
75
76
    public function offsetGet(mixed $offset): mixed
77
    {
78
        return $this->getParam($offset);
79 24
    }
80
81 24
    public function getParam(string $name, mixed $default = null): mixed
82
    {
83
        if ($this->hasParam($name)) {
84
            return $this->params[$name];
85
        }
86
        return $default;
87 1
    }
88
89 1
    public function offsetSet(mixed $offset, mixed $value): void
90
    {
91
        $this->setParam($offset, $value);
92
    }
93
94
    public function setParam(string $name, mixed $value): void
95
    {
96
        $this->params[$name] = $value;
97 1
    }
98
99 1
    public function offsetUnset(mixed $offset): void
100
    {
101
        $this->removeParam($offset);
102
    }
103
104
    public function removeParam(string $name): bool
105
    {
106
        if ($this->hasParam($name)) {
107 9
            unset($this->params[$name]);
108
            return true;
109 9
        }
110
        return false;
111
    }
112
113
    /**
114
     * @return \ArrayIterator<int|string, mixed>
115
     */
116
    public function getIterator(): \ArrayIterator
117 1
    {
118
        return new \ArrayIterator($this->params);
119 1
    }
120
}
121