Completed
Push — master ( bb5999...acd239 )
by Jakub
02:11
created

Filter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOperator() 0 7 3
B applyFilter() 0 14 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Utils;
5
6
use Nette\Utils\Strings;
7
8
/**
9
 * Filter
10
 *
11
 * @author Jakub Konečný
12
 * @internal
13
 */
14 1
final class Filter {
15 1
  use \Nette\StaticClass;
16
  
17
  public const OPERATORS = ["==", ">=", ">", "<=", "<",];
18
  
19
  public static function getOperator(string $input): string {
20 1
    foreach(static::OPERATORS as $operator) {
21 1
      if(Strings::endsWith($input, $operator)) {
22 1
        return $operator;
23
      }
24
    }
25 1
    return static::OPERATORS[0];
26
  }
27
  
28
  public static function applyFilter(array $input, array $filter = []): array {
29 1
    if(count($filter) === 0) {
30 1
      return $input;
31
    }
32 1
    return array_values(array_filter($input, function($item) use($filter) {
33
      /** @var string $key */
34 1
      foreach($filter as $key => $value) {
35 1
        $operator = static::getOperator($key);
36 1
        $key = Strings::endsWith($key, $operator) ? Strings::before($key, $operator) : $key;
37 1
        if(!eval("return \"{$item->$key}\" $operator \"$value\";")) {
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
introduced by
Use of eval() is discouraged
Loading history...
38 1
          return false;
39
        }
40
      }
41 1
      return true;
42 1
    }));
43
  }
44
}
45
?>