|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Filters; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Sfneal\Helpers\Strings\StringHelpers; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractFilter implements FilterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Builder |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $query; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $column = 'id'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* FilterListString constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string|null $column |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(string $column = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->column = $column ?? $this->column; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Apply a given search value to the builder instance. |
|
32
|
|
|
* |
|
33
|
|
|
* - if a 'string' value is found then a single value where clause is added |
|
34
|
|
|
* - if a 'array' value is found then a whereIn clause is added |
|
35
|
|
|
* |
|
36
|
|
|
* @param Builder $query |
|
37
|
|
|
* @param mixed $value |
|
38
|
|
|
* @return Builder $query |
|
39
|
|
|
*/ |
|
40
|
|
|
public function apply(Builder $query, $value = null) |
|
41
|
|
|
{ |
|
42
|
|
|
// Set the Query |
|
43
|
|
|
$this->setQuery($query); |
|
44
|
|
|
|
|
45
|
|
|
// Remove whitespace from the value |
|
46
|
|
|
if (is_string($value)) { |
|
47
|
|
|
$value = trim($value); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Determine if the the ID's are a comma or space separated list |
|
51
|
|
|
// if true, add where clause looking for an array of ID's |
|
52
|
|
|
if (is_string($value) && $ids = (new StringHelpers($value))->isListString()) { |
|
53
|
|
|
$this->query = $this->arrayValueClause($ids); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Add where clause searching for a single Plan ID |
|
57
|
|
|
else { |
|
58
|
|
|
$this->query = $this->stringValueClause($value); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->query; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add a where clause that searches for a single value. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $id |
|
68
|
|
|
* @return Builder $query |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function stringValueClause($id) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->query->whereIn($this->column, (array) $id); |
|
73
|
|
|
|
|
74
|
|
|
return $this->query; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a where clause that searches for an array of values. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $ids |
|
81
|
|
|
* @return Builder $query; |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function arrayValueClause(array $ids) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->query->whereIn($this->column, $ids); |
|
86
|
|
|
|
|
87
|
|
|
return $this->query; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the Query Builder property. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Builder $query |
|
94
|
|
|
*/ |
|
95
|
|
|
private function setQuery(Builder $query): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->query = $query; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|