Completed
Push — master ( 8196f2...c0d282 )
by Emily
02:08
created

Collection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
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
    public function offsetExists($offset)
79
    {
80
        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
    public function offsetGet($offset)
90
    {
91
        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
    public function offsetSet($offset, $value)
104
    {
105
        $this->data[$offset] = $value;
106
    }
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 mixed $item The element to add
156
     */
157
    public function add($item)
158
    {
159
        $this->data[] = $item;
160
    }
161
162
    /**
163
     * Returns how many elements are in the Collection
164
     *
165
     * @return int The number of elements in the Collection
166
     */
167 1
    public function size()
168
    {
169 1
        return count($this->data);
170
    }
171
172
    /**
173
     * Clears the Collection of all data
174
     */
175
    public function clear()
176
    {
177
        $this->data = array( );
178
    }
179
180
    /**
181
     * Magic Method which returns the Collection's data when it is
182
     * passed to var_dump or similar
183
     *
184
     * This prevents the Collection from returning the large amount of
185
     * extra stuff that is contained within Collection, such as the
186
     * internal pointer and inherited interanal Model elements.
187
     *
188
     * @return array The data
189
     */
190
    public function __debugInfo()
191
    {
192
        return $this->data;
193
    }
194
}
195