mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat: add Liquid filter 'find_by'
This commit is contained in:
parent
6cd00943a1
commit
f4f8ab5181
4 changed files with 158 additions and 0 deletions
|
|
@ -19,4 +19,24 @@ class Data extends FiltersProvider
|
|||
{
|
||||
return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an object in a collection by a specific key-value pair
|
||||
*
|
||||
* @param array $collection The collection to search in
|
||||
* @param string $key The key to search for
|
||||
* @param mixed $value The value to match
|
||||
* @param mixed $fallback Optional fallback value if no match is found
|
||||
* @return mixed The matching object or fallback value
|
||||
*/
|
||||
public function find_by(array $collection, string $key, mixed $value, mixed $fallback = null): mixed
|
||||
{
|
||||
foreach ($collection as $item) {
|
||||
if (is_array($item) && isset($item[$key]) && $item[$key] === $value) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
return $fallback;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue