Skip to content

Commit

Permalink
refactor: RecursiveToArray
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pulli committed Sep 22, 2023
1 parent c618533 commit aac785e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Macros/RecursiveToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

namespace Pulli\CollectionMacros\Macros;

use Closure;
use Illuminate\Support\Collection;

class RecursiveToArray
{
public function __invoke(): \Closure
public function __invoke(): Closure
{
return function(array $ary): array {
return Collection::mapToCollection($ary, true)->toArray();
$closure = function (&$ary) {
if (is_array($ary)) {
$ary = Collection::recursiveToArray($ary);
}

if (is_object($ary) && method_exists($ary, 'toArray')) {
$ary = Collection::recursiveToArray($ary->toArray());
}
};

array_walk($ary, $closure);

return $ary;
};
}
}

0 comments on commit aac785e

Please sign in to comment.