http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


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