http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 12 LOC: 204 Statements: 29

Source file Statements Methods Total coverage
List.php 96.6% 100.0% 97.6%
   
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_List_Abstract
22
 */
23 1
require_once 'Xyster/Collection/List/Abstract.php';
24
/**
25
 * Simple implementation of an index-based collection
26
 *
27
 * @category  Xyster
28
 * @package   Xyster_Collection
29
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
30
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
31
 */
32
class Xyster_Collection_List extends Xyster_Collection_List_Abstract
33
{
34
    private $_immutable;
35
36
	/**
37
	 * Creates a new list
38
	 *
39
	 * @param Xyster_Collection_Interface $values Any values to add to this list
40
	 * @param boolean $immutable Whether the set can be changed
41
	 */
42
	public function __construct( Xyster_Collection_Interface $values = null, $immutable = null )
43
	{
44 33
	    if ( $values ) {
45 13
		    $this->merge($values);
46 13
	    }
47 33
		$this->_immutable = $immutable;
48
	}
49
50
	/**
51
	 * Adds an item to the collection
52
	 *
53
	 * @param mixed $item The item to add
54
	 * @return boolean Whether the collection changed as a result of this method
55
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
56
	 */
57
	public function add( $item )
58
	{
59 30
		$this->_failIfImmutable();
60 30
		return parent::add($item);
61
	}
62
63
	/**
64
	 * Removes all items from the collection
65
	 *
66
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
67
	 */
68
	public function clear()
69
	{
70 4
		$this->_failIfImmutable();
71 2
		parent::clear();
72
	}
73
74
	/**
75
	 * Inserts a value into the list at the specified index
76
	 *
77
	 * {@inherit}
78
	 *
79
	 * @param int $index The index at which to insert
80
	 * @param mixed $value The value to insert
81
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
82
	 * @throws OutOfBoundsException if the index is invalid
83
	 */
84
	public function insert( $index, $value )
85
	{
86 2
		$this->_failIfImmutable();
87 1
		parent::insert($index, $value);
88
	}
89
90
	/**
91
	 * Inserts the supplied values into the list at the specified index
92
	 *
93
	 * {@inherit}
94
	 *
95
	 * @param int $index The index at which to insert
96
	 * @param Xyster_Collection_Interface $values The value to insert
97
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
98
	 * @throws OutOfBoundsException if the index is invalid
99
	 */
100
	public function insertAll( $index, Xyster_Collection_Interface $values )
101
	{
102 2
		$this->_failIfImmutable();
103 1
		parent::insertAll($index, $values);
104
	}
105
106
	/**
107
	 * Sets the value at a given index.
108
	 *
109
	 * The index must be greater than or equal to 0 and less than or equal to
110
	 * the size of this collection.  In other words, an index is valid if
111
	 * <code>( $index < 0 || $index > $this->count() )</code> is false.
112
	 *
113
	 * @param int $index The index to set
114
	 * @param mixed $value The value to set
115
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
116
	 * @throws OutOfBoundsException if the index is invalid
117
	 */
118
	public function offsetSet( $index, $value )
119
	{
120 2
		$this->_failIfImmutable();
121 1
		parent::offsetSet($index, $value);
122
	}
123
124
	/**
125
	 * Removes a value at the specified index
126
	 *
127
	 * {@inherit}
128
	 *
129
	 * @param int $index The index to "unset"
130
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
131
	 * @throws OutOfBoundsException if the index is invalid
132
	 */
133
	public function offsetUnset( $index )
134
	{
135 2
		$this->_failIfImmutable();
136 1
		parent::offsetUnset($index);
137
	}
138
139
	/**
140
	 * Removes the specified value from the collection
141
	 *
142
	 * @param mixed $item The value to remove
143
	 * @return boolean If the value was in the collection
144
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
145
	 */
146
	public function remove( $item )
147
	{
148 2
		$this->_failIfImmutable();
149 1
		return parent::remove($item);
150
	}
151
152
	/**
153
	 * Removes all of the specified values from the collection
154
	 *
155
	 * @param Xyster_Collection_Interface $values The values to remove
156
	 * @return boolean Whether the collection changed as a result of this method
157
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
158
	 */
159
	public function removeAll( Xyster_Collection_Interface $values )
160
	{
161 2
		$this->_failIfImmutable();
162 1
		return parent::removeAll($values);
163
	}
164
165
	/**
166
	 * Removes all values from the collection except for the ones specified
167
	 *
168
	 * @param Xyster_Collection_Interface $values The values to keep
169
	 * @return boolean Whether the collection changed as a result of this method
170
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
171
	 */
172
	public function retainAll( Xyster_Collection_Interface $values )
173
	{
174 2
		$this->_failIfImmutable();
175 1
		return parent::retainAll($values);
176
	}
177
178
	/**
179
	 * Removes $count elements starting at $from
180
	 *
181
	 * @param int $from The starting index
182
	 * @param int $count The number of elements to remove
183
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
184
	 * @throws OutOfBoundsException if $from is invalid
185
	 */
186
	public function slice( $from, $count )
187
	{
188 2
		$this->_failIfImmutable();
189 1
		parent::slice($from, $count);
190
	}
191
192
	/**
193
	 * A convenience method to fail on modification of immutable collection
194
	 *
195
	 * @throws Xyster_Collection_Exception if the collection is immutable
196
	 */
197
	private function _failIfImmutable()
198
	{
199 32
	    if ( $this->_immutable ) {
200 12
	        require_once 'Xyster/Collection/Exception.php';
201 12
			throw new Xyster_Collection_Exception("This collection cannot be changed");
202 0
		}
203
	}
204
}


Report generated at 2007-11-05T09:09:01-05:00