|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Minds Repository Response |
| 5 | + * |
| 6 | + * @author emi |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Minds\Common\Repository; |
| 10 | + |
| 11 | +class Response implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable |
| 12 | +{ |
| 13 | + /** @var array */ |
| 14 | + protected $data = []; |
| 15 | + |
| 16 | + /** @var string */ |
| 17 | + protected $pagingToken; |
| 18 | + |
| 19 | + public function __construct(array $data = null, $pagingToken = null) |
| 20 | + { |
| 21 | + if ($data !== null) { |
| 22 | + $this->data = $data; |
| 23 | + } |
| 24 | + |
| 25 | + if ($pagingToken !== null) { |
| 26 | + $this->pagingToken = $pagingToken; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Sets the paging token for this result set |
| 32 | + * @param string $pagingToken |
| 33 | + * @return Response |
| 34 | + */ |
| 35 | + public function setPagingToken($pagingToken) |
| 36 | + { |
| 37 | + $this->pagingToken = $pagingToken; |
| 38 | + return $this; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets the paging token for this result set |
| 43 | + * @return string |
| 44 | + */ |
| 45 | + public function getPagingToken() |
| 46 | + { |
| 47 | + return $this->pagingToken; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Return the current element |
| 52 | + * @link http://php.net/manual/en/iterator.current.php |
| 53 | + * @return mixed Can return any type. |
| 54 | + * @since 5.0.0 |
| 55 | + */ |
| 56 | + public function current() |
| 57 | + { |
| 58 | + return current($this->data); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Move forward to next element |
| 63 | + * @link http://php.net/manual/en/iterator.next.php |
| 64 | + * @return void Any returned value is ignored. |
| 65 | + * @since 5.0.0 |
| 66 | + */ |
| 67 | + public function next() |
| 68 | + { |
| 69 | + next($this->data); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Return the key of the current element |
| 74 | + * @link http://php.net/manual/en/iterator.key.php |
| 75 | + * @return mixed scalar on success, or null on failure. |
| 76 | + * @since 5.0.0 |
| 77 | + */ |
| 78 | + public function key() |
| 79 | + { |
| 80 | + return key($this->data); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks if current position is valid |
| 85 | + * @link http://php.net/manual/en/iterator.valid.php |
| 86 | + * @return boolean The return value will be casted to boolean and then evaluated. |
| 87 | + * Returns true on success or false on failure. |
| 88 | + * @since 5.0.0 |
| 89 | + */ |
| 90 | + public function valid() |
| 91 | + { |
| 92 | + return key($this->data) !== null; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Rewind the Iterator to the first element |
| 97 | + * @link http://php.net/manual/en/iterator.rewind.php |
| 98 | + * @return void Any returned value is ignored. |
| 99 | + * @since 5.0.0 |
| 100 | + */ |
| 101 | + public function rewind() |
| 102 | + { |
| 103 | + reset($this->data); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Rewinds the Iterator to the first element and returns its value |
| 108 | + * @return mixed |
| 109 | + */ |
| 110 | + public function reset() |
| 111 | + { |
| 112 | + return reset($this->data); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the pointer onto the last Iterator element and returns its value |
| 117 | + * @return mixed |
| 118 | + */ |
| 119 | + public function end() |
| 120 | + { |
| 121 | + return end($this->data); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Whether a offset exists |
| 126 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 127 | + * @param mixed $offset <p> |
| 128 | + * An offset to check for. |
| 129 | + * </p> |
| 130 | + * @return boolean true on success or false on failure. |
| 131 | + * </p> |
| 132 | + * <p> |
| 133 | + * The return value will be casted to boolean if non-boolean was returned. |
| 134 | + * @since 5.0.0 |
| 135 | + */ |
| 136 | + public function offsetExists($offset) |
| 137 | + { |
| 138 | + return isset($this->data[$offset]); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Offset to retrieve |
| 143 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 144 | + * @param mixed $offset <p> |
| 145 | + * The offset to retrieve. |
| 146 | + * </p> |
| 147 | + * @return mixed Can return all value types. |
| 148 | + * @since 5.0.0 |
| 149 | + */ |
| 150 | + public function offsetGet($offset) |
| 151 | + { |
| 152 | + return $this->data[$offset]; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Offset to set |
| 157 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 158 | + * @param mixed $offset <p> |
| 159 | + * The offset to assign the value to. |
| 160 | + * </p> |
| 161 | + * @param mixed $value <p> |
| 162 | + * The value to set. |
| 163 | + * </p> |
| 164 | + * @return void |
| 165 | + * @since 5.0.0 |
| 166 | + */ |
| 167 | + public function offsetSet($offset, $value) |
| 168 | + { |
| 169 | + if ($offset === null) { |
| 170 | + $this->data[] = $value; |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + $this->data[$offset] = $value; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Offset to unset |
| 179 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 180 | + * @param mixed $offset <p> |
| 181 | + * The offset to unset. |
| 182 | + * </p> |
| 183 | + * @return void |
| 184 | + * @since 5.0.0 |
| 185 | + */ |
| 186 | + public function offsetUnset($offset) |
| 187 | + { |
| 188 | + unset($this->data[$offset]); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Count elements of an object |
| 193 | + * @link http://php.net/manual/en/countable.count.php |
| 194 | + * @return int The custom count as an integer. |
| 195 | + * </p> |
| 196 | + * <p> |
| 197 | + * The return value is cast to an integer. |
| 198 | + * @since 5.1.0 |
| 199 | + */ |
| 200 | + public function count() |
| 201 | + { |
| 202 | + return count($this->data); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Exports the data array |
| 207 | + * @return array |
| 208 | + */ |
| 209 | + public function toArray() |
| 210 | + { |
| 211 | + return $this->data ?: []; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Specify data which should be serialized to JSON |
| 216 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
| 217 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
| 218 | + * which is a value of any type other than a resource. |
| 219 | + * @since 5.4.0 |
| 220 | + */ |
| 221 | + public function jsonSerialize() |
| 222 | + { |
| 223 | + return $this->toArray(); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Returns a clone of this response with the inverse order |
| 228 | + * @return Response |
| 229 | + */ |
| 230 | + public function reverse($preserveKeys = false) |
| 231 | + { |
| 232 | + return new static(array_reverse($this->data, $preserveKeys), $this->pagingToken); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Iterates over each element passing them to the callback function. |
| 237 | + * If the callback function returns true, the element is returned into |
| 238 | + * the result Response. |
| 239 | + * @param callable $callback |
| 240 | + * @param bool $preserveKeys |
| 241 | + * @return Response |
| 242 | + */ |
| 243 | + public function filter($callback, $preserveKeys = false) |
| 244 | + { |
| 245 | + $filtered = array_filter($this->data, $callback, ARRAY_FILTER_USE_BOTH); |
| 246 | + |
| 247 | + if (!$preserveKeys) { |
| 248 | + $filtered = array_values($filtered); |
| 249 | + } |
| 250 | + |
| 251 | + return new static($filtered, $this->pagingToken); |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Applies the callback to the elements and returns a clone of the Response |
| 256 | + * @param callable $callback |
| 257 | + * @return Response |
| 258 | + */ |
| 259 | + public function map($callback) |
| 260 | + { |
| 261 | + return new static(array_map($callback, $this->data), $this->pagingToken); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Iteratively reduce the Response to a single value using a callback function |
| 266 | + * @param callable $callback |
| 267 | + * @param mixed $initialValue |
| 268 | + * @return mixed |
| 269 | + */ |
| 270 | + public function reduce($callback, $initialValue = null) |
| 271 | + { |
| 272 | + return array_reduce($this->data, $callback, $initialValue); |
| 273 | + } |
| 274 | +} |
0 commit comments