Skip to content

Commit

Permalink
added filter and whereStartsWith
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 26, 2020
1 parent efdab6e commit 0abe2db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ public function except($keys)
return new static($values);
}

/**
* Filter the attributes, returning a bag of attributes that pass the filter.
*
* @param callable $callback
* @return static
*/
public function filter($callback)
{
return new static(collect($this->attributes)->filter($callback)->all());
}

/**
* Return a bag of attributes that have keys starting with the given value / pattern.
*
* @param string $string
* @return static
*/
public function whereStartsWith($string)
{
return $this->filter(function ($value, $key) use ($string) {
return Str::startsWith($key, $string);
});
}

/**
* Exclude the given attribute from the attribute array.
*
Expand Down
1 change: 1 addition & 0 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function testAttributeRetrieval()
{
$bag = new ComponentAttributeBag(['class' => 'font-bold', 'name' => 'test']);

$this->assertSame('class="font-bold"', (string) $bag->whereStartsWith('class'));
$this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->merge(['class' => 'mt-4']));
$this->assertSame('class="mt-4 font-bold" name="test"', (string) $bag->merge(['class' => 'mt-4', 'name' => 'foo']));
$this->assertSame('class="mt-4 font-bold" id="bar" name="test"', (string) $bag->merge(['class' => 'mt-4', 'id' => 'bar']));
Expand Down

0 comments on commit 0abe2db

Please sign in to comment.