Completed
Pull Request — master (#1)
by Martin
02:24
created

FlexibleData::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
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 7
    public function __construct(array $data = []) {
20 7
        $this->data = $data;
21 7
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 1
    public function build() {
27 1
        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
        if (null === $offset) {
69
            $this->data[] = $value;
70
        } else {
71 2
            $this->data[$offset] = $value;
72
        }
73 2
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 1
    public function offsetUnset($offset) {
79 1
        unset($this->data[$offset]);
80 1
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 2
    public function current() {
86 2
        return new FlexibleData($this->data[$this->position]);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92 2
    public function next() {
93 2
        ++$this->position;
94 2
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 1
    public function key() {
100 1
        return $this->position;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106 2
    public function valid() {
107 2
        return isset($this->data[$this->position]);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 2
    public function rewind() {
114 2
        $this->position = 0;
115 2
    }
116
}
117