| 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 |
|
* @see Xyster_Collection_Map_Entry |
| 21 |
|
*/ |
| 22 |
1 |
require_once 'Xyster/Collection/Map/Entry.php'; |
| 23 |
|
/** |
| 24 |
|
* @see Xyster_Type |
| 25 |
|
*/ |
| 26 |
1 |
require_once 'Xyster/Type.php'; |
| 27 |
|
/** |
| 28 |
|
* Implementation of a key-based collection |
| 29 |
|
* |
| 30 |
|
* @category Xyster |
| 31 |
|
* @package Xyster_Collection |
| 32 |
|
* @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net) |
| 33 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 34 |
|
*/ |
| 35 |
|
class Xyster_Collection_Map extends Xyster_Collection_Map_Abstract |
| 36 |
|
{ |
| 37 |
|
/** |
| 38 |
|
* The container for the {@link Xyster_Collection_Map_Entry} objects |
| 39 |
|
* |
| 40 |
|
* @var array |
| 41 |
|
*/ |
| 42 |
|
protected $_items = array(); |
| 43 |
|
|
| 44 |
|
/** |
| 45 |
|
* Whether this map is immutable |
| 46 |
|
* |
| 47 |
|
* @var boolean |
| 48 |
|
*/ |
| 49 |
|
private $_immutable = false; |
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* Creates a new map with object keys |
| 53 |
|
* |
| 54 |
|
* @param Xyster_Collection_Map_Interface $map The values to add to this map |
| 55 |
|
* @param boolean $immutable |
| 56 |
|
*/ |
| 57 |
|
public function __construct( Xyster_Collection_Map_Interface $map = null, $immutable = false ) |
| 58 |
|
{ |
| 59 |
32 |
if ( $map ) { |
| 60 |
5 |
$this->merge($map); |
| 61 |
5 |
} |
| 62 |
32 |
$this->_immutable = $immutable; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
/** |
| 66 |
|
* Removes all items from the map |
| 67 |
|
* |
| 68 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 69 |
|
*/ |
| 70 |
|
public function clear() |
| 71 |
|
{ |
| 72 |
4 |
$this->_failIfImmutable(); |
| 73 |
2 |
$this->_items = array(); |
| 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 |
22 |
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 |
9 |
return ( $this->count() ) ? |
| 94 |
9 |
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 |
2 |
$keys = new Xyster_Collection(); |
| 105 |
2 |
foreach( $this->_items as $entry ) { |
| 106 |
2 |
$keys->add($entry->getKey()); |
| 107 |
2 |
} |
| 108 |
2 |
require_once 'Xyster/Collection/Set.php'; |
| 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 |
|
* @throws Xyster_Collection_Exception if the key type is incorrect |
| 154 |
|
*/ |
| 155 |
|
public function offsetExists( $key ) |
| 156 |
|
{ |
| 157 |
29 |
$hash = $this->_hash($key); |
| 158 |
29 |
return array_key_exists($hash, $this->_items); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
/** |
| 162 |
|
* Gets the value at a specified key |
| 163 |
|
* |
| 164 |
|
* @param object $key The index to get |
| 165 |
|
* @return mixed The value found at $key or null if none |
| 166 |
|
*/ |
| 167 |
|
public function offsetGet( $key ) |
| 168 |
|
{ |
| 169 |
6 |
return $this->offsetExists($key) ? |
| 170 |
6 |
$this->_items[$this->_hash($key)]->getValue() : null; |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
/** |
| 174 |
|
* Sets the value at a given key. |
| 175 |
|
* |
| 176 |
|
* @param object $key The key to set |
| 177 |
|
* @param mixed $value The value to set |
| 178 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 179 |
|
*/ |
| 180 |
|
public function offsetSet( $key, $value ) |
| 181 |
|
{ |
| 182 |
29 |
$this->_failIfImmutable(); |
| 183 |
29 |
if ( !$this->offsetExists($key) ) { |
| 184 |
29 |
$index = $this->_hash($key); |
| 185 |
29 |
$entry = new Xyster_Collection_Map_Entry($key, $value); |
| 186 |
29 |
$this->_items[$index] = $entry; |
| 187 |
29 |
} else { |
| 188 |
1 |
$index = $this->_hash($key); |
| 189 |
1 |
$this->_items[$index]->setValue($value); |
| 190 |
|
} |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
/** |
| 194 |
|
* Removes a value at the specified key |
| 195 |
|
* |
| 196 |
|
* @param object $key The key to "unset" |
| 197 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 198 |
|
*/ |
| 199 |
|
public function offsetUnset( $key ) |
| 200 |
|
{ |
| 201 |
3 |
$this->_failIfImmutable(); |
| 202 |
2 |
if ( $this->offsetExists($key) ) { |
| 203 |
2 |
unset($this->_items[$this->_hash($key)]); |
| 204 |
2 |
} |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
/** |
| 208 |
|
* Puts the items in this map into an array |
| 209 |
|
* |
| 210 |
|
* This array contains objects with the key and value available. |
| 211 |
|
* |
| 212 |
|
* @return array The items in this map |
| 213 |
|
*/ |
| 214 |
|
public function toArray() |
| 215 |
|
{ |
| 216 |
2 |
return array_values($this->_items); |
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
/** |
| 220 |
|
* Gets the values contained in this map |
| 221 |
|
* |
| 222 |
|
* @return Xyster_Collection_Interface |
| 223 |
|
*/ |
| 224 |
|
public function values() |
| 225 |
|
{ |
| 226 |
6 |
$values = array(); |
| 227 |
6 |
foreach( $this->_items as $entry ) { |
| 228 |
6 |
$values[] = $entry->getValue(); |
| 229 |
6 |
} |
| 230 |
6 |
return Xyster_Collection::using($values, true); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
/** |
| 234 |
|
* A convenience method to fail on modification of immutable collection |
| 235 |
|
* |
| 236 |
|
* @throws Xyster_Collection_Exception if the collection is immutable |
| 237 |
|
*/ |
| 238 |
|
private function _failIfImmutable() |
| 239 |
|
{ |
| 240 |
30 |
if ( $this->_immutable ) { |
| 241 |
5 |
require_once 'Xyster/Collection/Exception.php'; |
| 242 |
5 |
throw new Xyster_Collection_Exception("This collection cannot be changed"); |
| 243 |
0 |
} |
| 244 |
|
} |
| 245 |
|
|
| 246 |
|
/** |
| 247 |
|
* Gets the hash code for a value |
| 248 |
|
* |
| 249 |
|
* @param mixed $value |
| 250 |
|
* @return mixed |
| 251 |
|
*/ |
| 252 |
|
private function _hash( $value ) |
| 253 |
|
{ |
| 254 |
29 |
return Xyster_Type::hash($value); |
| 255 |
|
} |
| 256 |
|
} |