Passed
Pull Request — master (#2)
by Andreas
11:41
created

ParamsBag::offsetGet()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
rs 10
cc 3
nc 4
nop 1
crap 3.2098
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 18
    public function __construct(array $params = [])
20
    {
21 18
        $this->params = $params;
22
    }
23
24 8
    public function get($name, $default = null)
25
    {
26 8
        return $this->offsetExists($name)
27
            ? $this->offsetGet($name)
28 8
            : $default;
29
    }
30
31 1
    public function __get($name)
32
    {
33 1
        return $this->offsetGet($name);
34
    }
35
36
    public function __set($name, $value)
37
    {
38
        $this->offsetSet($name, $value);
39
    }
40
41
    public function __isset($name)
42
    {
43
        return $this->offsetExists($name);
44
    }
45
46
    /**
47
     * Returns an array from params.
48
     *
49
     * @return array
50
     */
51
    public function getArrayCopy()
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
    public function current()
68
    {
69
        return current($this->params);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function key()
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
    public function offsetSet($offset, $value): void
100
    {
101
        if (is_null($offset)) {
102
            $this->params[] = $value;
103
        } else {
104
            $this->params[$offset] = $value;
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 8
    public function offsetExists($offset): bool
112
    {
113 8
        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 1
    public function offsetGet($offset)
128
    {
129 1
        $value = isset($this->params[$offset])
130 1
            ? $this->params[$offset]
131
            : null;
132
133 1
        if (is_array($value)) {
134
            return new self($value);
135
        }
136
137 1
        return $value;
138
    }
139
}
140