Skip to content

Commit

Permalink
Remove Arr::isEmpty() but make sure is_empty still works
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Jan 23, 2020
1 parent 4b6fd3d commit c8c8189
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 44 deletions.
12 changes: 11 additions & 1 deletion src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Modifiers/IsEmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\Modifiers;

use Statamic\Facades\Antlers;
use Statamic\Support\Str;
use Tests\TestCase;

class IsEmptyTest extends TestCase
{
/** @test */
function it_checks_if_its_empty()
{
$this->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]));
}
}
20 changes: 0 additions & 20 deletions tests/Support/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]));
}
}

0 comments on commit c8c8189

Please sign in to comment.