Skip to content

Commit

Permalink
[Data] Unable to collapse nested Collection since Arr::collapse() use…
Browse files Browse the repository at this point in the history
…s array_walk_recursive() #1118
  • Loading branch information
asika32764 committed Jun 17, 2024
1 parent 9b10c24 commit 5db17d5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
16 changes: 13 additions & 3 deletions packages/scalars/src/Concern/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public function flatten(string $delimiter = '.', int $depth = 0, ?string $prefix
}

/**
* collapse
*
* @param bool $keepKey
*
* @return static
Expand All @@ -86,7 +84,19 @@ public function flatten(string $delimiter = '.', int $depth = 0, ?string $prefix
*/
public function collapse(bool $keepKey = true): static
{
return $this->newInstance(Arr::collapse($this->dump(), $keepKey));
$result = [];

$this->walkRecursive(
static function ($v, $k) use ($keepKey, &$result) {
if ($keepKey) {
$result[$k] = $v;
} else {
$result[] = $v;
}
}
);

return $this->newInstance($result);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions packages/scalars/src/Concern/ArrayLoopTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,30 @@ public function walk(callable $callable, $userdata = null): ArrayLoopTrait|stati
}

/**
* walkRecursive
*
* @param callable $callable
* @param mixed $userdata
*
* @return static
*
* @since 3.5
*/
public function walkRecursive(callable $callable, $userdata = null): ArrayLoopTrait|static
public function walkRecursive(callable $callable, mixed $userdata = null): static
{
$new = clone $this;

array_walk_recursive($new->storage, $callable, $userdata);
$walk = static function (iterable &$items) use ($callable, $userdata, &$walk) {
foreach ($items as $k => &$v) {
if (is_array($v) || $v instanceof self) {
$walk($v, $k, $userdata);
} else {
$callable($v, $k, $userdata);
}

unset($v);
}
};

$walk($new->storage);

return $new;
}
Expand Down
28 changes: 28 additions & 0 deletions packages/scalars/test/ArrayObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Windwalker\Scalars\ArrayObject;
use Windwalker\Utilities\Iterator\PriorityQueue;

use function PHPUnit\Framework\assertEquals;
use function Windwalker\arr;

/**
Expand Down Expand Up @@ -338,6 +339,33 @@ public function testUnique(): void
);
}

public function testCollapse(): void
{
$items = new ArrayObject([
new ArrayObject([1, 2, 3]),
new ArrayObject([4, 5, 6]),
]);

$collapsed = $items->collapse(false);

assertEquals(
[1, 2, 3, 4, 5, 6],
$collapsed->dump()
);

$items = new ArrayObject([
new ArrayObject([1, 2, 3]),
new ArrayObject([4, 5, 6]),
]);

$collapsed = $items->collapse(true);

assertEquals(
[4, 5, 6],
$collapsed->dump()
);
}

public function testExplode(): void
{
$a = ArrayObject::explode(',', '1,2,3');
Expand Down

0 comments on commit 5db17d5

Please sign in to comment.