Skip to content

Commit

Permalink
[5.x] Filter out empty values in keyed array field (#11138)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Nov 25, 2024
1 parent f447d91 commit 099f34f
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Fieldtypes/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ public function process($data)

if ($this->config('expand')) {
return collect($data)
->when($this->isKeyed(), fn ($items) => $items->filter())
->map(fn ($value, $key) => ['key' => $key, 'value' => $value])
->values()
->all();
}

if ($this->isKeyed()) {
return collect($data)->filter()->all();
}

return $data;
}

Expand Down
136 changes: 136 additions & 0 deletions tests/Fieldtypes/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,23 @@ public static function keyedProcessProvider()
'side' => 'fries',
],
],
'associative array options, associative array value, with nulls' => [
false,
[
'food' => 'Food',
'drink' => 'Drink',
'side' => 'Side',
],
[
'food' => 'burger',
'drink' => 'coke',
'side' => null,
],
[
'food' => 'burger',
'drink' => 'coke',
],
],
'associative array options, associative array value with expanded setting' => [
true,
[
Expand All @@ -447,6 +464,23 @@ public static function keyedProcessProvider()
['key' => 'side', 'value' => 'fries'],
],
],
'associative array options, associative array value with expanded setting, with nulls' => [
true,
[
'food' => 'Food',
'drink' => 'Drink',
'side' => 'Side',
],
[
'food' => 'burger',
'drink' => 'coke',
'side' => null,
],
[
['key' => 'food', 'value' => 'burger'],
['key' => 'drink', 'value' => 'coke'],
],
],
'multidimensional array options, associative array value' => [
false,
[
Expand All @@ -465,6 +499,23 @@ public static function keyedProcessProvider()
'side' => 'fries',
],
],
'multidimensional array options, associative array value, with nulls' => [
false,
[
['key' => 'food', 'value' => 'Food'],
['key' => 'drink', 'value' => 'Drink'],
['key' => 'side', 'value' => 'Side'],
],
[
'food' => 'burger',
'drink' => 'coke',
'side' => null,
],
[
'food' => 'burger',
'drink' => 'coke',
],
],
'multidimensional array options, associative array value with expanded setting' => [
true,
[
Expand All @@ -483,6 +534,23 @@ public static function keyedProcessProvider()
['key' => 'side', 'value' => 'fries'],
],
],
'multidimensional array options, associative array value with expanded setting, with nulls' => [
true,
[
['key' => 'food', 'value' => 'Food'],
['key' => 'drink', 'value' => 'Drink'],
['key' => 'side', 'value' => 'Side'],
],
[
'food' => 'burger',
'drink' => 'coke',
'side' => null,
],
[
['key' => 'food', 'value' => 'burger'],
['key' => 'drink', 'value' => 'coke'],
],
],
'multidimensional array with numbers' => [
false,
[
Expand All @@ -501,6 +569,23 @@ public static function keyedProcessProvider()
2 => 'more',
],
],
'multidimensional array with numbers, with nulls' => [
false,
[
['key' => 0, 'value' => 'Zero'],
['key' => 1, 'value' => 'One'],
['key' => 2, 'value' => 'Two'],
],
[
0 => 'none',
1 => null,
2 => 'more',
],
[
0 => 'none',
2 => 'more',
],
],
'multidimensional array with numbers with expanded setting' => [
true,
[
Expand All @@ -519,6 +604,23 @@ public static function keyedProcessProvider()
['key' => 2, 'value' => 'more'],
],
],
'multidimensional array with numbers with expanded setting, with nulls' => [
true,
[
['key' => 0, 'value' => 'Zero'],
['key' => 1, 'value' => 'One'],
['key' => 2, 'value' => 'Two'],
],
[
0 => 'none',
1 => null,
2 => 'more',
],
[
['key' => 0, 'value' => 'none'],
['key' => 2, 'value' => 'more'],
],
],
'multidimensional array with non-sequential numbers' => [
false,
[
Expand All @@ -537,6 +639,23 @@ public static function keyedProcessProvider()
0 => 'none',
],
],
'multidimensional array with non-sequential numbers, with nulls' => [
false,
[
['key' => 0, 'value' => 'Zero'],
['key' => 1, 'value' => 'One'],
['key' => 2, 'value' => 'Two'],
],
[
2 => 'some',
1 => null,
0 => 'none',
],
[
2 => 'some',
0 => 'none',
],
],
'multidimensional array with non-sequential numbers with expanded setting' => [
true,
[
Expand All @@ -555,6 +674,23 @@ public static function keyedProcessProvider()
['key' => 0, 'value' => 'none'],
],
],
'multidimensional array with non-sequential numbers with expanded setting, with nulls' => [
true,
[
['key' => 0, 'value' => 'Zero'],
['key' => 1, 'value' => 'One'],
['key' => 2, 'value' => 'Two'],
],
[
2 => 'some',
1 => null,
0 => 'none',
],
[
['key' => 2, 'value' => 'some'],
['key' => 0, 'value' => 'none'],
],
],
];
}

Expand Down

0 comments on commit 099f34f

Please sign in to comment.