1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NwLaravel\Repositories\Criterias\Filters; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class FilterWhere implements FilterInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $table; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $columns; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $dates; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $operator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construct |
31
|
|
|
* |
32
|
|
|
* @param string $table |
33
|
|
|
* @param array $columns |
34
|
|
|
* @param array $dates |
35
|
|
|
* @param string $operator |
36
|
|
|
*/ |
37
|
24 |
|
public function __construct($table, $columns, $dates, $operator = '=') |
38
|
|
|
{ |
39
|
24 |
|
$this->table = $table; |
40
|
24 |
|
$this->columns = $columns; |
41
|
24 |
|
$this->dates = $dates; |
42
|
24 |
|
$this->operator = $operator; |
43
|
24 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Filter |
47
|
|
|
* |
48
|
|
|
* @param Query\Builder $query |
49
|
|
|
* @param int|string $key |
50
|
|
|
* @param mixed $value |
51
|
|
|
* |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
23 |
|
public function filter($query, $key, $value) |
55
|
|
|
{ |
56
|
23 |
|
$operator = $this->operator; |
57
|
|
|
|
58
|
23 |
|
$validOperator = function ($operator) { |
59
|
23 |
|
$operators = ['=', '<', '>', '<=', '>=', '<>', '!=']; |
60
|
23 |
|
return in_array($operator, $operators); |
61
|
23 |
|
}; |
62
|
|
|
|
63
|
23 |
|
if (! $validOperator($operator)) { |
64
|
2 |
|
throw new InvalidArgumentException("Illegal operator and value combination."); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Raw Expression with Bidding |
68
|
21 |
|
if (strpos($key, '?') !== false) { |
69
|
2 |
|
$query = $query->whereRaw($key, (array) $value); |
|
|
|
|
70
|
2 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
20 |
|
$table = $this->table; |
74
|
20 |
|
$column = $key; |
75
|
20 |
|
if (preg_match('/^([a-zA-Z0-9_\-]+)\.([a-zA-Z0-9_\-]+)/', $key, $matches)) { |
76
|
3 |
|
$table = $matches[1]; |
77
|
3 |
|
$column = $matches[2]; |
78
|
3 |
|
} |
79
|
|
|
|
80
|
|
|
// Montagem Tabela com Coluns |
81
|
20 |
|
$key = $table.'.'.$column; |
82
|
|
|
|
83
|
|
|
// Attributes Valids |
84
|
20 |
|
if (in_array($column, $this->columns)) { |
85
|
12 |
|
if (is_null($value)) { |
86
|
3 |
|
if ($operator == '!=' || $operator == '<>') { |
87
|
2 |
|
$query = $query->whereNotNull($key); |
|
|
|
|
88
|
2 |
|
} else { |
89
|
2 |
|
$query = $query->whereNull($key); |
|
|
|
|
90
|
|
|
} |
91
|
3 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
if (in_array($column, $this->dates)) { |
95
|
5 |
|
$filterDate = new FilterDate($operator); |
96
|
5 |
|
return $filterDate->filter($query, $key, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Using Where In With An Array |
100
|
6 |
|
if (is_array($value)) { |
101
|
3 |
|
if ($operator == '!=' || $operator == '<>') { |
102
|
2 |
|
$query = $query->whereNotIn($key, $value); |
|
|
|
|
103
|
2 |
|
} else { |
104
|
2 |
|
$query = $query->whereIn($key, $value); |
|
|
|
|
105
|
|
|
} |
106
|
3 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Busca Direta |
110
|
4 |
|
$query = $query->where($key, $operator, $value); |
|
|
|
|
111
|
|
|
|
112
|
4 |
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
9 |
|
$filterBetween = new FilterBetween($this->table, $this->columns, $this->dates); |
116
|
9 |
|
return $filterBetween->filter($query, $key, $value); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.