From 81ea922574b5244e6710a82f6e27cd885deec612 Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Fri, 18 Nov 2022 11:51:25 -0800 Subject: [PATCH] [9.x] Add `missing` method to `View/ComponentAttributeBag` (#45016) * add 'missing' method ComponentAttributeBag * add tests for ComponentAttributeBag for 'has' and 'missing' * code style * Update ComponentAttributeBag.php Co-authored-by: Taylor Otwell --- src/Illuminate/View/ComponentAttributeBag.php | 11 +++++++++++ tests/View/ViewComponentAttributeBagTest.php | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Illuminate/View/ComponentAttributeBag.php b/src/Illuminate/View/ComponentAttributeBag.php index 1be7382ecafb..0c6c615f36c2 100644 --- a/src/Illuminate/View/ComponentAttributeBag.php +++ b/src/Illuminate/View/ComponentAttributeBag.php @@ -69,6 +69,17 @@ public function has($key) return array_key_exists($key, $this->attributes); } + /** + * Determine if a given attribute is missing from the attribute array. + * + * @param string $key + * @return bool + */ + public function missing($key) + { + return ! $this->has($key, $this->attributes); + } + /** * Only include the given attribute from the attribute array. * diff --git a/tests/View/ViewComponentAttributeBagTest.php b/tests/View/ViewComponentAttributeBagTest.php index e1cb4f6b9131..453014e09c84 100644 --- a/tests/View/ViewComponentAttributeBagTest.php +++ b/tests/View/ViewComponentAttributeBagTest.php @@ -94,4 +94,14 @@ public function testAttributeRetrieval() 'test-extract-2' => 'defaultValue', ])); } + + public function testAttibuteExistence() + { + $bag = new ComponentAttributeBag(['name' => 'test']); + + $this->assertTrue((bool) $bag->has('name')); + $this->assertFalse((bool) $bag->missing('name')); + $this->assertFalse((bool) $bag->has('class')); + $this->assertTrue((bool) $bag->missing('class')); + } }