Collection   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 185
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A has() 0 4 1
A get() 0 4 2
A count() 0 4 1
A offsetExists() 0 4 1
A offsetSet() 0 10 2
A offsetGet() 0 4 2
A offsetUnset() 0 6 1
A rewind() 0 4 1
A current() 0 6 2
A key() 0 4 1
A next() 0 4 1
A valid() 0 10 2
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace ArgentCrusade\Selectel\CloudStorage\Collections;
4
5
use Iterator;
6
use Countable;
7
use ArrayAccess;
8
use JsonSerializable;
9
use ArgentCrusade\Selectel\CloudStorage\Contracts\Collections\CollectionContract;
10
11
class Collection implements CollectionContract, ArrayAccess, Countable, Iterator, JsonSerializable
12
{
13
    /**
14
     * Collection items.
15
     *
16
     * @var array
17
     */
18
    protected $items = [];
19
20
    /**
21
     * Iterator position.
22
     *
23
     * @var int
24
     */
25
    protected $position = 0;
26
27
    /**
28
     * Collection keys.
29
     *
30
     * @var array
31
     */
32
    protected $keys = [];
33
34
    /**
35
     * @param array $items = []
36
     */
37
    public function __construct(array $items = [])
38
    {
39
        $this->items = $items;
40
        $this->position = 0;
41
        $this->keys = array_keys($items);
42
    }
43
44
    /**
45
     * Determines if given key exists in current collection.
46
     *
47
     * @param mixed $key
48
     *
49
     * @return bool
50
     */
51
    public function has($key)
52
    {
53
        return isset($this->items[$key]);
54
    }
55
56
    /**
57
     * Retrieves item by given key from current collection.
58
     *
59
     * @param mixed $key
60
     *
61
     * @return mixed|null
62
     */
63
    public function get($key)
64
    {
65
        return $this->has($key) ? $this->items[$key] : null;
66
    }
67
68
    /**
69
     * Collection size.
70
     *
71
     * @return int
72
     */
73
    public function count()
74
    {
75
        return count($this->items);
76
    }
77
78
    /**
79
     * Determines if given offset exists in current collection.
80
     *
81
     * @param mixed $offset
82
     *
83
     * @return bool
84
     */
85
    public function offsetExists($offset)
86
    {
87
        return $this->has($offset);
88
    }
89
90
    /**
91
     * Puts value to given offset or appends it to current collection.
92
     *
93
     * @param mixed $offset
94
     * @param mixed $value
95
     */
96
    public function offsetSet($offset, $value)
97
    {
98
        if (is_null($offset)) {
99
            $this->items[] = $value;
100
        } else {
101
            $this->items[$offset] = $value;
102
        }
103
104
        $this->keys = array_keys($this->items);
105
    }
106
107
    /**
108
     * Retrieves given offset from current collection.
109
     * Returns NULL if no value found.
110
     *
111
     * @param mixed $offset
112
     *
113
     * @return mixed|null
114
     */
115
    public function offsetGet($offset)
116
    {
117
        return isset($this->items[$offset]) ? $this->items[$offset] : null;
118
    }
119
120
    /**
121
     * Drops given offset from current collection.
122
     *
123
     * @param mixed $offset
124
     */
125
    public function offsetUnset($offset)
126
    {
127
        unset($this->items[$offset]);
128
129
        $this->keys = array_keys($this->items);
130
    }
131
132
    /**
133
     * Rewinds iterator back to first position.
134
     */
135
    public function rewind()
136
    {
137
        $this->position = 0;
138
    }
139
140
    /**
141
     * Current iterator item.
142
     *
143
     * @return mixed|null
144
     */
145
    public function current()
146
    {
147
        $currentKey = $this->keys[$this->position];
148
149
        return isset($this->items[$currentKey]) ? $this->items[$currentKey] : null;
150
    }
151
152
    /**
153
     * Current iterator position.
154
     *
155
     * @return mixed
156
     */
157
    public function key()
158
    {
159
        return $this->keys[$this->position];
160
    }
161
162
    /**
163
     * Increments iterator position.
164
     */
165
    public function next()
166
    {
167
        $this->position++;
168
    }
169
170
    /**
171
     * Determines if there is some value at current iterator position.
172
     *
173
     * @return bool
174
     */
175
    public function valid()
176
    {
177
        if (!isset($this->keys[$this->position])) {
178
            return false;
179
        }
180
181
        $currentKey = $this->keys[$this->position];
182
183
        return isset($this->items[$currentKey]);
184
    }
185
186
    /**
187
     * JSON representation of collection.
188
     *
189
     * @return array
190
     */
191
    public function jsonSerialize()
192
    {
193
        return $this->items;
194
    }
195
}
196