Total Complexity | 11 |
Total Lines | 43 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
14 | 1 | final class Filter { |
|
15 | 1 | use \Nette\StaticClass; |
|
16 | |||
17 | public const OPERATORS = ["==", ">=", ">", "<=", "<", "!=",]; |
||
18 | |||
19 | public static function getOperator(string $input): string { |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param mixed $value |
||
30 | */ |
||
31 | protected static function getCondition(object $item, string $key, string $operator, $value): string { |
||
32 | 1 | if($key === "%class%") { |
|
33 | 1 | return "return \"" . get_class($item) . "\" $operator \"$value\";"; |
|
34 | } |
||
35 | 1 | return "return \"{$item->$key}\" $operator \"$value\";"; |
|
36 | } |
||
37 | |||
38 | public static function matches(object $item, array $filter): bool { |
||
39 | /** @var string $key */ |
||
40 | 1 | foreach($filter as $key => $value) { |
|
41 | 1 | $operator = static::getOperator($key); |
|
42 | /** @var string $key */ |
||
43 | 1 | $key = Strings::endsWith($key, $operator) ? Strings::before($key, $operator) : $key; |
|
44 | 1 | if(!eval(static::getCondition($item, $key, $operator, $value))) { |
|
45 | 1 | return false; |
|
46 | } |
||
47 | } |
||
48 | 1 | return true; |
|
49 | } |
||
50 | |||
51 | public static function applyFilter(array $input, array $filter = []): array { |
||
57 | 1 | })); |
|
58 | } |
||
59 | } |
||
60 | ?> |