| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalAccessException'); |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class stubArrayAccessor extends stubBaseObject implements ArrayAccess, Countable, IteratorAggregate |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
* the wrapped array |
|---|
| 38 |
* |
|---|
| 39 |
* @var array |
|---|
| 40 |
*/ |
|---|
| 41 |
protected $data = array(); |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* constructor |
|---|
| 45 |
* |
|---|
| 46 |
* @param array $data optional |
|---|
| 47 |
*/ |
|---|
| 48 |
public function __construct(array $data = array()) |
|---|
| 49 |
{ |
|---|
| 50 |
$this->data = $data; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* returns value of first element |
|---|
| 55 |
* |
|---|
| 56 |
* Returns null if there is no first element. |
|---|
| 57 |
* |
|---|
| 58 |
* @return mixed |
|---|
| 59 |
*/ |
|---|
| 60 |
public function first() |
|---|
| 61 |
{ |
|---|
| 62 |
if (count($this->data) === 0) { |
|---|
| 63 |
return null; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
reset($this->data); |
|---|
| 67 |
return current($this->data); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* returns value of last element |
|---|
| 72 |
* |
|---|
| 73 |
* @return mixed |
|---|
| 74 |
*/ |
|---|
| 75 |
public function last() |
|---|
| 76 |
{ |
|---|
| 77 |
if (count($this->data) === 0) { |
|---|
| 78 |
return null; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
return end($this->data); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
* returns value stored under given offset |
|---|
| 86 |
* |
|---|
| 87 |
* @param string|int $offset |
|---|
| 88 |
* @return mixed |
|---|
| 89 |
*/ |
|---|
| 90 |
public function at($offset) |
|---|
| 91 |
{ |
|---|
| 92 |
return $this->offsetGet($offset); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
* replaces current array with another array |
|---|
| 97 |
* |
|---|
| 98 |
* @param array $data |
|---|
| 99 |
*/ |
|---|
| 100 |
public function replace(array $data) |
|---|
| 101 |
{ |
|---|
| 102 |
$this->data = $data; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
* returns raw array |
|---|
| 107 |
* |
|---|
| 108 |
* @return array |
|---|
| 109 |
*/ |
|---|
| 110 |
public function toArray() |
|---|
| 111 |
{ |
|---|
| 112 |
return $this->data; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
* checks whether an entry for given offset exists |
|---|
| 117 |
* |
|---|
| 118 |
* @param string|int $offset |
|---|
| 119 |
* @return bool |
|---|
| 120 |
*/ |
|---|
| 121 |
public function offsetExists($offset) |
|---|
| 122 |
{ |
|---|
| 123 |
return isset($this->data[$offset]); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
* returns value stored under given offset |
|---|
| 128 |
* |
|---|
| 129 |
* @param string|int $offset |
|---|
| 130 |
* @return mixed |
|---|
| 131 |
* @throws stubIllegalAccessException |
|---|
| 132 |
*/ |
|---|
| 133 |
public function offsetGet($offset) |
|---|
| 134 |
{ |
|---|
| 135 |
if (isset($this->data[$offset]) === true) { |
|---|
| 136 |
return $this->data[$offset]; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
throw new stubIllegalAccessException('No element for offset ' . $offset); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
* sets value at given offset |
|---|
| 144 |
* |
|---|
| 145 |
* @param string|int $offset |
|---|
| 146 |
* @param mixed $value |
|---|
| 147 |
*/ |
|---|
| 148 |
public function offsetSet($offset, $value) |
|---|
| 149 |
{ |
|---|
| 150 |
$this->data[$offset] = $value; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
* removes given offset |
|---|
| 155 |
* |
|---|
| 156 |
* @param string|int $offset |
|---|
| 157 |
*/ |
|---|
| 158 |
public function offsetUnset($offset) |
|---|
| 159 |
{ |
|---|
| 160 |
unset($this->data[$offset]); |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
* returns amount of elements |
|---|
| 165 |
* |
|---|
| 166 |
* @return int |
|---|
| 167 |
*/ |
|---|
| 168 |
public function count() |
|---|
| 169 |
{ |
|---|
| 170 |
return count($this->data); |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
* returns an iterator to be used in foreach |
|---|
| 175 |
* |
|---|
| 176 |
* @return Iterator |
|---|
| 177 |
*/ |
|---|
| 178 |
public function getIterator() |
|---|
| 179 |
{ |
|---|
| 180 |
return new ArrayIterator($this->data); |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
?> |
|---|