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

Filter::applyFilter()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
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
?>