Skip to content

Commit

Permalink
[10.x] Introducing isEmpty and isNotEmpty to `ComponentAttributeB…
Browse files Browse the repository at this point in the history
…ag` (#49408)

* code

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
devajmeireles and taylorotwell committed Dec 17, 2023
1 parent ddade2c commit 56cf201
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ protected function resolveAppendableAttributeDefault($attributeDefaults, $key, $
return $value;
}

/**
* Determine if the attribute bag is empty.
*
* @return bool
*/
public function isEmpty()
{
return trim((string) $this) === '';
}

/**
* Determine if the attribute bag is not empty.
*
* @return bool
*/
public function isNotEmpty()
{
return ! $this->isEmpty();
}

/**
* Get all of the raw attributes.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,18 @@ public function testAttributeExistence()
$this->assertFalse((bool) $bag->has('name', 'class'));
$this->assertTrue((bool) $bag->missing('class'));
}

public function testAttributeIsEmpty()
{
$bag = new ComponentAttributeBag([]);

$this->assertTrue((bool) $bag->isEmpty());
}

public function testAttributeIsNotEmpty()
{
$bag = new ComponentAttributeBag(['name' => 'test']);

$this->assertTrue((bool) $bag->isNotEmpty());
}
}

0 comments on commit 56cf201

Please sign in to comment.