FlexibleData   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 118
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 2 1
A __construct() 0 2 1
A offsetSet() 0 2 1
A offsetUnset() 0 2 1
A key() 0 2 1
A current() 0 2 1
A valid() 0 2 1
A offsetGet() 0 2 1
A rewind() 0 2 1
A next() 0 2 1
A offsetExists() 0 2 1
A find() 0 14 4
A get() 0 6 2
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 12
    public function __construct(array $data = []) {
20 12
        $this->data = $data;
21 12
    }
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
     * @throws \OutOfBoundsException
34
     */
35 5
    public function get($key) {
36 5
        if (!array_key_exists($key, $this->data)) {
37 1
            throw new \OutOfBoundsException("No value set for $key.");
38
        }
39
40 4
        return $this->find($key);
41
    }
42
43
    /**
44
     * @param string|int $key
45
     * @param mixed $defaultValue
46
     * @return mixed
47
     */
48 7
    public function find($key, $defaultValue = null) {
49 7
        if (!array_key_exists($key, $this->data)) {
50 1
            return $defaultValue;
51
        }
52
53 6
        if (\is_scalar($this->data[$key])) {
54 5
            return $this->data[$key];
55
        }
56
57 4
        if (\is_array($this->data[$key])) {
58 4
            return new FlexibleData($this->data[$key]);
59
        }
60
61 2
        return $this->data[$key];
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 2
    public function offsetExists($offset) {
68 2
        return isset($this->data[$offset]);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 5
    public function &offsetGet($offset) {
75 5
        return $this->data[$offset];
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 3
    public function offsetSet($offset, $value) {
82 3
        $this->data[$offset] = $value;
83 3
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 1
    public function offsetUnset($offset) {
89 1
        unset($this->data[$offset]);
90 1
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 2
    public function current() {
96 2
        return new FlexibleData($this->data[$this->position]);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 2
    public function next() {
103 2
        ++$this->position;
104 2
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 1
    public function key() {
110 1
        return $this->position;
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 2
    public function valid() {
117 2
        return isset($this->data[$this->position]);
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 2
    public function rewind() {
124 2
        $this->position = 0;
125 2
    }
126
}
127