Completed
Push — master ( 7f2568...52038d )
by Emily
02:33
created

Collection::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 <[email protected]>
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 8
    public function current()
62
    {
63 8
        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 6
    public function next()
80
    {
81 6
        $this->pointer++;
82
83 6
        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
    public function offsetExists($offset)
93
    {
94
        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 9
    public function offsetGet($offset)
104
    {
105 9
        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 21
    public function offsetSet($offset, $value)
118
    {
119 21
        $this->data[$offset] = $value;
120 21
    }
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 8
    public function rewind()
136
    {
137 8
        $this->pointer = 0;
138 8
    }
139
140
    /**
141
     * Changes the position of the array pointer
142
     *
143
     * @param scalar $pos The position to seek to
144
     * @return mixed The value at that new point
145
     */
146
    public function seek($pos)
147
    {
148
        $this->pointer = $pos;
149
150
        return $this->offsetGet($pos);
151
    }
152
153
    /**
154
     * Checks if there are any more elements to be read from the
155
     * Collection
156
     *
157
     * @return boolean True if there are one or more elements
158
     */
159 8
    public function valid()
160
    {
161 8
        return $this->pointer < $this->size();
162
    }
163
164
    /**
165
     * Adds an element to the Collection, without specifying a key
166
     *
167
     * @param scalar $offset The key to set
168
     * @param mixed $item The element to add
169
     */
170
    public function add($offset, $item)
171
    {
172
        $this->offsetSet($offset, $item);
173
    }
174
175
    /**
176
     * Push an element onto the end of the array
177
     *
178
     * @param mixed $item The element to add
179
     */
180 21
    public function push($item)
181
    {
182 21
        $this->offsetSet($this->size(), $item);
183 21
    }
184
185
    /**
186
     * Returns how many elements are in the Collection
187
     *
188
     * @return int The number of elements in the Collection
189
     */
190 24
    public function size()
191
    {
192 24
        return count($this->data);
193
    }
194
195
    /**
196
     * Returns how many elements are in the Collection
197
     *
198
     * @return int The number of elements in the Collection
199
     */
200 1
    public function count()
201
    {
202 1
        return $this->size();
203
    }
204
205
    /**
206
     * Checks if the Collection is empty
207
     *
208
     * @return boolean True if the element is empty
209
     */
210 2
    public function empty()
211
    {
212 2
        return $this->size() === 0;
213
    }
214
215
    /**
216
     * Clears the Collection of all data
217
     */
218
    public function clear()
219
    {
220
        $this->data = array( );
221
    }
222
223
    /**
224
     * Magic Method which returns the Collection's data when it is
225
     * passed to var_dump or similar
226
     *
227
     * This prevents the Collection from returning the large amount of
228
     * extra stuff that is contained within Collection, such as the
229
     * internal pointer and inherited interanal Model elements.
230
     *
231
     * @return array The data
232
     */
233
    public function __debugInfo()
234
    {
235
        return $this->data;
236
    }
237
}
238