| 1 |
<?php |
| 2 |
|
| 3 |
namespace Boxzilla; |
| 4 |
|
| 5 |
use Iterator; |
| 6 |
use Countable; |
| 7 |
use ArrayAccess; |
| 8 |
|
| 9 |
class Collection implements Iterator, Countable, ArrayAccess { |
| 10 |
|
| 11 |
protected $elements = array(); |
| 12 |
private $position = 0; |
| 13 |
|
| 14 |
public function __construct( array $elements ) { |
| 15 |
$this->elements = $elements; |
| 16 |
$this->position = 0; |
| 17 |
} |
| 18 |
|
| 19 |
function rewind() { |
| 20 |
$this->position = 0; |
| 21 |
} |
| 22 |
|
| 23 |
function current() { |
| 24 |
return $this->elements[ $this->position ]; |
| 25 |
} |
| 26 |
|
| 27 |
function key() { |
| 28 |
return $this->position; |
| 29 |
} |
| 30 |
|
| 31 |
function next() { |
| 32 |
++$this->position; |
| 33 |
} |
| 34 |
|
| 35 |
function valid() { |
| 36 |
return isset( $this->elements[ $this->position ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param $callback |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
function map($callback) { |
| 45 |
$result = array(); |
| 46 |
|
| 47 |
foreach( $this->elements as $element ) { |
| 48 |
$result[] = $callback( $element ); |
| 49 |
} |
| 50 |
|
| 51 |
return $result; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param $callback |
| 56 |
* |
| 57 |
* @return null |
| 58 |
*/ |
| 59 |
function find($callback) { |
| 60 |
|
| 61 |
foreach( $this->elements as $element ) { |
| 62 |
if( $callback( $element ) ) { |
| 63 |
return $element; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return null; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* (PHP 5 >= 5.1.0)<br/> |
| 72 |
* Count elements of an object |
| 73 |
* @link http://php.net/manual/en/countable.count.php |
| 74 |
* @return int The custom count as an integer. |
| 75 |
* </p> |
| 76 |
* <p> |
| 77 |
* The return value is cast to an integer. |
| 78 |
*/ |
| 79 |
public function count() { |
| 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 ]; |
| 153 |
} |
| 154 |
} |