Skip to content

Commit 188b476

Browse files
committed
🔨 improve on arrayfy() function to return a Generator or an array
Signed-off-by: otengkwame <[email protected]>
1 parent d01931f commit 188b476

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Core/helpers/webby_helper.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,32 +1168,43 @@ function object_array($object_array, $index)
11681168
{
11691169

11701170
/**
1171-
* Encode an array-object and retrieve
1172-
* as an array
1171+
* Convert an array-object and retrieve
1172+
* as an array or generator
11731173
*
1174-
* @param mixed
1175-
* @return array|bool
1174+
* @param object|array $object
1175+
* @param boolean $asGenerator
1176+
* @param int $threshold
1177+
* @return \Generator|array
11761178
*/
1177-
function arrayfy($object)
1179+
function arrayfy($object, $asGenerator = false, $threshold = 1000)
11781180
{
11791181

1182+
if ($asGenerator) {
1183+
$object = to_generator($object, $threshold);
1184+
}
1185+
1186+
if ($object instanceof \Generator) {
1187+
return $object;
1188+
}
1189+
11801190
if (is_object($object)) {
1181-
$json = json_encode($object);
1182-
return json_decode($json, true);
1191+
$array = [];
1192+
foreach ($object as $key => $value) {
1193+
$array[$key] = arrayfy($value);
1194+
}
1195+
return $array;
11831196
}
11841197

1185-
if ($object) {
1186-
$json = json_encode($object);
1187-
return json_decode($json, true);
1198+
if (!is_array($object)) {
1199+
return json_decode(json_encode($object), true);
11881200
}
11891201

11901202
if (is_array($object)) {
11911203
return $object;
11921204
}
11931205

11941206
throw new \Exception("Parameter must be an object or a supporting type", 1);
1195-
1196-
return false;
1207+
11971208
}
11981209
}
11991210

0 commit comments

Comments
 (0)