http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 273 Statements: 52

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


Report generated at 2007-10-08T19:32:24-05:00