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