http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 241 Statements: 58

Source file Statements Methods Total coverage
Abstract.php 91.4% 100.0% 93.1%
   
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_Interface
17
 */
18 1
require_once 'Xyster/Collection/Interface.php';
19
/**
20
 * Xyster_Collection_Iterator
21
 */
22 1
require_once 'Xyster/Collection/Iterator.php';
23
/**
24
 * Abstract class for 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
class Xyster_Collection_Abstract implements Xyster_Collection_Interface
32
{
33
    /**
34
     * The actual collection entries
35
     *
36
     * @var array
37
     */
38
	protected $_items = array();
39
40
	/**
41
	 * Adds an item to the collection
42
	 *
43
	 * @param mixed $item The item to add
44
	 * @return boolean Whether the collection changed as a result of this method
45
	 */
46
	public function add( $item )
47
	{
48 273
		$this->_items[] = $item;
49 273
		return true;
50
	}
51
52
	/**
53
	 * Removes all items from the collection
54
	 */
55
	public function clear()
56
	{
57 25
		$this->_items = array();
58
	}
59
60
	/**
61
	 * Tests to see whether the collection contains the value supplied
62
	 *
63
	 * If the supplied value is an object, the comparison will be done for
64
	 * identity (===) and not for value (==).
65
	 *
66
	 * @param mixed $item The item to test
67
	 * @return boolean Whether the collection contains the supplied value
68
	 */
69
	public function contains( $item )
70
	{
71 233
		return in_array($item, $this->_items, true);
72
	}
73
74
	/**
75
	 * Tests to see whether the collection contains all of the supplied values
76
	 *
77
	 * @param Xyster_Collection_Interface $values The values to test
78
	 * @return boolean Whether the collection contains all of the supplied values
79
	 */
80
	public function containsAll( Xyster_Collection_Interface $values )
81
	{
82 39
		foreach( $values as $v ) {
83 39
			if ( !$this->contains($v) ) {
84 7
				return false;
85 0
			}
86 39
		}
87 39
		return true;
88
	}
89
90
	/**
91
	 * Tests to see whether the collection contains any of the supplied values
92
	 *
93
	 * Basically, implementations can safely return true on the first item that
94
	 * is found.
95
	 *
96
	 * @param Xyster_Collection_Interface $values The values to test
97
	 * @return boolean Whether the collection contains any of the supplied values
98
	 */
99
	public function containsAny( Xyster_Collection_Interface $values )
100
	{
101 26
		foreach( $values as $v ) {
102 26
			if ( $this->contains($v) ) {
103 16
				return true;
104 0
			}
105 19
		}
106 17
		return false;
107
	}
108
109
	/**
110
	 * Gets the number of items in the collection
111
	 *
112
	 * @return int The number of items
113
	 */
114
	public function count()
115
	{
116 281
		return count($this->_items);
117
	}
118
119
	/**
120
	 * Gets an iterator for the values in the collection
121
	 *
122
	 * @return SeekableIterator
123
	 */
124
	public function getIterator()
125
	{
126 225
		return count($this->_items) ?
127 219
			new Xyster_Collection_Iterator(array_values($this->_items)) :
128 225
			new EmptyIterator();
129
	}
130
131
	/**
132
	 * Tests to see if the collection contains no elements
133
	 *
134
	 * The return value from this method should be equivalent to
135
	 * <code>( $collection->count() == 0 )</code>.
136
	 *
137
	 * @return boolean Whether this collection has no elements
138
	 */
139
	public function isEmpty()
140
	{
141 17
		return $this->count() == 0;
142
	}
143
144
	/**
145
	 * Merges the values from the supplied collection into this one
146
	 *
147
	 * @param Xyster_Collection_Interface $values
148
	 * @return boolean Whether the collection changed as a result of this method
149
	 */
150
	public function merge( Xyster_Collection_Interface $values )
151
	{
152 125
		$before = count($this);
153 125
		foreach( $values as $v ) {
154 118
			$this->add($v);
155 118
		}
156 125
		return $this->count() != $before;
157
	}
158
159
	/**
160
	 * Removes the specified value from the collection
161
	 *
162
	 * @param mixed $item The value to remove
163
	 * @return boolean If the value was in the collection
164
	 */
165
	public function remove( $item )
166
	{
167 18
		$before = $this->count();
168 18
		foreach( $this->_items as $key=>$value ) {
169 16
			if ( $value === $item ) {
170 15
				unset($this->_items[$key]);
171 15
			}
172 16
		}
173 18
		return $this->count() != $before;
174
	}
175
176
	/**
177
	 * Removes all of the specified values from the collection
178
	 *
179
	 * @param Xyster_Collection_Interface $values The values to remove
180
	 * @return boolean Whether the collection changed as a result of this method
181
	 */
182
	public function removeAll( Xyster_Collection_Interface $values )
183
	{
184 8
		$before = $this->count();
185 8
		foreach( $this->_items as $key=>$value ) {
186 8
			if ( $values->contains($value) ) {
187 8
				unset($this->_items[$key]);
188 8
			}
189 8
		}
190 8
		return $this->count() != $before;
191
	}
192
193
	/**
194
	 * Removes all values from the collection except for the ones specified
195
	 *
196
	 * If the collection doesn't contain any of the values supplied, it should
197
	 * simply be emptied.
198
	 *
199
	 * @param Xyster_Collection_Interface $values The values to keep
200
	 * @return boolean Whether the collection changed as a result of this method
201
	 */
202
	public function retainAll( Xyster_Collection_Interface $values )
203
	{
204 9
		$before = $this->count();
205 9
		if ( !$values->containsAny($this) ) {
206 7
			$this->clear();
207 7
			return true;
208 0
		}
209 9
		foreach( $this->_items as $key=>$value ) {
210 9
			if ( !$values->contains($value) ) {
211 9
				unset($this->_items[$key]);
212 9
			}
213 9
		}
214 9
		return $this->count() != $before;
215
	}
216
217
	/**
218
	 * Puts the items in this collection into an array
219
	 *
220
	 * @return array The items in this collection
221
	 */
222
	public function toArray()
223
	{
224 17
		return array_values($this->_items);
225
	}
226
227
	/**
228
	 * Converts the collection into a string
229
	 *
230
	 * @magic
231
	 * @return string
232
	 */
233
	public function __toString()
234
	{
235
	    try {
236 4
	        return '[' . implode(',', $this->_items) . ']';
237 0
	    } catch ( Exception $e ) {
238 0
	        return '[???]';
239
	    }
240
	}
241
}


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