1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sarala\Query; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Sarala\Contracts\QueryContract; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
|
12
|
|
|
abstract class QueryBuilderAbstract implements QueryContract |
13
|
|
|
{ |
14
|
|
|
use QueryMapper; |
15
|
|
|
|
16
|
|
|
/** @var Request $request */ |
17
|
|
|
protected $request; |
18
|
|
|
|
19
|
|
|
/** @var Builder $query */ |
20
|
|
|
protected $query; |
21
|
|
|
|
22
|
|
|
/** @var Fields $fields */ |
23
|
|
|
protected $fields; |
24
|
|
|
|
25
|
|
|
/** @var QueryParamBag $includes */ |
26
|
|
|
protected $includes; |
27
|
|
|
|
28
|
|
|
/** @var QueryParamBag $filters */ |
29
|
|
|
protected $filters; |
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
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function fields() |
41
|
|
|
{ |
42
|
|
|
// .. |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function filter(QueryParamBag $filters) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
// .. |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function include(QueryParamBag $includes) |
51
|
|
|
{ |
52
|
|
|
// .. |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function orderBy(): array |
56
|
|
|
{ |
57
|
|
|
return []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function fetch() |
61
|
|
|
{ |
62
|
|
|
$this->fields(); |
63
|
|
|
|
64
|
|
|
if ($this->request->filled('filter')) { |
65
|
|
|
$this->filter($this->filters); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->request->filled('include')) { |
69
|
|
|
$this->include($this->includes); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->request->filled('sort')) { |
73
|
|
|
$this->appendSortQuery(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->request->filled('page')) { |
77
|
|
|
return $this->appendParamsToPaginatedUrl(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->query->get(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function appendSortQuery(): void |
84
|
|
|
{ |
85
|
|
|
$allowedSorts = $this->orderBy(); |
86
|
|
|
|
87
|
|
|
$this->request->sorts()->each(function (SortField $field) use ($allowedSorts) { |
88
|
|
|
if (in_array($field->getField(), $allowedSorts)) { |
89
|
|
|
$this->query->orderBy($field->getField(), $field->getDirection()); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function appendParamsToPaginatedUrl(): LengthAwarePaginator |
95
|
|
|
{ |
96
|
|
|
$perPage = $this->request->input('page.size', 10); |
97
|
|
|
$page = $this->request->input('page.number', 1); |
98
|
|
|
$paginator = $this->query->paginate($perPage, ['*'], 'page[number]', $page); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$paginator->appends($this->request->except('page')); |
101
|
|
|
|
102
|
|
|
if (! is_null($this->request->input('page.size'))) { |
103
|
|
|
$paginator->appends('page[size]', $this->request->input('page.size')); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $paginator; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function fetchFirst() |
110
|
|
|
{ |
111
|
|
|
return $this->fetch()->first(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|