From c8c8189248759b4610142eba2711af6d69e112a9 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 23 Jan 2020 11:47:25 -0500 Subject: [PATCH] Remove Arr::isEmpty() but make sure is_empty still works --- src/Modifiers/CoreModifiers.php | 12 ++++++++++- src/Support/Arr.php | 23 -------------------- tests/Modifiers/IsEmptyTest.php | 37 +++++++++++++++++++++++++++++++++ tests/Support/ArrTest.php | 20 ------------------ 4 files changed, 48 insertions(+), 44 deletions(-) create mode 100644 tests/Modifiers/IsEmptyTest.php diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 0c7a25e077..ef457f6bce 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -849,7 +849,17 @@ public function isBlank($value) */ public function isEmpty($value) { - return Arr::isEmpty($value); + if (is_array($value)) { + foreach ($value as $subvalue) { + if (!$this->isEmpty($subvalue)) { + return false; + } + } + } elseif (!empty($value) || $value !== '') { + return false; + } + + return true; } /** diff --git a/src/Support/Arr.php b/src/Support/Arr.php index 3104a97802..c2aac0425e 100644 --- a/src/Support/Arr.php +++ b/src/Support/Arr.php @@ -144,29 +144,6 @@ public static function explodeOptions($string, $keyed = false) return $options; } - /** - * Checks if $value is an "empty" array - * - * It might have keys, but if those keys are all empty strings, it's empty. - * - * @param mixed $value Value to check - * @return bool - */ - public static function isEmpty($value) - { - if (is_array($value)) { - foreach ($value as $subvalue) { - if (!self::isEmpty($subvalue)) { - return false; - } - } - } elseif (!empty($value) || $value !== '') { - return false; - } - - return true; - } - /** * Normalize arguments * diff --git a/tests/Modifiers/IsEmptyTest.php b/tests/Modifiers/IsEmptyTest.php new file mode 100644 index 0000000000..f4d00ed3e0 --- /dev/null +++ b/tests/Modifiers/IsEmptyTest.php @@ -0,0 +1,37 @@ +assertTrue($this->parse('')); // empty string is empty + $this->assertTrue($this->parse([])); // empty array is empty + + $this->assertFalse($this->parse(['foo' => 'bar'])); // definitely not empty + + $this->assertTrue($this->parse(['foo' => ''])); // just consists of empty strings + $this->assertTrue($this->parse(['foo' => '', 'bar' => ''])); + + $this->assertFalse($this->parse(null)); // nulls are not empty + $this->assertFalse($this->parse(['foo' => null])); // array of nulls are not empty + $this->assertFalse($this->parse(['foo' => '', 'bar' => null])); + + $this->assertTrue($this->parse(['foo' => []])); // recursion + $this->assertTrue($this->parse(['foo' => ['bar' => []]])); + $this->assertTrue($this->parse(['foo' => ['bar' => ['baz' => '']]])); + $this->assertFalse($this->parse(['foo' => ['bar' => ['baz' => 'qux']]])); + $this->assertFalse($this->parse(['foo' => ['bar' => ['baz' => null]]])); + } + + function parse($arr) + { + return Str::toBool(Antlers::parse('{{ if arr|is_empty }}true{{ else }}false{{ /if }}', ['arr' => $arr])); + } +} diff --git a/tests/Support/ArrTest.php b/tests/Support/ArrTest.php index 79ecfddf0a..f77625ba21 100644 --- a/tests/Support/ArrTest.php +++ b/tests/Support/ArrTest.php @@ -90,24 +90,4 @@ function it_gets_the_first_non_null_value() 'bar' => 'two', ], ['foo', 'bar'])); } - - /** @test */ - function it_checks_if_its_empty() - { - $this->assertTrue(Arr::isEmpty([])); // completely empty array - - $this->assertFalse(Arr::isEmpty(['foo' => 'bar'])); // definitely not empty - - $this->assertTrue(Arr::isEmpty(['foo' => ''])); // just consists of empty strings - $this->assertTrue(Arr::isEmpty(['foo' => '', 'bar' => ''])); - - $this->assertFalse(Arr::isEmpty(['foo' => null])); // nulls are not empty - $this->assertFalse(Arr::isEmpty(['foo' => '', 'bar' => null])); - - $this->assertTrue(Arr::isEmpty(['foo' => []])); // recursion - $this->assertTrue(Arr::isEmpty(['foo' => ['bar' => []]])); - $this->assertTrue(Arr::isEmpty(['foo' => ['bar' => ['baz' => '']]])); - $this->assertFalse(Arr::isEmpty(['foo' => ['bar' => ['baz' => 'qux']]])); - $this->assertFalse(Arr::isEmpty(['foo' => ['bar' => ['baz' => null]]])); - } }