|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\Repositories\Criterias\Filters; |
|
4
|
|
|
|
|
5
|
|
|
use Datetime; |
|
6
|
|
|
|
|
7
|
|
|
class FilterDate implements FilterInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $operator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var mixed |
|
16
|
|
|
*/ |
|
17
|
|
|
private $grammar; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Construct |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $operator |
|
23
|
|
|
*/ |
|
24
|
16 |
|
public function __construct($operator = '=') |
|
25
|
|
|
{ |
|
26
|
16 |
|
$this->operator = $operator; |
|
27
|
16 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Filter |
|
31
|
|
|
* |
|
32
|
|
|
* @param Query\Builder $query |
|
33
|
|
|
* @param int|string $key |
|
34
|
|
|
* @param mixed $value |
|
35
|
|
|
* |
|
36
|
|
|
* @return boolean |
|
37
|
|
|
*/ |
|
38
|
15 |
|
public function filter($query, $key, $value) |
|
39
|
|
|
{ |
|
40
|
15 |
|
if (is_string($value)) { |
|
41
|
8 |
|
$value = str_replace("_", '', $value); // Caso vier com a mascara __/__/____ |
|
42
|
8 |
|
} |
|
43
|
|
|
|
|
44
|
15 |
|
if (!empty($value) || is_null($value)) { |
|
45
|
14 |
|
$date = asDateTime($value); |
|
46
|
|
|
|
|
47
|
14 |
|
if ($date instanceof Datetime && !is_null($value)) { |
|
48
|
11 |
|
$query = $query->whereRaw( |
|
|
|
|
|
|
49
|
11 |
|
$this->dateFormatDb($query, $key, $this->operator), |
|
50
|
11 |
|
[$date->format('Y-m-d')] |
|
51
|
11 |
|
); |
|
52
|
11 |
|
} else { |
|
53
|
4 |
|
if ($this->operator == '!=' || $this->operator == '<>') { |
|
54
|
2 |
|
$query = $query->whereNotNull($key); |
|
|
|
|
|
|
55
|
2 |
|
} else { |
|
56
|
3 |
|
$query = $query->whereNull($key); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
14 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Date Formate Database |
|
68
|
|
|
* |
|
69
|
|
|
* @param Builder $query Builder |
|
70
|
|
|
* @param string $key Column |
|
71
|
|
|
* @param string $operator String Operator |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
11 |
|
private function dateFormatDb($query, $key, $operator) |
|
76
|
|
|
{ |
|
77
|
11 |
|
if (!$this->grammar) { |
|
78
|
11 |
|
$this->grammar = $query->getQuery()->getGrammar(); |
|
79
|
11 |
|
} |
|
80
|
|
|
|
|
81
|
11 |
|
$key = $this->grammar->wrap($key); |
|
82
|
|
|
|
|
83
|
11 |
|
$formatDb = sprintf("%s %s ?", $key, $operator); |
|
84
|
|
|
|
|
85
|
11 |
|
switch (true) { |
|
86
|
11 |
|
case $this->grammar instanceof \Illuminate\Database\Query\Grammars\MySqlGrammar: |
|
87
|
5 |
|
$formatDb = sprintf("DATE(%s) %s ?", $key, $operator); |
|
88
|
5 |
|
break; |
|
89
|
|
|
|
|
90
|
6 |
|
case $this->grammar instanceof \Illuminate\Database\Query\Grammars\PostgresGrammar: |
|
91
|
2 |
|
$formatDb = sprintf("DATE_TRUNC('day', %s) %s ?", $key, $operator); |
|
92
|
2 |
|
break; |
|
93
|
|
|
|
|
94
|
4 |
|
case $this->grammar instanceof \Illuminate\Database\Query\Grammars\SQLiteGrammar: |
|
95
|
2 |
|
$formatDb = sprintf("strftime('%%Y-%%m-%%d', %s) %s ?", $key, $operator); |
|
96
|
2 |
|
break; |
|
97
|
|
|
|
|
98
|
2 |
|
case $this->grammar instanceof \Illuminate\Database\Query\Grammars\SqlServerGrammar: |
|
99
|
2 |
|
$formatDb = sprintf("CAST(%s AS DATE) %s ?", $key, $operator); |
|
100
|
2 |
|
} |
|
101
|
|
|
|
|
102
|
11 |
|
return $formatDb; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.