feat: add Liquid filter 'find_by'

This commit is contained in:
Benjamin Nussbaum 2025-08-25 14:33:26 +02:00
parent 6cd00943a1
commit f4f8ab5181
4 changed files with 158 additions and 0 deletions

View file

@ -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;
}
}