Skip to content

Commit 81ea922

Browse files
[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 <[email protected]>
1 parent f1d7624 commit 81ea922

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public function has($key)
6969
return array_key_exists($key, $this->attributes);
7070
}
7171

72+
/**
73+
* Determine if a given attribute is missing from the attribute array.
74+
*
75+
* @param string $key
76+
* @return bool
77+
*/
78+
public function missing($key)
79+
{
80+
return ! $this->has($key, $this->attributes);
81+
}
82+
7283
/**
7384
* Only include the given attribute from the attribute array.
7485
*

tests/View/ViewComponentAttributeBagTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,14 @@ public function testAttributeRetrieval()
9494
'test-extract-2' => 'defaultValue',
9595
]));
9696
}
97+
98+
public function testAttibuteExistence()
99+
{
100+
$bag = new ComponentAttributeBag(['name' => 'test']);
101+
102+
$this->assertTrue((bool) $bag->has('name'));
103+
$this->assertFalse((bool) $bag->missing('name'));
104+
$this->assertFalse((bool) $bag->has('class'));
105+
$this->assertTrue((bool) $bag->missing('class'));
106+
}
97107
}

0 commit comments

Comments
 (0)