Completed
Push — master ( 1f44ff...9311e4 )
by Emily
02:16
created

Collection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <emily@emilyshepherd>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection;
16
/**
17
 * Spaark Framework
18
 *
19
 * @author Emily Shepherd <[email protected]>
20
 * @copyright 2012-2015 Emily Shepherd
21
 */
22
23
use \Spaark\CompositeUtils\Model\Base\Model;
24
25
/**
26
 * Represents an abstract collection of items
27
 *
28
 * This can be interacted with in the same way a PHP array:
29
 * <pre><code>
30
 * $a        = new Collection();
31
 * $a['key'] = 'value';
32
 * </code></pre>
33
 *
34
 * This class, on its own, does not add any new functionality over
35
 * PHP arrays. It is useful for two purposes: firstly as a means of
36
 * boxing a PHP array to ensure it is an object for type-hinting.
37
 * Secondly, other forms of Collection (such as Sets and HashMaps) may
38
 * extend this class and add their own functionality.
39
 */
40
class Collection implements \ArrayAccess, \Iterator, \Countable
41
{
42
    /**
43
     * The raw data of this Collection
44
     *
45
     * @var array
46
     */
47
    protected $data = array( );
48
49
    /**
50
     * The current position of the array pointer
51
     *
52
     * @var int
53
     */
54
    protected $pointer = 0;
55
56
    /**
57
     * Returns the current element
58
     *
59
     * @return mixed The current element
60
     */
61 7
    public function current()
62
    {
63 7
        return $this->offsetGet($this->pointer);
64
    }
65
66
    /**
67
     * Returns the current key
68
     *
69
     * @return int The current key
70
     */
71
    public function key()
72
    {
73
        return $this->pointer;
74
    }
75
76
    /**
77
     * Advances the internal pointer by one
78
     */
79 5
    public function next()
80
    {
81 5
        $this->pointer++;
82
83 5
        return $this->valid() ? $this->offsetGet($this->pointer) : null;
84
    }
85
86
    /**
87
     * Checks if the given offset is set
88
     *
89
     * @param scalar $offset The offset to check
90
     * @return boolean True if the offset is set
91
     */
92 29
    public function offsetExists($offset)
93
    {
94 29
        return isset($this->data[$offset]);
95
    }
96
97
    /**
98
     * Returns the data at the given offset
99
     *
100
     * @param scalar $offset The offset to get
101
     * @return mixed The value at the given offset
102
     */
103 35
    public function offsetGet($offset)
104
    {
105 35
        return $this->data[$offset];
106
    }
107
108
    /**
109
     * Adds a new key-value pair to the Collection
110
     *
111
     * If the key already exists in the collection, its key-value is
112
     * overwritten.
113
     *
114
     * @param scalar $offset The key to set
115
     * @param mixed $value The value to set
116
     */
117 20
    public function offsetSet($offset, $value)
118
    {
119 20
        $this->data[$offset] = $value;
120 20
    }
121
122
    /**
123
     * Removes the given key-value pair from the Collection
124
     *
125
     * @param scalar $offset The key to unset
126
     */
127
    public function offsetUnset($offset)
128
    {
129
        unset($this->data[$offset]);
130
    }
131
132
    /**
133
     * Resets the internal array pointer
134
     */
135 7
    public function rewind()
136
    {
137 7
        $this->pointer = 0;
138
139 7
        return $this->offsetGet(0);
140
    }
141
142
    /**
143
     * Changes the position of the array pointer
144
     *
145
     * @param scalar $pos The position to seek to
146
     * @return mixed The value at that new point
147
     */
148
    public function seek($pos)
149
    {
150
        $this->pointer = $pos;
151
152
        return $this->offsetGet($pos);
153
    }
154
155
    /**
156
     * Checks if there are any more elements to be read from the
157
     * Collection
158
     *
159
     * @return boolean True if there are one or more elements
160
     */
161 7
    public function valid()
162
    {
163 7
        return $this->pointer < $this->size();
164
    }
165
166
    /**
167
     * Adds an element to the Collection, without specifying a key
168
     *
169
     * @param scalar $offset The key to set
170
     * @param mixed $item The element to add
171
     */
172 19
    public function add($offset, $item)
173
    {
174 19
        $this->offsetSet($offset, $item);
175 19
    }
176
177
    /**
178
     * Push an element onto the end of the array
179
     *
180
     * @param mixed $item The element to add
181
     */
182 21
    public function push($item)
183
    {
184 21
        $this->offsetSet($this->size(), $item);
185 21
    }
186
187
    /**
188
     * Returns how many elements are in the Collection
189
     *
190
     * @return int The number of elements in the Collection
191
     */
192 27
    public function size()
193
    {
194 27
        return count($this->data);
195
    }
196
197
    /**
198
     * Returns how many elements are in the Collection
199
     *
200
     * @return int The number of elements in the Collection
201
     */
202 4
    public function count()
203
    {
204 4
        return $this->size();
205
    }
206
207
    /**
208
     * Checks if the Collection is empty
209
     *
210
     * @return boolean True if the element is empty
211
     */
212 2
    public function empty()
213
    {
214 2
        return $this->size() === 0;
215
    }
216
217
    /**
218
     * Clears the Collection of all data
219
     */
220
    public function clear()
221
    {
222
        $this->data = array( );
223
    }
224
225
    /**
226
     * Magic Method which returns the Collection's data when it is
227
     * passed to var_dump or similar
228
     *
229
     * This prevents the Collection from returning the large amount of
230
     * extra stuff that is contained within Collection, such as the
231
     * internal pointer and inherited interanal Model elements.
232
     *
233
     * @return array The data
234
     */
235
    public function __debugInfo()
236
    {
237
        return $this->data;
238
    }
239
}
240