http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 209 Statements: 37

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