Skip to content

Commit a662f66

Browse files
committed
fix+doc: minor fixes, add docs
1 parent c4540b1 commit a662f66

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

src/UnderscoreBase.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class UnderscoreBase implements \ArrayAccess, \Countable, \IteratorAggregate, \J
1212
/**
1313
* Constructor.
1414
*
15-
* @param array|mixed $data.
15+
* @param array|mixed $data Array or array like or array convertible.
1616
*/
1717
public function __construct($data = [])
1818
{
@@ -38,11 +38,12 @@ public function get($index = null)
3838
/**
3939
* Get data as array.
4040
*
41-
* @param mixed $data
41+
* @param mixed $data Arbitrary data.
42+
* @param bool $cast Force casting to array!
4243
*
4344
* @return array
4445
*/
45-
public function asArray($data)
46+
public function asArray($data, $cast = true)
4647
{
4748
if (\is_array($data)) {
4849
return $data;
@@ -66,7 +67,7 @@ public function asArray($data)
6667
return $data->toArray();
6768
}
6869

69-
return (array) $data;
70+
return $cast ? (array) $data : $data;
7071
}
7172

7273
/**
@@ -76,12 +77,12 @@ public function asArray($data)
7677
*/
7778
public function toArray()
7879
{
79-
return array_map(function ($value) {
80+
return \array_map(function ($value) {
8081
if (\is_scalar($value)) {
8182
return $value;
8283
}
8384

84-
return $this->asArray($value);
85+
return $this->asArray($value, false);
8586
}, $this->data);
8687
}
8788

@@ -143,68 +144,97 @@ protected function valueFn($fn)
143144

144145
$value = \array_column([$value], $fn);
145146

146-
return $value ? $value[0] : null;
147+
return $value ? $value[0] : null;
147148
};
148149
}
149150

150151
/**
151-
* {@inheritdoc}
152+
* Checks if offset/index exists.
153+
*
154+
* @param string|int $index
155+
*
156+
* @return bool
152157
*/
153158
public function offsetExists($index)
154159
{
155160
return \array_key_exists($index, $this->data);
156161
}
157162

158163
/**
159-
* {@inheritdoc}
164+
* Gets the value at given offset/index.
165+
*
166+
* @return mixed
160167
*/
161168
public function offsetGet($index)
162169
{
163170
return $this->data[$index];
164171
}
165172

166173
/**
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
168180
*/
169181
public function offsetSet($index, $value)
170182
{
171183
$this->data[$index] = $value;
172184
}
173185

174186
/**
175-
* {@inheritdoc}
187+
* Unsets/removes the value at given index.
188+
*
189+
* @param string|int $index
176190
*/
177191
public function offsetUnset($index)
178192
{
179193
unset($this->data[$index]);
180194
}
181195

182196
/**
183-
* {@inheritdoc}
197+
* Gets the count of items.
198+
*
199+
* @return int
184200
*/
185201
public function count()
186202
{
187203
return \count($this->data);
188204
}
189205

190206
/**
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
192218
*/
193219
public function getIterator()
194220
{
195221
return new \ArrayIterator($this->data);
196222
}
197223

198224
/**
199-
* {@inheritdoc}
225+
* Gets the data for json serialization.
226+
*
227+
* @return array
200228
*/
201229
public function jsonSerialize()
202230
{
203-
return $this->data;
231+
return $this->toArray();
204232
}
205233

206234
/**
207-
* {@inheritdoc}
235+
* Stringify the underscore instance.
236+
*
237+
* @return string Json encoded data.
208238
*/
209239
public function __toString()
210240
{
@@ -218,7 +248,7 @@ public function __toString()
218248
*/
219249
public function now()
220250
{
221-
return microtime(1) * 1000;
251+
return \microtime(1) * 1000;
222252
}
223253

224254
/**

0 commit comments

Comments
 (0)