http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 262 Statements: 51
Legend: executednot executeddead code
Source file Statements Methods Total coverage
AbstractList.php 100.0% 100.0% 100.0%
 
1
<?php
2
3
/**
4
 * Xyster Framework
5
 *
6
 * This source file is subject to the new BSD license that is bundled
7
 * with this package in the file LICENSE.txt.
8
 * It is also available through the world-wide-web at this URL:
9
 * http://www.opensource.org/licenses/bsd-license.php
10
 *
11
 * @category  Xyster
12
 * @package   Xyster_Collection
13
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
14
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
15
 */
16
namespace Xyster\Collection;
17
/**
18
 * Abstract class for index-based collections
19
 *
20
 * @category  Xyster
21
 * @package   Xyster_Collection
22
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
23
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
24
 */
25
abstract class AbstractList extends AbstractCollection implements IList
26
{
27
    /**
28
     * Gets the value at a specified index
29
     *
30
     * This method is an alias to ArrayAccess::offsetGet
31
     *
32
     * The index must be greater than or equal to 0 and less than or equal to
33
     * the size of this collection.  In other words, an index is valid if
34
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
35
     *
36
     * @param int $index The index to get
37
     * @return mixed The value found at $index
38
     * @throws OutOfBoundsException if the index is invalid
39
     */
40
    public function get($index)
41
    {
42 3
        return $this->offsetGet($index);
43
    }
44
45
    /**
46
     * Returns the first index found for the specified value
47
     *
48
     * @param mixed $value
49
     * @return int The first index found, or false if the value isn't contained
50
     */
51
    public function indexOf($value)
52
    {
53 2
        return array_search($value, $this->_items, true);
54
    }
55
56
    /**
57
     * Inserts a value into the list at the specified index
58
     *
59
     * The index must be greater than or equal to 0 and less than or equal to
60
     * the size of this collection.  In other words, an index is valid if
61
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
62
     *
63
     * @param int $index The index at which to insert
64
     * @param mixed $value The value to insert
65
     * @throws OutOfBoundsException if the index is invalid
66
     */
67
    public function insert($index, $value)
68
    {
69 1
        if ($index < 0 || $index > $this->count()) {
70 1
            throw new \OutOfBoundsException("Invalid index given");
71
        }
72 1
        array_splice($this->_items, $index, 0, $value);
73
    }
74
75
    /**
76
     * Inserts the supplied values into the list at the specified index
77
     *
78
     * The index must be greater than or equal to 0 and less than or equal to
79
     * the size of this collection.  In other words, an index is valid if
80
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
81
     *
82
     * @param int $index The index at which to insert
83
     * @param ICollection $values The value to insert
84
     * @throws OutOfBoundsException if the index is invalid
85
     */
86
    public function insertAll($index, ICollection $values)
87
    {
88 1
        if ($index < 0 || $index > $this->count()) {
89 1
            throw new \OutOfBoundsException("Invalid index given");
90
        }
91 1
        array_splice($this->_items, $index, 0, $values->toArray());
92
    }
93
94
    /**
95
     * Gets whether the specified index exists in the list
96
     *
97
     * @param int $index The index to test
98
     * @return boolean Whether the index is in the list
99
     */
100
    public function offsetExists($index)
101
    {
102 7
        return $index > -1 && $index < $this->count();
103
    }
104
105
    /**
106
     * Gets the value at a specified index
107
     *
108
     * The index must be greater than or equal to 0 and less than
109
     * the size of this collection.  In other words, an index is valid if
110
     * <code>( $index < 0 || $index >= $this->count() )</code> is false.
111
     *
112
     * @param int $index The index to get
113
     * @return mixed The value found at $index
114
     * @throws OutOfBoundsException if the index is invalid
115
     */
116
    public function offsetGet($index)
117
    {
118 5
        if (!$this->offsetExists($index)) {
119 1
            throw new \OutOfBoundsException("Invalid index given");
120
        }
121 5
        return $this->_items[$index];
122
    }
123
124
    /**
125
     * Sets the value at a given index.
126
     *
127
     * The index must be greater than or equal to 0 and less than or equal to
128
     * the size of this collection.  In other words, an index is valid if
129
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
130
     *
131
     * @param int $index The index to set
132
     * @param mixed $value The value to set
133
     * @throws OutOfBoundsException if the index is invalid
134
     */
135
    public function offsetSet($index, $value)
136
    {
137 1
        if ($index < 0 || $index > $this->count()) {
138 1
            throw new \OutOfBoundsException("Invalid index given");
139
        }
140 1
        $this->_items[$index] = $value;
141
    }
142
143
    /**
144
     * Removes a value at the specified index
145
     *
146
     * The index must be greater than or equal to 0 and less than
147
     * the size of this collection.  In other words, an index is valid if
148
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
149
     *
150
     * @param int $index The index to "unset"
151
     * @throws OutOfBoundsException if the index is invalid
152
     */
153
    public function offsetUnset($index)
154
    {
155 1
        if ($index < 0 || $index >= $this->count()) {
156 1
            throw new \OutOfBoundsException("Invalid index given");
157
        }
158 1
        unset($this->_items[$index]);
159 1
        $this->_items = array_values($this->_items);
160
    }
161
162
    /**
163
     * Removes the specified value from the collection
164
     *
165
     * @param mixed $item The value to remove
166
     * @return boolean If the value was in the collection
167
     */
168
    public function remove($item)
169
    {
170 1
        $removed = parent::remove($item);
171 1
        if ($removed) {
172 1
            $this->_items = array_values($this->_items);
173 1
        }
174 1
        return $removed;
175
    }
176
177
    /**
178
     * Removes all of the specified values from the collection
179
     *
180
     * @param ICollection $values The values to remove
181
     * @return boolean Whether the collection changed as a result of this method
182
     */
183
    public function removeAll(ICollection $values)
184
    {
185 1
        $removed = parent::removeAll($values);
186 1
        if ($removed) {
187 1
            $this->_items = array_values($this->_items);
188 1
        }
189 1
        return $removed;
190
    }
191
192
    /**
193
     * Removes a value at the specified index
194
     *
195
     * This method is an alias to ArrayAccess::offsetUnset
196
     *
197
     * The index must be greater than or equal to 0 and less than
198
     * the size of this collection.  In other words, an index is valid if
199
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
200
     *
201
     * @param int $index The index to "unset"
202
     * @throws OutOfBoundsException if the index is invalid
203
     */
204
    public function removeAt($index)
205
    {
206 1
        return $this->offsetUnset($index);
207
    }
208
209
    /**
210
     * Removes all values from the collection except for the ones specified
211
     *
212
     * {@inherit}
213
     *
214
     * @param ICollection $values The values to keep
215
     * @return boolean Whether the collection changed as a result of this method
216
     */
217
    public function retainAll(ICollection $values)
218
    {
219 1
        $removed = parent::retainAll($values);
220 1
        if ($removed && $this->count()) {
221 1
            $this->_items = array_values($this->_items);
222 1
        }
223 1
        return $removed;
224
    }
225
226
    /**
227
     * Sets the value at a given index.
228
     *
229
     * This method is an alias to ArrayAccess::offsetSet.
230
     *
231
     * The index must be greater than or equal to 0 and less than
232
     * the size of this collection.  In other words, an index is valid if
233
     * <code>( $index < 0 || $index > $this->count() )</code> is false.
234
     *
235
     * @param int $index The index to set
236
     * @param mixed $value The value to set
237
     * @throws OutOfBoundsException if the index is invalid
238
     */
239
    public function set($index, $value)
240
    {
241 1
        $this->offsetSet($index, $value);
242
    }
243
244
    /**
245
     * Removes $count elements starting at $from
246
     *
247
     * @param int $from The starting index
248
     * @param int $count The number of elements to remove
249
     * @throws OutOfBoundsException if $from is invalid
250
     */
251
    public function slice($from, $count)
252
    {
253 1
        if ($from < 0 || $from >= $this->count()) {
254 1
            throw new \OutOfBoundsException("Invalid index given");
255
        }
256 1
        for ($i = $from; $i < $count + $from; $i++) {
257 1
            if (isset($this->_items[$i]))
258 1
                unset($this->_items[$i]);
259 1
        }
260 1
        $this->_items = array_values($this->_items);
261
    }
262 1
}


Report generated at 2010-10-18T17:19:49-04:00