Passed
Push — master ( 57f961...375149 )
by Andreas
17:52 queued 06:08
created

ParamsBag::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Larium\Pay;
6
7
use ArrayAccess;
8
use Iterator;
9
10
class ParamsBag implements Iterator, ArrayAccess
11
{
12
    /**
13
     * The array of option values.
14
     *
15
     * @var array
16
     */
17
    private $params = [];
18
19 20
    public function __construct(array $params = [])
20
    {
21 20
        $this->params = $params;
22
    }
23
24 9
    public function get(mixed $name, mixed $default = null): mixed
25
    {
26 9
        return $this->offsetExists($name)
27
            ? $this->offsetGet($name)
28 9
            : $default;
29
    }
30
31 3
    public function __get(mixed $name): mixed
32
    {
33 3
        return $this->offsetGet($name);
34
    }
35
36 1
    public function __set(mixed $name, mixed $value): void
37
    {
38 1
        $this->offsetSet($name, $value);
39
    }
40
41
    public function __isset($name): bool
42
    {
43
        return $this->offsetExists($name);
44
    }
45
46
    /**
47
     * Returns an array from params.
48
     *
49
     * @return array
50
     */
51
    public function getArrayCopy(): array
52
    {
53
        return $this->params;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function rewind(): void
60
    {
61
        reset($this->params);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function current(): mixed
68
    {
69 1
        return current($this->params);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function key(): mixed
76
    {
77
        return key($this->params);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function next(): void
84
    {
85
        next($this->params);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function valid(): bool
92
    {
93
        return current($this->params) !== false;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function offsetSet($offset, $value): void
100
    {
101 1
        if (is_null($offset)) {
102
            $this->params[] = $value;
103
        } else {
104 1
            $this->params[$offset] = $value;
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 9
    public function offsetExists($offset): bool
112
    {
113 9
        return array_key_exists($offset, $this->params);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function offsetUnset($offset): void
120
    {
121
        unset($this->params[$offset]);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 3
    public function offsetGet($offset): mixed
128
    {
129 3
        $value = isset($this->params[$offset])
130 3
            ? $this->params[$offset]
131 1
            : null;
132
133 3
        if (is_array($value)) {
134
            return new self($value);
135
        }
136
137 3
        return $value;
138
    }
139
}
140