| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Collection |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
* @version $Id$ |
| 20 |
|
*/ |
| 21 |
|
/** |
| 22 |
|
* Xyster_Collection_Abstract |
| 23 |
|
*/ |
| 24 |
1 |
require_once 'Xyster/Collection/Abstract.php'; |
| 25 |
|
/** |
| 26 |
|
* Implementation of Xyster_Collection_Abstract with static helper methods |
| 27 |
|
* |
| 28 |
|
* @category Xyster |
| 29 |
|
* @package Xyster_Collection |
| 30 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 31 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 32 |
|
*/ |
| 33 |
|
class Xyster_Collection extends Xyster_Collection_Abstract |
| 34 |
|
{ |
| 35 |
|
private $_immutable = false; |
| 36 |
|
|
| 37 |
|
/** |
| 38 |
|
* Creates a new simple collection |
| 39 |
|
* |
| 40 |
|
* @param Xyster_Collection_Interface $collection |
| 41 |
|
* @param boolean $immutable |
| 42 |
|
*/ |
| 43 |
|
public function __construct( Xyster_Collection_Interface $collection = null, $immutable = false ) |
| 44 |
|
{ |
| 45 |
379 |
if ( $collection ) { |
| 46 |
35 |
$this->merge($collection); |
| 47 |
35 |
} |
| 48 |
379 |
$this->_immutable = $immutable; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* Adds an item to the collection |
| 53 |
|
* |
| 54 |
|
* @param mixed $item The item to add |
| 55 |
|
* @return boolean Whether the collection changed as a result of this method |
| 56 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 57 |
|
*/ |
| 58 |
|
public function add( $item ) |
| 59 |
|
{ |
| 60 |
145 |
$this->_failIfImmutable(); |
| 61 |
145 |
return parent::add($item); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
/** |
| 65 |
|
* Removes all items from the collection |
| 66 |
|
* |
| 67 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 68 |
|
*/ |
| 69 |
|
public function clear() |
| 70 |
|
{ |
| 71 |
17 |
$this->_failIfImmutable(); |
| 72 |
13 |
parent::clear(); |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
/** |
| 76 |
|
* Removes the specified value from the collection |
| 77 |
|
* |
| 78 |
|
* @param mixed $item The value to remove |
| 79 |
|
* @return boolean If the value was in the collection |
| 80 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 81 |
|
*/ |
| 82 |
|
public function remove( $item ) |
| 83 |
|
{ |
| 84 |
10 |
$this->_failIfImmutable(); |
| 85 |
8 |
return parent::remove($item); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
/** |
| 89 |
|
* Removes all of the specified values from the collection |
| 90 |
|
* |
| 91 |
|
* @param Xyster_Collection_Interface $values The values to remove |
| 92 |
|
* @return boolean Whether the collection changed as a result of this method |
| 93 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 94 |
|
*/ |
| 95 |
|
public function removeAll( Xyster_Collection_Interface $values ) |
| 96 |
|
{ |
| 97 |
5 |
$this->_failIfImmutable(); |
| 98 |
3 |
return parent::removeAll($values); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Removes all values from the collection except for the ones specified |
| 103 |
|
* |
| 104 |
|
* @param Xyster_Collection_Interface $values The values to keep |
| 105 |
|
* @return boolean Whether the collection changed as a result of this method |
| 106 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 107 |
|
*/ |
| 108 |
|
public function retainAll( Xyster_Collection_Interface $values ) |
| 109 |
|
{ |
| 110 |
5 |
$this->_failIfImmutable(); |
| 111 |
3 |
return parent::retainAll($values); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
/** |
| 115 |
|
* Creates a new collection containing the values |
| 116 |
|
* |
| 117 |
|
* @param array $values |
| 118 |
|
* @param boolean $immutable |
| 119 |
|
* @return Xyster_Collection_Interface |
| 120 |
|
*/ |
| 121 |
|
static public function using( array $values, $immutable = false ) |
| 122 |
|
{ |
| 123 |
65 |
$collection = new Xyster_Collection(null,$immutable); |
| 124 |
65 |
$collection->_items = array_values($values); |
| 125 |
65 |
return $collection; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
/** |
| 129 |
|
* Returns a new unchangable collection containing all the supplied values |
| 130 |
|
* |
| 131 |
|
* @param Xyster_Collection_Interface $collection |
| 132 |
|
* @return Xyster_Collection_Interface |
| 133 |
|
*/ |
| 134 |
|
static public function fixedCollection( Xyster_Collection_Interface $collection ) |
| 135 |
|
{ |
| 136 |
7 |
return new Xyster_Collection( $collection, true ); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
/** |
| 140 |
|
* Returns a new unchangable list containing all the supplied values |
| 141 |
|
* |
| 142 |
|
* @param Xyster_Collection_List_Interface $list |
| 143 |
|
* @return Xyster_Collection_List_Interface |
| 144 |
|
*/ |
| 145 |
|
static public function fixedList( Xyster_Collection_List_Interface $list ) |
| 146 |
|
{ |
| 147 |
1 |
return new Xyster_Collection_List( $list, true ); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
/** |
| 151 |
|
* Returns a new unchangable map containing all the supplied key/value pairs |
| 152 |
|
* |
| 153 |
|
* @param Xyster_Collection_Map_Interface $map |
| 154 |
|
* @return Xyster_Collection_Map_Interface |
| 155 |
|
*/ |
| 156 |
|
static public function fixedMap( Xyster_Collection_Map_Interface $map ) |
| 157 |
|
{ |
| 158 |
5 |
return new Xyster_Collection_Map( $map, true ); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
/** |
| 162 |
|
* Returns a new unchangable set containing all the supplied values |
| 163 |
|
* |
| 164 |
|
* @param Xyster_Collection_Set_Interface $set |
| 165 |
|
* @return Xyster_Collection_Set_Interface |
| 166 |
|
*/ |
| 167 |
|
static public function fixedSet( Xyster_Collection_Set_Interface $set ) |
| 168 |
|
{ |
| 169 |
5 |
return new Xyster_Collection_Set( $set, true ); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
/** |
| 173 |
|
* A convenience method to fail on modification of immutable collection |
| 174 |
|
* |
| 175 |
|
* @throws Xyster_Collection_Exception if the collection is immutable |
| 176 |
|
*/ |
| 177 |
|
private function _failIfImmutable() |
| 178 |
|
{ |
| 179 |
148 |
if ( $this->_immutable ) { |
| 180 |
14 |
require_once 'Xyster/Collection/Exception.php'; |
| 181 |
14 |
throw new Xyster_Collection_Exception("This collection cannot be changed"); |
| 182 |
0 |
} |
| 183 |
|
} |
| 184 |
|
} |