1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sarala\Query; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
10
|
|
|
|
11
|
|
|
abstract class QueryBuilderAbstract |
12
|
|
|
{ |
13
|
|
|
/** @var Request $request */ |
14
|
|
|
protected $request; |
15
|
|
|
|
16
|
|
|
/** @var Builder $query */ |
17
|
|
|
protected $query; |
18
|
|
|
|
19
|
|
|
/** @var Fields $fields */ |
20
|
|
|
protected $fields; |
21
|
|
|
|
22
|
|
|
/** @var QueryParamBag $includes */ |
23
|
|
|
protected $includes; |
24
|
|
|
|
25
|
|
|
/** @var QueryParamBag $filters */ |
26
|
|
|
protected $filters; |
27
|
|
|
|
28
|
|
|
/** @var QueryHelper $queryHelper */ |
29
|
|
|
private $queryHelper; |
30
|
|
|
|
31
|
|
|
public function __construct(Request $request) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->request = $request; |
34
|
|
|
$this->fields = $this->request->fields(); |
35
|
|
|
$this->includes = $this->request->includes(); |
36
|
|
|
$this->filters = $this->request->filters(); |
37
|
|
|
$this->query = $this->init(); |
38
|
|
|
$this->queryHelper = new QueryHelper($this->query, $this->includes); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
abstract protected function init(): Builder; |
42
|
|
|
|
43
|
|
|
protected function fields() |
44
|
|
|
{ |
45
|
|
|
// .. |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function filter(QueryParamBag $filters) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
// .. |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function include(QueryParamBag $includes) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
// .. |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function sort(): array |
59
|
|
|
{ |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function exact($fields): QueryHelper |
64
|
|
|
{ |
65
|
|
|
return $this->queryHelper->exact($fields); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function countExact($fields): QueryHelper |
69
|
|
|
{ |
70
|
|
|
return $this->queryHelper->countExact($fields); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function alias($fields, $value): QueryHelper |
74
|
|
|
{ |
75
|
|
|
return $this->queryHelper->alias($fields, $value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function countAlias($fields, $value): QueryHelper |
79
|
|
|
{ |
80
|
|
|
return $this->queryHelper->countAlias($fields, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function fetch() |
84
|
|
|
{ |
85
|
|
|
$this->fields(); |
86
|
|
|
|
87
|
|
|
if ($this->request->filled('filter')) { |
88
|
|
|
$this->filter($this->filters); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->request->filled('include')) { |
92
|
|
|
$this->include($this->includes); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($this->request->filled('sort')) { |
96
|
|
|
$this->appendSortQuery(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->request->filled('page')) { |
100
|
|
|
return $this->appendParamsToPaginatedUrl(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->query->get(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function appendSortQuery(): void |
107
|
|
|
{ |
108
|
|
|
$allowedSorts = $this->sort(); |
109
|
|
|
|
110
|
|
|
$this->request->sorts()->each(function (SortField $field) use ($allowedSorts) { |
111
|
|
|
if (in_array($field->getField(), $allowedSorts)) { |
112
|
|
|
$this->query->orderBy($field->getField(), $field->getDirection()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function appendParamsToPaginatedUrl(): LengthAwarePaginator |
118
|
|
|
{ |
119
|
|
|
$perPage = $this->request->input('page.size', 10); |
120
|
|
|
$page = $this->request->input('page.number', 1); |
121
|
|
|
$paginator = $this->query->paginate($perPage, ['*'], 'page[number]', $page); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$paginator->appends($this->request->except('page')); |
124
|
|
|
|
125
|
|
|
foreach ($this->request->get('filter', []) as $key => $value) { |
126
|
|
|
if (is_null($value)) { |
127
|
|
|
$value = ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$paginator->appends("filter[{$key}]", $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (! is_null($this->request->input('page.size'))) { |
134
|
|
|
$paginator->appends('page[size]', $this->request->input('page.size')); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $paginator; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function fetchFirst() |
141
|
|
|
{ |
142
|
|
|
return $this->fetch()->first(); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|