diff --git a/src/Fieldtypes/Arr.php b/src/Fieldtypes/Arr.php index c269f6de50..2b21c19645 100644 --- a/src/Fieldtypes/Arr.php +++ b/src/Fieldtypes/Arr.php @@ -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; } diff --git a/tests/Fieldtypes/ArrayTest.php b/tests/Fieldtypes/ArrayTest.php index b886771517..5e27418904 100644 --- a/tests/Fieldtypes/ArrayTest.php +++ b/tests/Fieldtypes/ArrayTest.php @@ -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, [ @@ -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, [ @@ -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, [ @@ -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, [ @@ -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, [ @@ -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, [ @@ -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, [ @@ -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'], + ], + ], ]; }