http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 209 Statements: 33

Source file Statements Methods Total coverage
String.php 93.9% 100.0% 95.7%
   
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
 * @see Xyster_Collection_Map_Abstract
22
 */
23 1
require_once 'Xyster/Collection/Map/Abstract.php';
24
/**
25
 * A simple string key-based map
26
 *
27
 * @category  Xyster
28
 * @package   Xyster_Collection
29
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
30
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
31
 */
32
class Xyster_Collection_Map_String extends Xyster_Collection_Map_Abstract
33
{
34
	/**
35
	 * The map array
36
	 *
37
	 * @var array
38
	 */
39
	protected $_items = array();
40
41
	/**
42
	 * Creates a new map with object keys
43
	 *
44
	 * @param Xyster_Collection_Map_Interface $map The values to add to this map
45
	 */
46
	public function __construct( Xyster_Collection_Map_Interface $map = null )
47
	{
48 103
	    if ( $map ) {
49 1
		    $this->merge($map);
50 1
	    }
51
	}
52
53
	/**
54
	 * Removes all items from the map
55
	 *
56
	 */
57
	public function clear()
58
	{
59 1
		$this->_items = array();
60
	}
61
62
	/**
63
	 * Tests to see whether the map contains the value supplied
64
	 *
65
	 * If the supplied value is an object, the comparison will be done for
66
	 * identity (===) and not for value (==).
67
	 *
68
	 * @param mixed $item The item to test
69
	 * @return boolean Whether the map contains the supplied value
70
	 */
71
	public function containsValue( $item )
72
	{
73 11
		return in_array($item, $this->_items, true);
74
	}
75
76
	/**
77
	 * Gets the number of items in the map
78
	 *
79
	 * @return int The number of items
80
	 */
81
	public function count()
82
	{
83 10
		return count($this->_items);
84
	}
85
86
	/**
87
	 * Gets an iterator for the keys and values in this set
88
	 *
89
	 * @return Iterator
90
	 */
91
	public function getIterator()
92
	{
93 3
		return ( $this->count() ) ?
94 3
			new Xyster_Collection_Iterator($this->_items) : new EmptyIterator();
95
	}
96
97
	/**
98
	 * Gets all keys contained in this map
99
	 *
100
	 * @return Xyster_Collection_Set_Interface The keys in this map
101
	 */
102
	public function keys()
103
	{
104 1
	    return new Xyster_Collection_Set(
105 1
	        Xyster_Collection::using(array_keys($this->_items)),
106 1
	        true );
107
	}
108
109
	/**
110
	 * Gets the first key found for the value supplied
111
	 *
112
	 * This method could return null if the key is null, or if the key was not
113
	 * found.  Use {@link containsKey} to check which is true.
114
	 *
115
	 * @param mixed $value
116
	 * @return mixed The key found, or false if none
117
	 */
118
	public function keyFor( $value )
119
	{
120 1
		return array_search($value, $this->_items, true);
121
	}
122
123
	/**
124
	 * Gets all keys for the value supplied
125
	 *
126
	 * @param mixed $value The value for which to search
127
	 * @return Xyster_Collection_Set_Interface
128
	 */
129
	public function keysFor( $value )
130
	{
131 8
		return new Xyster_Collection_Set(
132 8
		    Xyster_Collection::using(array_keys($this->_items,$value,true)),
133 8
		    true );
134
	}
135
136
	/**
137
	 * Gets whether the specified key exists in the map
138
	 *
139
	 * @param object $key The key to test
140
	 * @return boolean Whether the key is in the map
141
	 */
142
	public function offsetExists( $key )
143
	{
144 77
		if ( !is_scalar($key) ) {
145 1
			throw new InvalidArgumentException("Only strings can be keys in this map");
146 0
		}
147 77
		return array_key_exists( $key, $this->_items );
148
	}
149
150
	/**
151
	 * Gets the value at a specified key
152
	 *
153
	 * @param object $key The index to get
154
	 * @return mixed The value found at $key or null if none
155
	 */
156
	public function offsetGet( $key )
157
	{
158 73
		return $this->offsetExists($key) ?
159 48
			$this->_items[$key] :
160 73
			null;
161
	}
162
163
	/**
164
	 * Sets the value at a given key.
165
	 *
166
	 * @param object $key The key to set
167
	 * @param mixed $value The value to set
168
	 * @throws InvalidArgumentException if the collection cannot contain the value
169
	 */
170
	public function offsetSet( $key, $value )
171
	{
172 72
		if ( !is_scalar($key) ) {
173 1
			throw new InvalidArgumentException("Only strings can be keys in this map");
174 0
		}
175 72
		$this->_items[$key] = $value;
176
	}
177
178
	/**
179
	 * Removes a value at the specified key
180
	 *
181
	 * @param object $key The key to "unset"
182
	 */
183
	public function offsetUnset( $key )
184
	{
185 9
		if ( $this->offsetExists($key) ) {
186 8
			unset($this->_items[$key]);
187 8
		}
188
	}
189
190
	/**
191
	 * Puts the items in this collection into an array
192
	 *
193
	 * @return array The items in this collection
194
	 */
195
	public function toArray()
196
	{
197 1
		return array()+$this->_items;
198
	}
199
200
	/**
201
	 * Gets the values contained in this map
202
	 *
203
	 * @return Xyster_Collection_Interface
204
	 */
205
	public function values()
206
	{
207 6
		return Xyster_Collection::using($this->_items, true);
208
	}
209
}


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