http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


Report generated at 2010-10-18T17:19:49-04:00