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 * Some smarter array functions. This is a very work-in-progress class.
16 * @author Art <a.molcanovas@gmail.com>
17 */
18 class SmartObj 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 * Initialises our smart array-based object
47 * @author Art <a.molcanovas@gmail.com>
48 *
49 * @param array $initial The initial array to set
50 *
51 * @return SmartObj
52 */
53 static function smartObj(array $initial = []) {
54 return new SmartObj($initial);
55 }
56
57 /**
58 * Returns an array value
59 *
60 * @author Art <a.molcanovas@gmail.com>
61 *
62 * @param string $k The value's key
63 *
64 * @return mixed
65 */
66 function __get($k) {
67 return get($this->data[$k]);
68 }
69
70 /**
71 * Sets an array value
72 *
73 * @author Art <a.molcanovas@gmail.com>
74 *
75 * @param string $k The key
76 * @param mixed $v The value
77 */
78 function __set($k, $v) {
79 $this->data[$k] = $v;
80 }
81
82 /**
83 * Returns the data set
84 *
85 * @author Art <a.molcanovas@gmail.com>
86 * @return array
87 */
88 function toArray() {
89 return $this->data;
90 }
91
92 /**
93 * Removes all the duplicate values from an array
94 * @author Art <a.molcanovas@gmail.com>
95 *
96 * @return SmartObj
97 */
98 function uniqueRecursive() {
99 $this->uniqueRecursiveInternal($this->data);
100
101 return $this;
102 }
103
104 /**
105 * Internal handler for uniquefyRecursively
106 * @author Art <a.molcanovas@gmail.com>
107 *
108 * @param array $curr Reference to the currently processed step
109 */
110 protected function uniqueRecursiveInternal(array &$curr) {
111 foreach ($curr as &$v) {
112 if (is_array($v)) {
113 $v = array_map('unserialize', array_unique(array_map('serialize', $v)));
114 $this->uniqueRecursiveInternal($v);
115 }
116 }
117 }
118
119 /**
120 * Returns the array iterator for our data
121 * @author Art <a.molcanovas@gmail.com>
122 *
123 * @return ArrayIterator
124 */
125 function getIterator() {
126 return new ArrayIterator($this->data);
127 }
128
129 /**
130 * Checks whether a offset exists
131 * @author Art <a.molcanovas@gmail.com>
132 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
133 *
134 * @param mixed $offset An offset to check for.
135 *
136 * @return boolean
137 */
138 function offsetExists($offset) {
139 return array_key_exists($offset, $this->data);
140 }
141
142 /**
143 * Gets an offset
144 * @author Art <a.molcanovas@gmail.com>
145 * @link http://php.net/manual/en/arrayaccess.offsetget.php
146 *
147 * @param mixed $offset The offset to retrieve.
148 *
149 * @return mixed
150 */
151 function offsetGet($offset) {
152 return get($this->data[$offset]);
153 }
154
155 /**
156 * Sets an offset
157 * @author Art <a.molcanovas@gmail.com>
158 * @link http://php.net/manual/en/arrayaccess.offsetset.php
159 *
160 * @param mixed $offset The offset to assign the value to.
161 * @param mixed $value The value to set.
162 */
163 function offsetSet($offset, $value) {
164 if ($offset === null) {
165 $this->data[] = $value;
166 } else {
167 $this->data[$offset] = $value;
168 }
169 }
170
171 /**
172 * Unsets an offset
173 * @author Art <a.molcanovas@gmail.com>
174 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
175 *
176 * @param mixed $offset The offset to unset.
177 */
178 function offsetUnset($offset) {
179 unset($this->data[$offset]);
180 }
181 }
182 }
183