| 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 |
|
* Xyster_Collection_List_Abstract |
| 17 |
|
*/ |
| 18 |
1 |
require_once 'Xyster/Collection/List/Abstract.php'; |
| 19 |
|
/** |
| 20 |
|
* Simple implementation of an index-based collection |
| 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 |
|
class Xyster_Collection_List extends Xyster_Collection_List_Abstract |
| 28 |
|
{ |
| 29 |
|
private $_immutable; |
| 30 |
|
|
| 31 |
|
/** |
| 32 |
|
* Creates a new list |
| 33 |
|
* |
| 34 |
|
* @param Xyster_Collection_Interface $values Any values to add to this list |
| 35 |
|
* @param boolean $immutable Whether the set can be changed |
| 36 |
|
*/ |
| 37 |
|
public function __construct( Xyster_Collection_Interface $values = null, $immutable = null ) |
| 38 |
|
{ |
| 39 |
33 |
if ( $values ) { |
| 40 |
13 |
$this->merge($values); |
| 41 |
13 |
} |
| 42 |
33 |
$this->_immutable = $immutable; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
/** |
| 46 |
|
* Adds an item to the collection |
| 47 |
|
* |
| 48 |
|
* @param mixed $item The item to add |
| 49 |
|
* @return boolean Whether the collection changed as a result of this method |
| 50 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 51 |
|
*/ |
| 52 |
|
public function add( $item ) |
| 53 |
|
{ |
| 54 |
30 |
$this->_failIfImmutable(); |
| 55 |
30 |
return parent::add($item); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
/** |
| 59 |
|
* Removes all items from the collection |
| 60 |
|
* |
| 61 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 62 |
|
*/ |
| 63 |
|
public function clear() |
| 64 |
|
{ |
| 65 |
4 |
$this->_failIfImmutable(); |
| 66 |
2 |
parent::clear(); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
/** |
| 70 |
|
* Inserts a value into the list at the specified index |
| 71 |
|
* |
| 72 |
|
* {@inherit} |
| 73 |
|
* |
| 74 |
|
* @param int $index The index at which to insert |
| 75 |
|
* @param mixed $value The value to insert |
| 76 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 77 |
|
* @throws OutOfBoundsException if the index is invalid |
| 78 |
|
*/ |
| 79 |
|
public function insert( $index, $value ) |
| 80 |
|
{ |
| 81 |
2 |
$this->_failIfImmutable(); |
| 82 |
1 |
parent::insert($index, $value); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
|
* Inserts the supplied values into the list at the specified index |
| 87 |
|
* |
| 88 |
|
* {@inherit} |
| 89 |
|
* |
| 90 |
|
* @param int $index The index at which to insert |
| 91 |
|
* @param Xyster_Collection_Interface $values The value to insert |
| 92 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 93 |
|
* @throws OutOfBoundsException if the index is invalid |
| 94 |
|
*/ |
| 95 |
|
public function insertAll( $index, Xyster_Collection_Interface $values ) |
| 96 |
|
{ |
| 97 |
2 |
$this->_failIfImmutable(); |
| 98 |
1 |
parent::insertAll($index, $values); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Sets the value at a given index. |
| 103 |
|
* |
| 104 |
|
* The index must be greater than or equal to 0 and less than or equal to |
| 105 |
|
* the size of this collection. In other words, an index is valid if |
| 106 |
|
* <code>( $index < 0 || $index > $this->count() )</code> is false. |
| 107 |
|
* |
| 108 |
|
* @param int $index The index to set |
| 109 |
|
* @param mixed $value The value to set |
| 110 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 111 |
|
* @throws OutOfBoundsException if the index is invalid |
| 112 |
|
*/ |
| 113 |
|
public function offsetSet( $index, $value ) |
| 114 |
|
{ |
| 115 |
2 |
$this->_failIfImmutable(); |
| 116 |
1 |
parent::offsetSet($index, $value); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
|
* Removes a value at the specified index |
| 121 |
|
* |
| 122 |
|
* {@inherit} |
| 123 |
|
* |
| 124 |
|
* @param int $index The index to "unset" |
| 125 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 126 |
|
* @throws OutOfBoundsException if the index is invalid |
| 127 |
|
*/ |
| 128 |
|
public function offsetUnset( $index ) |
| 129 |
|
{ |
| 130 |
2 |
$this->_failIfImmutable(); |
| 131 |
1 |
parent::offsetUnset($index); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
/** |
| 135 |
|
* Removes the specified value from the collection |
| 136 |
|
* |
| 137 |
|
* @param mixed $item The value to remove |
| 138 |
|
* @return boolean If the value was in the collection |
| 139 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 140 |
|
*/ |
| 141 |
|
public function remove( $item ) |
| 142 |
|
{ |
| 143 |
2 |
$this->_failIfImmutable(); |
| 144 |
1 |
return parent::remove($item); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
/** |
| 148 |
|
* Removes all of the specified values from the collection |
| 149 |
|
* |
| 150 |
|
* @param Xyster_Collection_Interface $values The values to remove |
| 151 |
|
* @return boolean Whether the collection changed as a result of this method |
| 152 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 153 |
|
*/ |
| 154 |
|
public function removeAll( Xyster_Collection_Interface $values ) |
| 155 |
|
{ |
| 156 |
2 |
$this->_failIfImmutable(); |
| 157 |
1 |
return parent::removeAll($values); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
/** |
| 161 |
|
* Removes all values from the collection except for the ones specified |
| 162 |
|
* |
| 163 |
|
* @param Xyster_Collection_Interface $values The values to keep |
| 164 |
|
* @return boolean Whether the collection changed as a result of this method |
| 165 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 166 |
|
*/ |
| 167 |
|
public function retainAll( Xyster_Collection_Interface $values ) |
| 168 |
|
{ |
| 169 |
2 |
$this->_failIfImmutable(); |
| 170 |
1 |
return parent::retainAll($values); |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
/** |
| 174 |
|
* Removes $count elements starting at $from |
| 175 |
|
* |
| 176 |
|
* @param int $from The starting index |
| 177 |
|
* @param int $count The number of elements to remove |
| 178 |
|
* @throws Xyster_Collection_Exception if the collection cannot be modified |
| 179 |
|
* @throws OutOfBoundsException if $from is invalid |
| 180 |
|
*/ |
| 181 |
|
public function slice( $from, $count ) |
| 182 |
|
{ |
| 183 |
2 |
$this->_failIfImmutable(); |
| 184 |
1 |
parent::slice($from, $count); |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
/** |
| 188 |
|
* A convenience method to fail on modification of immutable collection |
| 189 |
|
* |
| 190 |
|
* @throws Xyster_Collection_Exception if the collection is immutable |
| 191 |
|
*/ |
| 192 |
|
private function _failIfImmutable() |
| 193 |
|
{ |
| 194 |
32 |
if ( $this->_immutable ) { |
| 195 |
12 |
require_once 'Xyster/Collection/Exception.php'; |
| 196 |
12 |
throw new Xyster_Collection_Exception("This collection cannot be changed"); |
| 197 |
0 |
} |
| 198 |
|
} |
| 199 |
|
} |