@@ -12,7 +12,7 @@ class UnderscoreBase implements \ArrayAccess, \Countable, \IteratorAggregate, \J
12
12
/**
13
13
* Constructor.
14
14
*
15
- * @param array|mixed $data.
15
+ * @param array|mixed $data Array or array like or array convertible .
16
16
*/
17
17
public function __construct ($ data = [])
18
18
{
@@ -38,11 +38,12 @@ public function get($index = null)
38
38
/**
39
39
* Get data as array.
40
40
*
41
- * @param mixed $data
41
+ * @param mixed $data Arbitrary data.
42
+ * @param bool $cast Force casting to array!
42
43
*
43
44
* @return array
44
45
*/
45
- public function asArray ($ data )
46
+ public function asArray ($ data, $ cast = true )
46
47
{
47
48
if (\is_array ($ data )) {
48
49
return $ data ;
@@ -66,7 +67,7 @@ public function asArray($data)
66
67
return $ data ->toArray ();
67
68
}
68
69
69
- return (array ) $ data ;
70
+ return $ cast ? (array ) $ data : $ data ;
70
71
}
71
72
72
73
/**
@@ -76,12 +77,12 @@ public function asArray($data)
76
77
*/
77
78
public function toArray ()
78
79
{
79
- return array_map (function ($ value ) {
80
+ return \ array_map (function ($ value ) {
80
81
if (\is_scalar ($ value )) {
81
82
return $ value ;
82
83
}
83
84
84
- return $ this ->asArray ($ value );
85
+ return $ this ->asArray ($ value, false );
85
86
}, $ this ->data );
86
87
}
87
88
@@ -143,68 +144,97 @@ protected function valueFn($fn)
143
144
144
145
$ value = \array_column ([$ value ], $ fn );
145
146
146
- return $ value ? $ value [0 ] : null ;
147
+ return $ value ? $ value [0 ] : null ;
147
148
};
148
149
}
149
150
150
151
/**
151
- * {@inheritdoc}
152
+ * Checks if offset/index exists.
153
+ *
154
+ * @param string|int $index
155
+ *
156
+ * @return bool
152
157
*/
153
158
public function offsetExists ($ index )
154
159
{
155
160
return \array_key_exists ($ index , $ this ->data );
156
161
}
157
162
158
163
/**
159
- * {@inheritdoc}
164
+ * Gets the value at given offset/index.
165
+ *
166
+ * @return mixed
160
167
*/
161
168
public function offsetGet ($ index )
162
169
{
163
170
return $ this ->data [$ index ];
164
171
}
165
172
166
173
/**
167
- * {@inheritdoc}
174
+ * Sets a new value at the given offset/index.
175
+ *
176
+ * @param string|int $index
177
+ * @param mixed $value
178
+ *
179
+ * @return void
168
180
*/
169
181
public function offsetSet ($ index , $ value )
170
182
{
171
183
$ this ->data [$ index ] = $ value ;
172
184
}
173
185
174
186
/**
175
- * {@inheritdoc}
187
+ * Unsets/removes the value at given index.
188
+ *
189
+ * @param string|int $index
176
190
*/
177
191
public function offsetUnset ($ index )
178
192
{
179
193
unset($ this ->data [$ index ]);
180
194
}
181
195
182
196
/**
183
- * {@inheritdoc}
197
+ * Gets the count of items.
198
+ *
199
+ * @return int
184
200
*/
185
201
public function count ()
186
202
{
187
203
return \count ($ this ->data );
188
204
}
189
205
190
206
/**
191
- * {@inheritdoc}
207
+ * Alias of count().
208
+ */
209
+ public function size ()
210
+ {
211
+ return $ this ->count ();
212
+ }
213
+
214
+ /**
215
+ * Gets the iterator for looping.
216
+ *
217
+ * @return \ArrayIterator
192
218
*/
193
219
public function getIterator ()
194
220
{
195
221
return new \ArrayIterator ($ this ->data );
196
222
}
197
223
198
224
/**
199
- * {@inheritdoc}
225
+ * Gets the data for json serialization.
226
+ *
227
+ * @return array
200
228
*/
201
229
public function jsonSerialize ()
202
230
{
203
- return $ this ->data ;
231
+ return $ this ->toArray () ;
204
232
}
205
233
206
234
/**
207
- * {@inheritdoc}
235
+ * Stringify the underscore instance.
236
+ *
237
+ * @return string Json encoded data.
208
238
*/
209
239
public function __toString ()
210
240
{
@@ -218,7 +248,7 @@ public function __toString()
218
248
*/
219
249
public function now ()
220
250
{
221
- return microtime (1 ) * 1000 ;
251
+ return \ microtime (1 ) * 1000 ;
222
252
}
223
253
224
254
/**
0 commit comments