|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ---------------------------------------------------------------------------- |
| 4 | + * This code is part of the Sclable Business Application Development Platform |
| 5 | + * and is subject to the provisions of your License Agreement with |
| 6 | + * Sclable Business Solutions GmbH. |
| 7 | + * |
| 8 | + * @copyright (c) 2016 Sclable Business Solutions GmbH |
| 9 | + * ---------------------------------------------------------------------------- |
| 10 | + */ |
| 11 | + |
| 12 | +namespace sclable\arrayFunctions; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class AccessibleArray |
| 16 | + * |
| 17 | + * |
| 18 | + * @package sclable\arrayFunctions |
| 19 | + * @author Michael Rutz <[email protected]> |
| 20 | + * |
| 21 | + */ |
| 22 | +class AccessibleArray implements \ArrayAccess |
| 23 | +{ |
| 24 | + /** |
| 25 | + * the raw array data container |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + protected $data; |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * ArrayWrap constructor. |
| 33 | + * @param array $data |
| 34 | + */ |
| 35 | + public function __construct($data) |
| 36 | + { |
| 37 | + if (is_array($data) !== true) { |
| 38 | + throw new \InvalidArgumentException('The first argument $data is not an array.'); |
| 39 | + } |
| 40 | + |
| 41 | + $this->data = $data; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Whether a offset exists |
| 46 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
| 47 | + * @param mixed $offset An offset to check for. |
| 48 | + * @return boolean true on success or false on failure. |
| 49 | + * The return value will be casted to boolean if non-boolean was returned. |
| 50 | + */ |
| 51 | + public function offsetExists($offset) |
| 52 | + { |
| 53 | + return array_key_exists($offset, $this->data); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Offset to retrieve |
| 58 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
| 59 | + * @param mixed $offset The offset to retrieve. |
| 60 | + * @return mixed Can return all value types. |
| 61 | + */ |
| 62 | + public function offsetGet($offset) |
| 63 | + { |
| 64 | + return array_key_exists($offset, $this->data) ? $this->data[$offset] : null; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Offset to set |
| 69 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
| 70 | + * @param mixed $offset The offset to assign the value to. |
| 71 | + * @param mixed $value The value to set. |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + public function offsetSet($offset, $value) |
| 75 | + { |
| 76 | + $this->data[$offset] = $value; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Offset to unset |
| 81 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
| 82 | + * @param mixed $offset The offset to unset. |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function offsetUnset($offset) |
| 86 | + { |
| 87 | + unset($this->data[$offset]); |
| 88 | + } |
| 89 | +} |
0 commit comments