http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 268 Statements: 52

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


Report generated at 2008-03-05T18:27:43-05:00