| 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_Interface |
| 17 |
|
*/ |
| 18 |
1 |
require_once 'Xyster/Collection/Map/Interface.php'; |
| 19 |
|
/** |
| 20 |
|
* Abstract class for key-based collections |
| 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 |
|
abstract class Xyster_Collection_Map_Abstract implements Xyster_Collection_Map_Interface |
| 28 |
|
{ |
| 29 |
|
/** |
| 30 |
|
* Tests to see whether the map contains the key supplied |
| 31 |
|
* |
| 32 |
|
* If the supplied value is an object, and the map allows objects as keys, |
| 33 |
|
* the comparison will be done for identity (===) and not for value (==). |
| 34 |
|
* |
| 35 |
|
* This method is an alias to ArrayAccess::offsetExists |
| 36 |
|
* |
| 37 |
|
* @param mixed $key The key to test |
| 38 |
|
* @return boolean Whether the map contains the supplied key |
| 39 |
|
*/ |
| 40 |
|
public function containsKey( $key ) |
| 41 |
|
{ |
| 42 |
6 |
return $this->offsetExists($key); |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
/** |
| 46 |
|
* Tests to see whether the map contains the value supplied |
| 47 |
|
* |
| 48 |
|
* If the supplied value is an object, the comparison will be done for |
| 49 |
|
* identity (===) and not for value (==). |
| 50 |
|
* |
| 51 |
|
* @param mixed $item The item to test |
| 52 |
|
* @return boolean Whether the map contains the supplied value |
| 53 |
|
*/ |
| 54 |
|
public function containsValue( $item ) |
| 55 |
|
{ |
| 56 |
4 |
foreach( $this->values() as $value ) { |
| 57 |
4 |
if ( $item === $value ) { |
| 58 |
4 |
return true; |
| 59 |
0 |
} |
| 60 |
3 |
} |
| 61 |
2 |
return false; |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* Gets the value corresponding to the supplied key |
| 66 |
|
* |
| 67 |
|
* This method could return null if the value is null, or if the value was |
| 68 |
|
* not found. Use {@link containsValue} to check which is true. |
| 69 |
|
* |
| 70 |
|
* @param mixed $key |
| 71 |
|
* @return mixed The value found, or null if none |
| 72 |
|
*/ |
| 73 |
|
public function get( $key ) |
| 74 |
|
{ |
| 75 |
82 |
return $this->offsetGet($key); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
/** |
| 79 |
|
* Tests to see if the collection contains no elements |
| 80 |
|
* |
| 81 |
|
* The return value from this method should be equivalent to |
| 82 |
|
* <code>( $collection->count() == 0 )</code>. |
| 83 |
|
* |
| 84 |
|
* @return boolean Whether this collection has no elements |
| 85 |
|
*/ |
| 86 |
|
public function isEmpty() |
| 87 |
|
{ |
| 88 |
3 |
return $this->count() == 0; |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
/** |
| 92 |
|
* Combines this map with the supplied map |
| 93 |
|
* |
| 94 |
|
* The values in the supplied map will take precedence if this map and the |
| 95 |
|
* supplied map have duplicate keys. |
| 96 |
|
* |
| 97 |
|
* @param Xyster_Map_Interface $map |
| 98 |
|
* @return boolean Whether the map changed as a result of this method |
| 99 |
|
*/ |
| 100 |
|
public function merge( Xyster_Collection_Map_Interface $map ) |
| 101 |
|
{ |
| 102 |
8 |
$changed = false; |
| 103 |
8 |
foreach( $map as $key=>$value ) { |
| 104 |
7 |
if ( $value instanceof Xyster_Collection_Map_Entry ) { |
| 105 |
5 |
if ( !$changed && $this->get($value->getKey()) !== $value->getValue() ) { |
| 106 |
5 |
$changed = true; |
| 107 |
5 |
} |
| 108 |
5 |
$this->set($value->getKey(), $value->getValue()); |
| 109 |
5 |
} else { |
| 110 |
2 |
if ( !$changed && $this->get($key) !== $value ) { |
| 111 |
2 |
$changed = true; |
| 112 |
2 |
} |
| 113 |
2 |
$this->set($key, $value); |
| 114 |
|
} |
| 115 |
7 |
} |
| 116 |
8 |
return $changed; |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
|
* Removes the mapping for the specified key |
| 121 |
|
* |
| 122 |
|
* This method is an alias to ArrayAccess::offsetUnset |
| 123 |
|
* |
| 124 |
|
* @param mixed $key |
| 125 |
|
*/ |
| 126 |
|
public function remove( $key ) |
| 127 |
|
{ |
| 128 |
13 |
$this->offsetUnset($key); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
/** |
| 132 |
|
* Sets a mapping for the key and value supplied |
| 133 |
|
* |
| 134 |
|
* This method is an alias to ArrayAccess::offsetSet |
| 135 |
|
* |
| 136 |
|
* @param mixed $key |
| 137 |
|
* @param mixed $value |
| 138 |
|
*/ |
| 139 |
|
public function set( $key, $value ) |
| 140 |
|
{ |
| 141 |
105 |
$this->offsetSet($key,$value); |
| 142 |
|
} |
| 143 |
|
} |