Skip to content

Commit fe8cb8a

Browse files
committed
Use collection for firstWhere
1 parent 5d2ce59 commit fe8cb8a

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/Twig/Extensions/ArrayTwigExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function getFilters(): array
3030
new TwigFilter('contains', Arr::contains(...)),
3131
new TwigFilter('diff', 'array_diff'),
3232
new TwigFilter('filter', $this->filterFilter(...), ['needs_environment' => true]),
33-
new TwigFilter('firstWhere', Arr::first(...)),
33+
new TwigFilter('firstWhere', $this->firstWhereFilter(...)),
3434
new TwigFilter('flatten', Arr::flatten(...)),
3535
new TwigFilter('group', $this->groupFilter(...)),
3636
new TwigFilter('indexOf', $this->indexOfFilter(...)),
@@ -115,6 +115,11 @@ public function filterFilter(TwigEnvironment $env, iterable $arr, ?callable $arr
115115
return iterator_to_array($filtered);
116116
}
117117

118+
public function firstWhereFilter(iterable $array, callable|string $key, mixed $value = true, bool $strict = false): mixed
119+
{
120+
return collect($array)->firstWhere($key, $strict ? '===' : '==', $value);
121+
}
122+
118123
/**
119124
* @throws RuntimeError
120125
*/

0 commit comments

Comments
 (0)