Skip to content

Commit

Permalink
[9.x] Add missing method to View/ComponentAttributeBag (#45016)
Browse files Browse the repository at this point in the history
* add 'missing' method ComponentAttributeBag

* add tests for ComponentAttributeBag for 'has' and 'missing'

* code style

* Update ComponentAttributeBag.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
ProjektGopher and taylorotwell committed Nov 18, 2022
1 parent f1d7624 commit 81ea922
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 81ea922

Please sign in to comment.