Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add depth parameter to flatten modifier #10187

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,13 @@ public function first($value, $params)
}

/**
* Flattens a multi-dimensional collection into a single dimension.
* Flattens a multi-dimensional collection into a single or arbitrary dimension.
*
* @return array
*/
public function flatten($value)
public function flatten($value, $params)
{
return collect($value)->flatten()->toArray();
return collect($value)->flatten(Arr::get($params, 0, INF))->toArray();
}

/**
Expand Down
84 changes: 62 additions & 22 deletions tests/Modifiers/FlattenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,80 @@
use Statamic\Modifiers\Modify;
use Tests\TestCase;

/**
* @todo Add test with numeric indices
*/
class FlattenTest extends TestCase
{
/** @test */
public function it_flattens_a_multidimensional_array(): void
/**
* @test
*
* @dataProvider flattenProvider
*/
public function it_flattens_an_array($depth, $expected): void
{
$input = [
'ingredients' => [
'spices' => ['garlic', 'cumin', 'ginger', 'turmeric', 'paprika', 'curry powder'],
'vegetables' => ['tomatoes', 'onion'],
'meat' => ['chicken'],
[
'foo',
[
'bar',
[
'baz',
],
],
],
'zap',
];

$expected = [
'garlic',
'cumin',
'ginger',
'turmeric',
'paprika',
'curry powder',
'tomatoes',
'onion',
'chicken',
$modified = $this->modify($input, $depth);
$this->assertEquals($expected, $modified);
}

public static function flattenProvider()
{
return [
'depth null' => [null, ['foo', 'bar', 'baz', 'zap']],
'depth 1' => [1, ['foo', ['bar', ['baz']], 'zap']],
'depth 2' => [2, ['foo', 'bar', ['baz'], 'zap']],
'depth 3' => [3, ['foo', 'bar', 'baz', 'zap']],
'depth 4' => [4, ['foo', 'bar', 'baz', 'zap']],
];
}

/**
* @test
*
* @dataProvider flattenWithKeysProvider
*/
public function it_flattens_an_array_with_keys($depth, $expected): void
{
$input = [
[
'foo',
'alfa' => [
'bar',
'bravo' => [
'baz',
],
],
],
'zap',
];

$modified = $this->modify($input);
$modified = $this->modify($input, $depth);
$this->assertEquals($expected, $modified);
}

private function modify(array $value)
public static function flattenWithKeysProvider()
{
return [
'depth null' => [null, ['foo', 'bar', 'baz', 'zap']],
'depth 1' => [1, ['foo', ['bar', 'bravo' => ['baz']], 'zap']],
'depth 2' => [2, ['foo', 'bar', ['baz'], 'zap']],
'depth 3' => [3, ['foo', 'bar', 'baz', 'zap']],
'depth 4' => [4, ['foo', 'bar', 'baz', 'zap']],
];
}

private function modify(array $value, $param = null)
{
return Modify::value($value)->flatten()->fetch();
return Modify::value($value)->flatten($param)->fetch();
}
}
Loading