From e932c077094aabde6d18d9cee1f81d7ac246cc15 Mon Sep 17 00:00:00 2001 From: Saya Date: Tue, 7 Dec 2021 19:17:13 +0800 Subject: [PATCH 1/5] Update Arr.php filterNulls --- src/Illuminate/Collections/Arr.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 1ce093079f64..ba49ae4bd765 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -172,6 +172,19 @@ public static function exists($array, $key) return array_key_exists($key, $array); } + /** + * Filter the array for nulls. + * + * @param array $array + * @return array + */ + public static function filterNulls($array) + { + return array_filter($array, function ($value) { + return !is_null($value); + }); + } + /** * Return the first element in an array passing a given truth test. * From 9a62d0a077c5a99750af7a3b18a76cb9b3e78888 Mon Sep 17 00:00:00 2001 From: Saya Date: Tue, 7 Dec 2021 19:23:16 +0800 Subject: [PATCH 2/5] Update SupportArrTest.php --- tests/Support/SupportArrTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index e34f469c848d..7d223aef2263 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -163,6 +163,12 @@ public function testExists() $this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b')); } + public function testFilterNulls() + { + $array = Arr::filterNulls([null, 0, false, '', null, []]); + $this->assertEquals([0, false, '', []], $array); + } + public function testFirst() { $array = [100, 200, 300]; From 6c032c7c11a38df9bc5858df08f02de89f5e0c49 Mon Sep 17 00:00:00 2001 From: Saya Date: Tue, 7 Dec 2021 19:37:34 +0800 Subject: [PATCH 3/5] Update SupportArrTest.php --- tests/Support/SupportArrTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 7d223aef2263..1c05faa32e99 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -165,7 +165,7 @@ public function testExists() public function testFilterNulls() { - $array = Arr::filterNulls([null, 0, false, '', null, []]); + $array = array_values(Arr::filterNulls([null, 0, false, '', null, []])); $this->assertEquals([0, false, '', []], $array); } From 281534693969f02c5e5b74f14c7418d12f3386d1 Mon Sep 17 00:00:00 2001 From: Saya Date: Tue, 7 Dec 2021 19:41:24 +0800 Subject: [PATCH 4/5] StyleCI --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index ba49ae4bd765..9652b30340a3 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -181,7 +181,7 @@ public static function exists($array, $key) public static function filterNulls($array) { return array_filter($array, function ($value) { - return !is_null($value); + return ! is_null($value); }); } From a211f37ab33ad05c846f6c3b01a87d6bbdf33cfa Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 7 Dec 2021 08:42:48 -0600 Subject: [PATCH 5/5] formatting - rename mehtod --- src/Illuminate/Collections/Arr.php | 24 +++++++++++------------- tests/Support/SupportArrTest.php | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 9652b30340a3..702bde2524d5 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -172,19 +172,6 @@ public static function exists($array, $key) return array_key_exists($key, $array); } - /** - * Filter the array for nulls. - * - * @param array $array - * @return array - */ - public static function filterNulls($array) - { - return array_filter($array, function ($value) { - return ! is_null($value); - }); - } - /** * Return the first element in an array passing a given truth test. * @@ -730,6 +717,17 @@ public static function where($array, callable $callback) return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); } + /** + * Filter items where the value is not null. + * + * @param array $array + * @return array + */ + public static function whereNotNull($array) + { + return static::where($array, fn ($x) => ! is_null($x)); + } + /** * If the given value is not an array and not null, wrap it in one. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 1c05faa32e99..5d405b1f9332 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -163,9 +163,9 @@ public function testExists() $this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b')); } - public function testFilterNulls() + public function testWhereNotNull() { - $array = array_values(Arr::filterNulls([null, 0, false, '', null, []])); + $array = array_values(Arr::whereNotNull([null, 0, false, '', null, []])); $this->assertEquals([0, false, '', []], $array); }