-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.php
196 lines (174 loc) · 5.8 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Kellton\Tools\Builders;
use Carbon\Carbon;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Kellton\Tools\Data\FilterData;
use Kellton\Tools\Enums\FilterOperator;
use Kellton\Tools\Enums\OrderDirection;
use Kellton\Tools\Models\Model;
use Kellton\Tools\Undefined;
/**
* Class Builder handles base class for eloquent builders.
*
* @method self whereId(int $value)
* @method Model|EloquentCollection |array|static|null find($id, $columns = ['*'])
* @method Model|object|static|null first($columns = ['*'])
* @method Model|self create(array $attributes = [])
*/
class Builder extends EloquentBuilder
{
/**
* Scope a query with whereIn by a primary key.
*
* @param Collection $values
*
* @return static
*/
public function whereKeyIn(Collection $values): static
{
if ($values->isEmpty()) {
return $this->whereRaw('0=1');
}
return $this->whereIn($this->model->getQualifiedKeyName(), $values);
}
/**
* Scope a query to only include models in the given values for column.
*
* @param string $columnName
* @param Collection $values
*
* @return static
*/
public function whereInColumn(string $columnName, Collection $values): static
{
if ($values->isEmpty()) {
return $this->whereRaw('0=1');
}
return $this->whereIn($this->qualifyColumn($columnName), $values);
}
/**
* Scope a query to only include models not in the given values for column.
*
* @param string $columnName
* @param Collection $values
*
* @return static
*/
public function whereNotInColumn(string $columnName, Collection $values): static
{
if ($values->isEmpty()) {
return $this->whereRaw('1=1');
}
return $this->whereNotIn($this->qualifyColumn($columnName), $values);
}
/**
* Scope a query to check if a date is in range of 2 date columns.
*
* @param string $startAtColumnName
* @param string $endAtColumnName
* @param Carbon|null $date
*
* @return static
*/
public function whereDateInRange(string $startAtColumnName, string $endAtColumnName, ?Carbon $date = null): static
{
$self = $this;
if (!$date) {
$date = now();
}
return $this
->where(static function (self $query) use ($self, $date, $startAtColumnName) {
$query
->whereNull($self->qualifyColumn($startAtColumnName))
->orWhere(
$self->qualifyColumn($startAtColumnName),
'<=',
$date->format(config('tools.date.datetime_format'))
);
})
->where(static function (self $query) use ($self, $date, $endAtColumnName) {
$query
->whereNull($self->qualifyColumn($endAtColumnName))
->orWhere(
$self->qualifyColumn($endAtColumnName),
'>=',
$date->format(config('tools.date.datetime_format'))
);
});
}
/**
* Scope a query to add filters based on the given query data.
*
* @param Collection|Undefined $filters
*
* @return $this
*/
public function filters(Collection|Undefined $filters): static
{
if ($filters instanceof Undefined) {
return $this;
}
$filters->each(function (FilterData $filter) {
$operation = match ($filter->operation) {
FilterOperator::EQUAL => '=',
FilterOperator::LIKE => 'LIKE',
FilterOperator::IN => 'IN',
};
$value = $filter->value;
if ($filter->operation === FilterOperator::LIKE) {
$value = "%{$value}%";
}
if ($filter->operation === FilterOperator::IN) {
// Move below line outside if condition if needed
$type = DB::getSchemaBuilder()->getColumnType($this->model->getTable(), $filter->name);
$value = collect(explode(',', $value));
if ($type === 'json') {
$this->whereJsonContains($filter->name, $value);
} else {
$this->whereIn($filter->name, $value);
}
} else {
$this->where($filter->name, $operation, $value);
}
});
return $this;
}
/**
* Scope a query to add sorting based on the given query data.
*
* @param string|Undefined $orderBy
* @param OrderDirection|Undefined $orderDirection
*
* @return $this
*/
public function order(string|Undefined $orderBy, OrderDirection|Undefined $orderDirection): static
{
if (!($orderBy instanceof Undefined)) {
$direction = $orderDirection instanceof Undefined ? OrderDirection::ASC : $orderDirection;
$this->orderBy($orderBy, $direction->value);
}
return $this;
}
/**
* Paginate the given query.
*
* @param int|Undefined $page
* @param int|Undefined $perPage
*
* @return LengthAwarePaginator
*/
public function pagination(int|Undefined $page, int|Undefined $perPage): LengthAwarePaginator
{
if ($page instanceof Undefined) {
$page = 1;
}
if ($perPage instanceof Undefined) {
$perPage = (int) config('tools.pagination.per_page');
}
return $this->paginate(perPage: $perPage, page: $page);
}
}