From 56cf2018490ea0193f50bf2bff59654a46cf52d8 Mon Sep 17 00:00:00 2001 From: AJ <60591772+devajmeireles@users.noreply.github.com> Date: Sun, 17 Dec 2023 12:34:19 -0300 Subject: [PATCH] [10.x] Introducing `isEmpty` and `isNotEmpty` to `ComponentAttributeBag` (#49408) * code * formatting --------- Co-authored-by: Taylor Otwell --- src/Illuminate/View/ComponentAttributeBag.php | 20 +++++++++++++++++++ tests/View/ViewComponentAttributeBagTest.php | 14 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Illuminate/View/ComponentAttributeBag.php b/src/Illuminate/View/ComponentAttributeBag.php index 812b9829551a..3c3d1a27dbff 100644 --- a/src/Illuminate/View/ComponentAttributeBag.php +++ b/src/Illuminate/View/ComponentAttributeBag.php @@ -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. * diff --git a/tests/View/ViewComponentAttributeBagTest.php b/tests/View/ViewComponentAttributeBagTest.php index 2d15ee11fac1..1b64720f9b79 100644 --- a/tests/View/ViewComponentAttributeBagTest.php +++ b/tests/View/ViewComponentAttributeBagTest.php @@ -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()); + } }