http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 12 LOC: 182 Statements: 29

Source file Statements Methods Total coverage
Collection.php 96.6% 100.0% 97.6%
   
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
	 * Creates a new simple collection
34
	 *
35
	 * @param Xyster_Collection_Interface $collection
36
	 * @param boolean $immutable
37
	 */
38
	public function __construct( Xyster_Collection_Interface $collection = null, $immutable = false )
39
    {
40 381
        if ( $collection ) {
41 35
            $this->merge($collection);
42 35
        }
43 381
        $this->_immutable = $immutable;
44
	}
45
46
	/**
47
	 * Adds an item to the collection
48
	 *
49
	 * @param mixed $item The item to add
50
	 * @return boolean Whether the collection changed as a result of this method
51
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
52
	 */
53
	public function add( $item )
54
	{
55 145
		$this->_failIfImmutable();
56 145
	    return parent::add($item);
57
	}
58
59
	/**
60
	 * Removes all items from the collection
61
	 *
62
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
63
	 */
64
	public function clear()
65
	{
66 17
		$this->_failIfImmutable();
67 13
	    parent::clear();
68
	}
69
70
	/**
71
	 * Removes the specified value from the collection
72
	 *
73
	 * @param mixed $item The value to remove
74
	 * @return boolean If the value was in the collection
75
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
76
	 */
77
	public function remove( $item )
78
	{
79 10
		$this->_failIfImmutable();
80 8
	    return parent::remove($item);
81
	}
82
83
	/**
84
	 * Removes all of the specified values from the collection
85
	 *
86
	 * @param Xyster_Collection_Interface $values The values to remove
87
	 * @return boolean Whether the collection changed as a result of this method
88
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
89
	 */
90
	public function removeAll( Xyster_Collection_Interface $values )
91
	{
92 5
		$this->_failIfImmutable();
93 3
		return parent::removeAll($values);
94
	}
95
96
	/**
97
	 * Removes all values from the collection except for the ones specified
98
	 *
99
	 * @param Xyster_Collection_Interface $values The values to keep
100
	 * @return boolean Whether the collection changed as a result of this method
101
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
102
	 */
103
	public function retainAll( Xyster_Collection_Interface $values )
104
	{
105 5
		$this->_failIfImmutable();
106 3
		return parent::retainAll($values);
107
	}
108
109
	/**
110
	 * Creates a new collection containing the values
111
	 *
112
	 * @param array $values
113
	 * @param boolean $immutable
114
	 * @return Xyster_Collection_Interface
115
	 */
116
	static public function using( array $values, $immutable = false )
117
	{
118 65
		$collection = new Xyster_Collection(null,$immutable);
119 65
		$collection->_items = array_values($values);
120 65
		return $collection;
121
	}
122
123
	/**
124
	 * Returns a new unchangable collection containing all the supplied values
125
	 *
126
	 * @param Xyster_Collection_Interface $collection
127
	 * @return Xyster_Collection_Interface
128
	 */
129
	static public function fixedCollection( Xyster_Collection_Interface $collection )
130
	{
131 7
		return new Xyster_Collection( $collection, true );
132
	}
133
134
	/**
135
	 * Returns a new unchangable list containing all the supplied values
136
	 *
137
	 * @param Xyster_Collection_List_Interface $list
138
	 * @return Xyster_Collection_List_Interface
139
	 */
140
	static public function fixedList( Xyster_Collection_List_Interface $list )
141
	{
142 1
	    require_once 'Xyster/Collection/List.php';
143 1
		return new Xyster_Collection_List( $list, true );
144
	}
145
146
	/**
147
	 * Returns a new unchangable map containing all the supplied key/value pairs
148
	 *
149
	 * @param Xyster_Collection_Map_Interface $map
150
	 * @return Xyster_Collection_Map_Interface
151
	 */
152
	static public function fixedMap( Xyster_Collection_Map_Interface $map )
153
	{
154 5
	    require_once 'Xyster/Collection/Map.php';
155 5
		return new Xyster_Collection_Map( $map, true );
156
	}
157
158
	/**
159
	 * Returns a new unchangable set containing all the supplied values
160
	 *
161
	 * @param Xyster_Collection_Set_Interface $set
162
	 * @return Xyster_Collection_Set_Interface
163
	 */
164
	static public function fixedSet( Xyster_Collection_Set_Interface $set )
165
	{
166 5
	    require_once 'Xyster/Collection/Set.php';
167 5
		return new Xyster_Collection_Set( $set, true );
168
	}
169
170
	/**
171
	 * A convenience method to fail on modification of immutable collection
172
	 *
173
	 * @throws Xyster_Collection_Exception if the collection is immutable
174
	 */
175
	private function _failIfImmutable()
176
	{
177 148
	    if ( $this->_immutable ) {
178 14
	        require_once 'Xyster/Collection/Exception.php';
179 14
			throw new Xyster_Collection_Exception("This collection cannot be changed");
180 0
		}
181
	}
182
}


Report generated at 2008-01-20T12:13:39-05:00