http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 246 Statements: 58

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


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