Completed
Push — master ( 01dc8d...fb21bc )
by Emily
02:05
created

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