http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 13 LOC: 201 Statements: 34

Source file Statements Methods Total coverage
Collection.php 97.1% 100.0% 97.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
 * @version   $Id$
15
 */
16
/**
17
 * @see Xyster_Collection_Abstract
18
 */
19 1
require_once 'Xyster/Collection/Abstract.php';
20
/**
21
 * Implementation of Xyster_Collection_Abstract with static helper methods
22
 *
23
 * @category  Xyster
24
 * @package   Xyster_Collection
25
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
26
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
27
 */
28
class Xyster_Collection extends Xyster_Collection_Abstract
29
{
30
    private $_immutable = false;
31
32
    /**
33
     * @var Xyster_Container_List_Empty
34
     */
35
    static private $_emptyList = null;
36
37
	/**
38
	 * Creates a new simple collection
39
	 *
40
	 * @param Xyster_Collection_Interface $collection
41
	 * @param boolean $immutable
42
	 */
43
	public function __construct( Xyster_Collection_Interface $collection = null, $immutable = false )
44
    {
45 444
        if ( $collection ) {
46 35
            $this->merge($collection);
47 35
        }
48 444
        $this->_immutable = $immutable;
49
	}
50
51
	/**
52
	 * Adds an item to the collection
53
	 *
54
	 * @param mixed $item The item to add
55
	 * @return boolean Whether the collection changed as a result of this method
56
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
57
	 */
58
	public function add( $item )
59
	{
60 157
		$this->_failIfImmutable();
61 157
	    return parent::add($item);
62
	}
63
64
	/**
65
	 * Removes all items from the collection
66
	 *
67
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
68
	 */
69
	public function clear()
70
	{
71 20
		$this->_failIfImmutable();
72 16
	    parent::clear();
73
	}
74
75
	/**
76
	 * Removes the specified value from the collection
77
	 *
78
	 * @param mixed $item The value to remove
79
	 * @return boolean If the value was in the collection
80
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
81
	 */
82
	public function remove( $item )
83
	{
84 10
		$this->_failIfImmutable();
85 8
	    return parent::remove($item);
86
	}
87
88
	/**
89
	 * Removes all of the specified values from the collection
90
	 *
91
	 * @param Xyster_Collection_Interface $values The values to remove
92
	 * @return boolean Whether the collection changed as a result of this method
93
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
94
	 */
95
	public function removeAll( Xyster_Collection_Interface $values )
96
	{
97 5
		$this->_failIfImmutable();
98 3
		return parent::removeAll($values);
99
	}
100
101
	/**
102
	 * Removes all values from the collection except for the ones specified
103
	 *
104
	 * @param Xyster_Collection_Interface $values The values to keep
105
	 * @return boolean Whether the collection changed as a result of this method
106
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
107
	 */
108
	public function retainAll( Xyster_Collection_Interface $values )
109
	{
110 5
		$this->_failIfImmutable();
111 3
		return parent::retainAll($values);
112
	}
113
114
	/**
115
	 * Gets an immutable, empty list
116
	 *
117
	 * @return Xyster_Container_List_Interface
118
	 */
119
	static public function emptyList()
120
	{
121 1
		if ( self::$_emptyList === null ) {
122 1
			require_once 'Xyster/Collection/List/Empty.php';
123 1
			self::$_emptyList = new Xyster_Collection_List_Empty;
124 1
		}
125 1
		return self::$_emptyList;
126
	}
127
128
	/**
129
	 * Returns a new unchangable collection containing all the supplied values
130
	 *
131
	 * @param Xyster_Collection_Interface $collection
132
	 * @return Xyster_Collection_Interface
133
	 */
134
	static public function fixedCollection( Xyster_Collection_Interface $collection )
135
	{
136 7
		return new Xyster_Collection($collection, true);
137
	}
138
139
	/**
140
	 * Returns a new unchangable list containing all the supplied values
141
	 *
142
	 * @param Xyster_Collection_List_Interface $list
143
	 * @return Xyster_Collection_List_Interface
144
	 */
145
	static public function fixedList( Xyster_Collection_List_Interface $list )
146
	{
147 1
	    require_once 'Xyster/Collection/List.php';
148 1
		return new Xyster_Collection_List($list, true);
149
	}
150
151
	/**
152
	 * Returns a new unchangable map containing all the supplied key/value pairs
153
	 *
154
	 * @param Xyster_Collection_Map_Interface $map
155
	 * @return Xyster_Collection_Map_Interface
156
	 */
157
	static public function fixedMap( Xyster_Collection_Map_Interface $map )
158
	{
159 5
	    require_once 'Xyster/Collection/Map.php';
160 5
		return new Xyster_Collection_Map($map, true);
161
	}
162
163
	/**
164
	 * Returns a new unchangable set containing all the supplied values
165
	 *
166
	 * @param Xyster_Collection_Set_Interface $set
167
	 * @return Xyster_Collection_Set_Interface
168
	 */
169
	static public function fixedSet( Xyster_Collection_Set_Interface $set )
170
	{
171 5
	    require_once 'Xyster/Collection/Set.php';
172 5
		return new Xyster_Collection_Set($set, true);
173
	}
174
175
    /**
176
     * Creates a new collection containing the values
177
     *
178
     * @param array $values
179
     * @param boolean $immutable
180
     * @return Xyster_Collection_Interface
181
     */
182
    static public function using( array $values, $immutable = false )
183
    {
184 105
        $collection = new Xyster_Collection(null, $immutable);
185 105
        $collection->_items = array_values($values);
186 105
        return $collection;
187
    }
188
189
	/**
190
	 * A convenience method to fail on modification of immutable collection
191
	 *
192
	 * @throws Xyster_Collection_Exception if the collection is immutable
193
	 */
194
	private function _failIfImmutable()
195
	{
196 160
	    if ( $this->_immutable ) {
197 14
	        require_once 'Xyster/Collection/Exception.php';
198 14
			throw new Xyster_Collection_Exception("This collection cannot be changed");
199 0
		}
200
	}
201
}


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