Skip to content

Commit c8c8189

Browse files
committed
Remove Arr::isEmpty() but make sure is_empty still works
1 parent 4b6fd3d commit c8c8189

4 files changed

Lines changed: 48 additions & 44 deletions

File tree

src/Modifiers/CoreModifiers.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,17 @@ public function isBlank($value)
849849
*/
850850
public function isEmpty($value)
851851
{
852-
return Arr::isEmpty($value);
852+
if (is_array($value)) {
853+
foreach ($value as $subvalue) {
854+
if (!$this->isEmpty($subvalue)) {
855+
return false;
856+
}
857+
}
858+
} elseif (!empty($value) || $value !== '') {
859+
return false;
860+
}
861+
862+
return true;
853863
}
854864

855865
/**

src/Support/Arr.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,6 @@ public static function explodeOptions($string, $keyed = false)
144144
return $options;
145145
}
146146

147-
/**
148-
* Checks if $value is an "empty" array
149-
*
150-
* It might have keys, but if those keys are all empty strings, it's empty.
151-
*
152-
* @param mixed $value Value to check
153-
* @return bool
154-
*/
155-
public static function isEmpty($value)
156-
{
157-
if (is_array($value)) {
158-
foreach ($value as $subvalue) {
159-
if (!self::isEmpty($subvalue)) {
160-
return false;
161-
}
162-
}
163-
} elseif (!empty($value) || $value !== '') {
164-
return false;
165-
}
166-
167-
return true;
168-
}
169-
170147
/**
171148
* Normalize arguments
172149
*

tests/Modifiers/IsEmptyTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Tests\Modifiers;
4+
5+
use Statamic\Facades\Antlers;
6+
use Statamic\Support\Str;
7+
use Tests\TestCase;
8+
9+
class IsEmptyTest extends TestCase
10+
{
11+
/** @test */
12+
function it_checks_if_its_empty()
13+
{
14+
$this->assertTrue($this->parse('')); // empty string is empty
15+
$this->assertTrue($this->parse([])); // empty array is empty
16+
17+
$this->assertFalse($this->parse(['foo' => 'bar'])); // definitely not empty
18+
19+
$this->assertTrue($this->parse(['foo' => ''])); // just consists of empty strings
20+
$this->assertTrue($this->parse(['foo' => '', 'bar' => '']));
21+
22+
$this->assertFalse($this->parse(null)); // nulls are not empty
23+
$this->assertFalse($this->parse(['foo' => null])); // array of nulls are not empty
24+
$this->assertFalse($this->parse(['foo' => '', 'bar' => null]));
25+
26+
$this->assertTrue($this->parse(['foo' => []])); // recursion
27+
$this->assertTrue($this->parse(['foo' => ['bar' => []]]));
28+
$this->assertTrue($this->parse(['foo' => ['bar' => ['baz' => '']]]));
29+
$this->assertFalse($this->parse(['foo' => ['bar' => ['baz' => 'qux']]]));
30+
$this->assertFalse($this->parse(['foo' => ['bar' => ['baz' => null]]]));
31+
}
32+
33+
function parse($arr)
34+
{
35+
return Str::toBool(Antlers::parse('{{ if arr|is_empty }}true{{ else }}false{{ /if }}', ['arr' => $arr]));
36+
}
37+
}

tests/Support/ArrTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,4 @@ function it_gets_the_first_non_null_value()
9090
'bar' => 'two',
9191
], ['foo', 'bar']));
9292
}
93-
94-
/** @test */
95-
function it_checks_if_its_empty()
96-
{
97-
$this->assertTrue(Arr::isEmpty([])); // completely empty array
98-
99-
$this->assertFalse(Arr::isEmpty(['foo' => 'bar'])); // definitely not empty
100-
101-
$this->assertTrue(Arr::isEmpty(['foo' => ''])); // just consists of empty strings
102-
$this->assertTrue(Arr::isEmpty(['foo' => '', 'bar' => '']));
103-
104-
$this->assertFalse(Arr::isEmpty(['foo' => null])); // nulls are not empty
105-
$this->assertFalse(Arr::isEmpty(['foo' => '', 'bar' => null]));
106-
107-
$this->assertTrue(Arr::isEmpty(['foo' => []])); // recursion
108-
$this->assertTrue(Arr::isEmpty(['foo' => ['bar' => []]]));
109-
$this->assertTrue(Arr::isEmpty(['foo' => ['bar' => ['baz' => '']]]));
110-
$this->assertFalse(Arr::isEmpty(['foo' => ['bar' => ['baz' => 'qux']]]));
111-
$this->assertFalse(Arr::isEmpty(['foo' => ['bar' => ['baz' => null]]]));
112-
}
11393
}

0 commit comments

Comments
 (0)