Passed
Pull Request — master (#1)
by Martin
01:58
created

FlexibleData::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace KingsonDe\Marshal\Data;
6
7
class FlexibleData implements DataStructure, \ArrayAccess, \Iterator {
8
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    /**
15
     * @var int
16
     */
17
    private $position = 0;
18
19 8
    public function __construct(array $data = []) {
20 8
        $this->data = $data;
21 8
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 2
    public function build() {
27 2
        return $this->data;
28
    }
29
30
    /**
31
     * @param string|int $key
32
     * @return mixed
33
     */
34 4
    public function get($key) {
35 4
        if (!isset($this->data[$key])) {
36 1
            return null;
37
        }
38
39 4
        if (\is_scalar($this->data[$key])) {
40 2
            return $this->data[$key];
41
        }
42
43 3
        if (\is_array($this->data[$key])) {
44 3
            return new FlexibleData($this->data[$key]);
45
        }
46
47 1
        return $this->data[$key];
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function offsetExists($offset) {
54 1
        return isset($this->data[$offset]);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 4
    public function &offsetGet($offset) {
61 4
        return $this->data[$offset];
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 2
    public function offsetSet($offset, $value) {
68 2
        $this->data[$offset] = $value;
69 2
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    public function offsetUnset($offset) {
75 1
        unset($this->data[$offset]);
76 1
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 2
    public function current() {
82 2
        return new FlexibleData($this->data[$this->position]);
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 2
    public function next() {
89 2
        ++$this->position;
90 2
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 1
    public function key() {
96 1
        return $this->position;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 2
    public function valid() {
103 2
        return isset($this->data[$this->position]);
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 2
    public function rewind() {
110 2
        $this->position = 0;
111 2
    }
112
}
113