|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Boduch\Grid\Source; |
|
4
|
|
|
|
|
5
|
|
|
use Boduch\Grid\Column; |
|
6
|
|
|
use Boduch\Grid\Filters\FilterOperator; |
|
7
|
|
|
use Boduch\Grid\Order; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
class EloquentSource implements SourceInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $source; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model $source |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($source) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->source = $source; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Column[] $columns |
|
27
|
|
|
*/ |
|
28
|
|
|
public function applyFilters($columns) |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ($columns as $column) { |
|
31
|
|
|
/** @var \Boduch\Grid\Column $column */ |
|
32
|
|
|
if ($column->isFilterable() && !$column->getFilter()->isEmpty()) { |
|
33
|
|
|
$this->buildQuery($column); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param int $perPage |
|
40
|
|
|
* @param int $currentPage |
|
41
|
|
|
* @param Order $order |
|
42
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
|
43
|
|
|
*/ |
|
44
|
|
|
public function execute($perPage, $currentPage, Order $order) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this |
|
|
|
|
|
|
47
|
|
|
->source |
|
48
|
|
|
->when($order->getColumn(), function (Builder $builder) use ($order) { |
|
|
|
|
|
|
49
|
|
|
return $builder->orderBy($order->getColumn(), $order->getDirection()); |
|
50
|
|
|
}) |
|
51
|
|
|
->forPage($currentPage, $perPage) |
|
52
|
|
|
->get(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function total() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->source->count(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Column $column |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function buildQuery(Column $column) |
|
67
|
|
|
{ |
|
68
|
|
|
$name = $column->getFilter()->getName(); |
|
69
|
|
|
$input = $column->getFilter()->getInput(); |
|
70
|
|
|
|
|
71
|
|
|
if ($column->getFilter()->getOperator() == FilterOperator::OPERATOR_BETWEEN) { |
|
72
|
|
|
$this->source->whereBetween($name, $input); |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->source->where( |
|
|
|
|
|
|
75
|
|
|
$name, |
|
76
|
|
|
$column->getFilter()->getOperator(), |
|
77
|
|
|
$this->normalizeValue($input, $column->getFilter()->getOperator()) |
|
|
|
|
|
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string|string[] $value |
|
84
|
|
|
* @param string $operator |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function normalizeValue($value, $operator) |
|
88
|
|
|
{ |
|
89
|
|
|
if (is_array($value)) { |
|
90
|
|
|
$value = array_filter($value); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($operator == FilterOperator::OPERATOR_LIKE || $operator == FilterOperator::OPERATOR_ILIKE) { |
|
94
|
|
|
$value = str_replace('*', '%', $value); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: