Skip to content

Commit d833bb9

Browse files
committed
✨ add to_generator() function to convert arrays to generators
Signed-off-by: otengkwame <[email protected]>
1 parent b2931ef commit d833bb9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Core/helpers/webby_helper.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,39 @@ function compare_json($first_object, $second_object)
12881288
}
12891289
}
12901290

1291+
if ( ! function_exists('to_generator'))
1292+
{
1293+
/**
1294+
* Convert an array or object
1295+
* to a generator
1296+
*
1297+
* @param mixed $data
1298+
* @param integer $threshold
1299+
* @return \Generator|array|object
1300+
*/
1301+
function to_generator($data, $threshold = 1000)
1302+
{
1303+
$count = 0;
1304+
1305+
if (is_array($data)) {
1306+
$count = count($data);
1307+
}
1308+
1309+
if (is_object($data)) {
1310+
$count = count(get_object_vars($data));
1311+
}
1312+
1313+
if ($count > $threshold) {
1314+
foreach ($data as $key => $value) {
1315+
yield $key => $value;
1316+
}
1317+
} else {
1318+
return $data;
1319+
}
1320+
1321+
}
1322+
}
1323+
12911324
/* ------------------------------- Date | Time | Format Functions ---------------------------------*/
12921325

12931326

0 commit comments

Comments
 (0)