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
Map.php 94.8% 100.0% 95.8%
   
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_Map_Abstract
22
 */
23 1
require_once 'Xyster/Collection/Map/Abstract.php';
24
/**
25
 * Xyster_Collection_Map_Entry
26
 */
27 1
require_once 'Xyster/Collection/Map/Entry.php';
28
/**
29
 * Implementation of a key-based collection
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_Map extends Xyster_Collection_Map_Abstract
37
{
38
	/**
39
	 * The container for the {@link Xyster_Collection_Map_Entry} objects
40
	 *
41
	 * @var array
42
	 */
43
	protected $_items = array();
44
45
	/**
46
	 * Whether this map is immutable
47
	 *
48
	 * @var boolean
49
	 */
50
	private $_immutable = false;
51
52
	/**
53
	 * Creates a new map with object keys
54
	 *
55
	 * @param Xyster_Collection_Map_Interface $map The values to add to this map
56
	 * @param boolean $immutable
57
	 */
58
	public function __construct( Xyster_Collection_Map_Interface $map = null, $immutable = false )
59
	{
60 32
	    if ( $map ) {
61 5
		    $this->merge($map);
62 5
	    }
63 32
		$this->_immutable = $immutable;
64
	}
65
66
	/**
67
	 * Removes all items from the map
68
	 *
69
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
70
	 */
71
	public function clear()
72
	{
73 4
		$this->_failIfImmutable();
74 2
		$this->_items = array();
75
	}
76
77
	/**
78
	 * Gets the number of items in the map
79
	 *
80
	 * @return int The number of items
81
	 */
82
	public function count()
83
	{
84 22
		return count($this->_items);
85
	}
86
87
	/**
88
	 * Gets an iterator for the keys and values in this set
89
	 *
90
	 * @return Iterator
91
	 */
92
	public function getIterator()
93
	{
94 9
		return ( $this->count() ) ?
95 9
			new Xyster_Collection_Iterator($this->_items) : new EmptyIterator();
96
	}
97
98
	/**
99
	 * Gets all keys contained in this map
100
	 *
101
	 * @return Xyster_Collection_Set_Interface The keys in this map
102
	 */
103
	public function keys()
104
	{
105 2
	    $keys = new Xyster_Collection();
106 2
	    foreach( $this->_items as $entry ) {
107 2
	        $keys->add($entry->getKey());
108 2
	    }
109 2
	    return new Xyster_Collection_Set($keys, true);
110
	}
111
112
	/**
113
	 * Gets the first key found for the value supplied
114
	 *
115
	 * This method could return null if the key is null, or if the key was not
116
	 * found.  Use {@link containsKey} to check which is true.
117
	 *
118
	 * @param mixed $value
119
	 * @return mixed The key found, or false if none
120
	 */
121
	public function keyFor( $value )
122
	{
123 2
		foreach( $this->_items as $entry ) {
124 2
			if ( $entry->getValue() === $value ) {
125 2
				return $entry->getKey();
126 0
			}
127 2
		}
128 2
		return false;
129
	}
130
131
	/**
132
	 * Gets all keys for the value supplied
133
	 *
134
	 * @param mixed $value The value for which to search
135
	 * @return Xyster_Collection_Set_Interface
136
	 */
137
	public function keysFor( $value )
138
	{
139 2
		$c = new Xyster_Collection();
140 2
	    foreach( $this->_items as $entry ) {
141 2
			if ( $entry->getValue() === $value ) {
142 2
				$c->add($entry->getKey());
143 2
			}
144 2
		}
145 2
		return new Xyster_Collection_Set($c, true);
146
	}
147
148
	/**
149
	 * Gets whether the specified key exists in the map
150
	 *
151
	 * @param object $key The key to test
152
	 * @return boolean Whether the key is in the map
153
	 */
154
	public function offsetExists( $key )
155
	{
156 29
		if ( !is_object($key) ) {
157 1
			throw new InvalidArgumentException("Only objects can be keys in this map");
158 0
		}
159 29
		return array_key_exists(spl_object_hash($key), $this->_items);
160
	}
161
162
	/**
163
	 * Gets the value at a specified key
164
	 *
165
	 * @param object $key The index to get
166
	 * @return mixed The value found at $key or null if none
167
	 */
168
	public function offsetGet( $key )
169
	{
170 6
		return $this->offsetExists($key) ?
171 6
			$this->_items[spl_object_hash($key)]->getValue() : null;
172
	}
173
174
	/**
175
	 * Sets the value at a given key.
176
	 *
177
	 * @param object $key The key to set
178
	 * @param mixed $value The value to set
179
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
180
	 */
181
	public function offsetSet( $key, $value )
182
	{
183 29
		$this->_failIfImmutable();
184 29
		if ( !$this->offsetExists($key) )  {
185 29
			$index = spl_object_hash($key);
186 29
			$entry = new Xyster_Collection_Map_Entry($key, $value);
187 29
			$this->_items[$index] = $entry;
188 29
		} else {
189 1
			$index = spl_object_hash($key);
190 1
			$this->_items[$index]->setValue($value);
191
		}
192
	}
193
194
	/**
195
	 * Removes a value at the specified key
196
	 *
197
	 * @param object $key The key to "unset"
198
	 * @throws Xyster_Collection_Exception if the collection cannot be modified
199
	 */
200
	public function offsetUnset( $key )
201
	{
202 3
		$this->_failIfImmutable();
203 2
		if ( $this->offsetExists($key) ) {
204 2
			unset($this->_items[spl_object_hash($key)]);
205 2
		}
206
	}
207
208
	/**
209
	 * Puts the items in this map into an array
210
	 *
211
	 * This array contains objects with the key and value available.
212
	 *
213
	 * @return array The items in this map
214
	 */
215
	public function toArray()
216
	{
217 2
		return array_values($this->_items);
218
	}
219
220
	/**
221
	 * Gets the values contained in this map
222
	 *
223
	 * @return Xyster_Collection_Interface
224
	 */
225
	public function values()
226
	{
227 6
		$values = array();
228 6
		foreach( $this->_items as $entry ) {
229 6
		    $values[] = $entry->getValue();
230 6
		}
231 6
		return Xyster_Collection::using($values, true);
232
	}
233
234
	/**
235
	 * A convenience method to fail on modification of immutable collection
236
	 *
237
	 * @throws Xyster_Collection_Exception if the collection is immutable
238
	 */
239
	private function _failIfImmutable()
240
	{
241 30
	    if ( $this->_immutable ) {
242 5
	        require_once 'Xyster/Collection/Exception.php';
243 5
			throw new Xyster_Collection_Exception("This collection cannot be changed");
244 0
		}
245
	}
246
}


Report generated at 2007-10-08T19:32:24-05:00