1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | if (!function_exists('match')) { |
||
6 | /** |
||
7 | * A simple match function that compares a value against multiple cases. |
||
8 | * |
||
9 | * @param mixed $value The value to match. |
||
10 | * @param array $cases An associative array of cases, where keys can be callables or values to compare. |
||
11 | * @param mixed|null $default Optional default value to return if no matches are found. |
||
12 | * @return mixed The result associated with the matched case or the default value. |
||
13 | */ |
||
14 | function match($value, array $cases, $default = null) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
15 | { |
||
16 | foreach ($cases as $case => $result) { |
||
17 | if ((is_callable($case) && $case($value)) || $case === $value) { |
||
18 | return is_callable($result) ? $result($value) : $result; |
||
19 | } |
||
20 | } |
||
21 | |||
22 | return $default; |
||
23 | } |
||
24 | } |
||
25 |