1 <?php
2
3 namespace Alo\Traversables;
4
5 use IteratorAggregate;
6 use ArrayIterator;
7 use ArrayAccess;
8 use Countable;
9
10 if (!defined('GEN_START')) {
11 http_response_code(404);
12 } else {
13
14 /**
15 * Makes an object usable as an array
16 * @author Art <a.molcanovas@gmail.com>
17 */
18 class ArrayObj implements IteratorAggregate, ArrayAccess, Countable {
19
20 /**
21 * The array we're working with
22 * @var array
23 */
24 protected $data;
25
26 /**
27 * Initialises our smart array-based object
28 * @author Art <a.molcanovas@gmail.com>
29 *
30 * @param array $initial The initial array to set
31 */
32 function __construct(array $initial = []) {
33 $this->data = $initial;
34 }
35
36 /**
37 * Returns the number of items in the data object
38 * @author Art <a.molcanovas@gmail.com>
39 * @return int
40 */
41 function count() {
42 return count($this->data);
43 }
44
45 /**
46 * Returns an array value
47 *
48 * @author Art <a.molcanovas@gmail.com>
49 *
50 * @param string $k The value's key
51 *
52 * @return mixed
53 */
54 function __get($k) {
55 return get($this->data[$k]);
56 }
57
58 /**
59 * Sets an array value
60 *
61 * @author Art <a.molcanovas@gmail.com>
62 *
63 * @param string $k The key
64 * @param mixed $v The value
65 */
66 function __set($k, $v) {
67 $this->data[$k] = $v;
68 }
69
70 /**
71 * Returns the data set
72 *
73 * @author Art <a.molcanovas@gmail.com>
74 * @return array
75 */
76 function toArray() {
77 return $this->data;
78 }
79
80 /**
81 * Returns the array iterator for our data
82 * @author Art <a.molcanovas@gmail.com>
83 *
84 * @return ArrayIterator
85 */
86 function getIterator() {
87 return new ArrayIterator($this->data);
88 }
89
90 /**
91 * Checks whether a offset exists
92 * @author Art <a.molcanovas@gmail.com>
93 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
94 *
95 * @param mixed $offset An offset to check for.
96 *
97 * @return boolean
98 */
99 function offsetExists($offset) {
100 return array_key_exists($offset, $this->data);
101 }
102
103 /**
104 * Gets an offset
105 * @author Art <a.molcanovas@gmail.com>
106 * @link http://php.net/manual/en/arrayaccess.offsetget.php
107 *
108 * @param mixed $offset The offset to retrieve.
109 *
110 * @return mixed
111 */
112 function offsetGet($offset) {
113 return get($this->data[$offset]);
114 }
115
116 /**
117 * Sets an offset
118 * @author Art <a.molcanovas@gmail.com>
119 * @link http://php.net/manual/en/arrayaccess.offsetset.php
120 *
121 * @param mixed $offset The offset to assign the value to.
122 * @param mixed $value The value to set.
123 */
124 function offsetSet($offset, $value) {
125 if ($offset === null) {
126 $this->data[] = $value;
127 } else {
128 $this->data[$offset] = $value;
129 }
130 }
131
132 /**
133 * Unsets an offset
134 * @author Art <a.molcanovas@gmail.com>
135 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
136 *
137 * @param mixed $offset The offset to unset.
138 */
139 function offsetUnset($offset) {
140 unset($this->data[$offset]);
141 }
142 }
143 }
144