Skip to content

Commit 0abe2db

Browse files
committed
added filter and whereStartsWith
1 parent efdab6e commit 0abe2db

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Illuminate/View/ComponentAttributeBag.php

+24
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ public function except($keys)
8383
return new static($values);
8484
}
8585

86+
/**
87+
* Filter the attributes, returning a bag of attributes that pass the filter.
88+
*
89+
* @param callable $callback
90+
* @return static
91+
*/
92+
public function filter($callback)
93+
{
94+
return new static(collect($this->attributes)->filter($callback)->all());
95+
}
96+
97+
/**
98+
* Return a bag of attributes that have keys starting with the given value / pattern.
99+
*
100+
* @param string $string
101+
* @return static
102+
*/
103+
public function whereStartsWith($string)
104+
{
105+
return $this->filter(function ($value, $key) use ($string) {
106+
return Str::startsWith($key, $string);
107+
});
108+
}
109+
86110
/**
87111
* Exclude the given attribute from the attribute array.
88112
*

tests/View/ViewComponentAttributeBagTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function testAttributeRetrieval()
1111
{
1212
$bag = new ComponentAttributeBag(['class' => 'font-bold', 'name' => 'test']);
1313

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

0 commit comments

Comments
 (0)