| @@ -3,10 +3,11 @@ | ||
| 3 | 3 | namespace Boxzilla; |
| 4 | 4 | |
| 5 | 5 | use Iterator; |
| 6 | 6 | use Countable; |
| 7 | +use ArrayAccess; | |
| 7 | 8 | |
| 8 | -class Collection implements Iterator, Countable { | |
| 9 | +class Collection implements Iterator, Countable, ArrayAccess { | |
| 9 | 10 | |
| 10 | 11 | protected $elements = array(); |
| 11 | 12 | private $position = 0; |
| 12 | 13 | |
| @@ -76,6 +77,78 @@ | ||
| 76 | 77 | * The return value is cast to an integer. |
| 77 | 78 | */ |
| 78 | 79 | public function count() { |
| 79 | 80 | return count( $this->elements ); |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Whether a offset exists | |
| 85 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php | |
| 86 | + * @param mixed $offset <p> | |
| 87 | + * An offset to check for. | |
| 88 | + * </p> | |
| 89 | + * @return boolean true on success or false on failure. | |
| 90 | + * </p> | |
| 91 | + * <p> | |
| 92 | + * The return value will be casted to boolean if non-boolean was returned. | |
| 93 | + * @since 5.0.0 | |
| 94 | + */ | |
| 95 | + public function offsetExists($offset) | |
| 96 | + { | |
| 97 | + return isset( $this->elements[ $offset ] ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Offset to retrieve | |
| 102 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php | |
| 103 | + * @param mixed $offset <p> | |
| 104 | + * The offset to retrieve. | |
| 105 | + * </p> | |
| 106 | + * @return mixed Can return all value types. | |
| 107 | + * @since 5.0.0 | |
| 108 | + */ | |
| 109 | + public function offsetGet($offset) | |
| 110 | + { | |
| 111 | + return $this->elements[ $offset ]; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Offset to set | |
| 116 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php | |
| 117 | + * @param mixed $offset <p> | |
| 118 | + * The offset to assign the value to. | |
| 119 | + * </p> | |
| 120 | + * @param mixed $value <p> | |
| 121 | + * The value to set. | |
| 122 | + * </p> | |
| 123 | + * @return void | |
| 124 | + * @since 5.0.0 | |
| 125 | + */ | |
| 126 | + public function offsetSet($offset, $value) | |
| 127 | + { | |
| 128 | + $this->elements[ $offset ] = $value; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Offset to unset | |
| 133 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php | |
| 134 | + * @param mixed $offset <p> | |
| 135 | + * The offset to unset. | |
| 136 | + * </p> | |
| 137 | + * @return void | |
| 138 | + * @since 5.0.0 | |
| 139 | + */ | |
| 140 | + public function offsetUnset($offset) | |
| 141 | + { | |
| 142 | + unset( $this->elements[ $offset] ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * Return a random value out of the collection. | |
| 147 | + * | |
| 148 | + * @return mixed | |
| 149 | + */ | |
| 150 | + public function random() { | |
| 151 | + $key = array_rand( $this->elements ); | |
| 152 | + return $this->elements[ $key ]; | |
| 80 | 153 | } |
| 81 | 154 | } |