1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
class QueryBuilderRequest extends Request |
10
|
|
|
{ |
11
|
|
|
public static function fromRequest(Request $request): self |
12
|
|
|
{ |
13
|
|
|
return static::createFrom($request, new self()); |
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
View Code Duplication |
public function includes() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$parameter = config('query-builder.parameters.include'); |
19
|
|
|
|
20
|
|
|
$includeParts = $this->getPartsOfRequest($parameter); |
21
|
|
|
|
22
|
|
|
if (!is_array($includeParts)) { |
23
|
|
|
$includeParts = explode(',', strtolower($this->getPartsOfRequest($parameter))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return collect($includeParts)->filter(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function appends() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$appendParameter = config('query-builder.parameters.append'); |
32
|
|
|
|
33
|
|
|
$appendParts = $this->getPartsOfRequest($appendParameter); |
34
|
|
|
|
35
|
|
|
if (!is_array($appendParts)) { |
36
|
|
|
$appendParts = explode(',', strtolower($appendParts)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return collect($appendParts)->filter(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function filters() |
43
|
|
|
{ |
44
|
|
|
$filterParameter = config('query-builder.parameters.filter'); |
45
|
|
|
|
46
|
|
|
$filterParts = $this->getPartsOfRequest($filterParameter); |
47
|
|
|
|
48
|
|
|
if (is_string($filterParts)) { |
49
|
|
|
return collect(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$filters = collect($filterParts); |
53
|
|
|
|
54
|
|
|
return $filters->map(function ($value) { |
55
|
|
|
return $this->getFilterValue($value); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function fields(): Collection |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$fieldsParameter = config('query-builder.parameters.fields'); |
62
|
|
|
|
63
|
|
|
$fieldsPerTable = collect($this->getPartsOfRequest($fieldsParameter)); |
64
|
|
|
|
65
|
|
|
if ($fieldsPerTable->isEmpty()) { |
66
|
|
|
return collect(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $fieldsPerTable->map(function ($fields) { |
70
|
|
|
return explode(',', $fields); |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array|string|null |
76
|
|
|
*/ |
77
|
|
|
public function sort() |
78
|
|
|
{ |
79
|
|
|
return $this->getPartsOfRequest(config('query-builder.parameters.sort')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function sorts(): Collection |
83
|
|
|
{ |
84
|
|
|
$sortParts = $this->sort(); |
85
|
|
|
|
86
|
|
|
if (is_string($sortParts)) { |
87
|
|
|
$sortParts = explode(',', $sortParts); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return collect($sortParts)->filter(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
protected function getFilterValue($value) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if (is_array($value)) { |
96
|
|
|
return collect($value)->map(function ($valueValue) { |
97
|
|
|
return $this->getFilterValue($valueValue); |
98
|
|
|
})->all(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (Str::contains($value, ',')) { |
102
|
|
|
return explode(',', $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($value === 'true') { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($value === 'false') { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getPartsOfRequest($parameter) |
117
|
|
|
{ |
118
|
|
|
if (!empty($this->query($parameter, []))) { |
119
|
|
|
return $this->query($parameter, []); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!empty($this->json($parameter, []))) { |
123
|
|
|
return $this->json($parameter, []); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: