1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class QueryBuilderRequest extends Request |
10
|
|
|
{ |
11
|
|
|
private static $includesArrayValueDelimiter = ','; |
12
|
|
|
private static $appendsArrayValueDelimiter = ','; |
13
|
|
|
private static $fieldsArrayValueDelimiter = ','; |
14
|
|
|
private static $sortsArrayValueDelimiter = ','; |
15
|
|
|
private static $filterArrayValueDelimiter = ','; |
16
|
|
|
|
17
|
|
|
public static function setArrayValueDelimiter(string $delimiter): void |
18
|
|
|
{ |
19
|
|
|
static::$filterArrayValueDelimiter = $delimiter; |
|
|
|
|
20
|
|
|
static::$includesArrayValueDelimiter = $delimiter; |
|
|
|
|
21
|
|
|
static::$appendsArrayValueDelimiter = $delimiter; |
|
|
|
|
22
|
|
|
static::$fieldsArrayValueDelimiter = $delimiter; |
|
|
|
|
23
|
|
|
static::$sortsArrayValueDelimiter = $delimiter; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function fromRequest(Request $request): self |
27
|
|
|
{ |
28
|
|
|
return static::createFrom($request, new self()); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function includes(): Collection |
32
|
|
|
{ |
33
|
|
|
$includeParameterName = config('query-builder.parameters.include'); |
34
|
|
|
|
35
|
|
|
$includeParts = $this->query($includeParameterName); |
36
|
|
|
|
37
|
|
|
if (!is_array($includeParts)) { |
38
|
|
|
$includeParts = explode(static::getIncludesArrayValueDelimiter(), $this->query($includeParameterName)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return collect($includeParts) |
42
|
|
|
->filter() |
43
|
|
|
->map([Str::class, 'camel']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function appends(): Collection |
47
|
|
|
{ |
48
|
|
|
$appendParameterName = config('query-builder.parameters.append'); |
49
|
|
|
|
50
|
|
|
$appendParts = $this->query($appendParameterName); |
51
|
|
|
|
52
|
|
|
if (!is_array($appendParts)) { |
53
|
|
|
$appendParts = explode(static::getAppendsArrayValueDelimiter(), strtolower($appendParts)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return collect($appendParts)->filter(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function fields(): Collection |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$fieldsParameterName = config('query-builder.parameters.fields'); |
62
|
|
|
|
63
|
|
|
$fieldsPerTable = collect($this->query($fieldsParameterName)); |
64
|
|
|
|
65
|
|
|
if ($fieldsPerTable->isEmpty()) { |
66
|
|
|
return collect(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $fieldsPerTable->map(function ($fields) { |
70
|
|
|
return explode(static::getFieldsArrayValueDelimiter(), $fields); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function sorts(): Collection |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$sortParameterName = config('query-builder.parameters.sort'); |
77
|
|
|
|
78
|
|
|
$sortParts = $this->query($sortParameterName); |
79
|
|
|
|
80
|
|
|
if (is_string($sortParts)) { |
81
|
|
|
$sortParts = explode(static::getSortsArrayValueDelimiter(), $sortParts); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return collect($sortParts)->filter(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function filters(): Collection |
88
|
|
|
{ |
89
|
|
|
$filterParameterName = config('query-builder.parameters.filter'); |
90
|
|
|
|
91
|
|
|
$filterParts = $this->query($filterParameterName, []); |
92
|
|
|
|
93
|
|
|
if (is_string($filterParts)) { |
94
|
|
|
return collect(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$filters = collect($filterParts); |
98
|
|
|
|
99
|
|
|
return $filters->map(function ($value) { |
100
|
|
|
return $this->getFilterValue($value); |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $value |
106
|
|
|
* |
107
|
|
|
* @return array|bool |
108
|
|
|
*/ |
109
|
|
|
protected function getFilterValue($value) |
110
|
|
|
{ |
111
|
|
|
if (is_array($value)) { |
112
|
|
|
return collect($value)->map(function ($valueValue) { |
113
|
|
|
return $this->getFilterValue($valueValue); |
114
|
|
|
})->all(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (Str::contains($value, static::getFilterArrayValueDelimiter())) { |
118
|
|
|
return explode(static::getFilterArrayValueDelimiter(), $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($value === 'true') { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($value === 'false') { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function setIncludesArrayValueDelimiter(string $includesArrayValueDelimiter): void |
133
|
|
|
{ |
134
|
|
|
static::$includesArrayValueDelimiter = $includesArrayValueDelimiter; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function setAppendsArrayValueDelimiter(string $appendsArrayValueDelimiter): void |
138
|
|
|
{ |
139
|
|
|
static::$appendsArrayValueDelimiter = $appendsArrayValueDelimiter; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function setFieldsArrayValueDelimiter(string $fieldsArrayValueDelimiter): void |
143
|
|
|
{ |
144
|
|
|
static::$fieldsArrayValueDelimiter = $fieldsArrayValueDelimiter; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public static function setSortsArrayValueDelimiter(string $sortsArrayValueDelimiter): void |
148
|
|
|
{ |
149
|
|
|
static::$sortsArrayValueDelimiter = $sortsArrayValueDelimiter; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public static function setFilterArrayValueDelimiter(string $filterArrayValueDelimiter): void |
153
|
|
|
{ |
154
|
|
|
static::$filterArrayValueDelimiter = $filterArrayValueDelimiter; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static function getIncludesArrayValueDelimiter(): string |
158
|
|
|
{ |
159
|
|
|
return static::$includesArrayValueDelimiter; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public static function getAppendsArrayValueDelimiter(): string |
163
|
|
|
{ |
164
|
|
|
return static::$appendsArrayValueDelimiter; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public static function getFieldsArrayValueDelimiter(): string |
168
|
|
|
{ |
169
|
|
|
return static::$fieldsArrayValueDelimiter; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function getSortsArrayValueDelimiter(): string |
173
|
|
|
{ |
174
|
|
|
return static::$sortsArrayValueDelimiter; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public static function getFilterArrayValueDelimiter(): string |
178
|
|
|
{ |
179
|
|
|
return static::$filterArrayValueDelimiter; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: