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
|
|
|
public function includes(): Collection |
17
|
|
|
{ |
18
|
|
|
$includeParameterName = config('query-builder.parameters.include'); |
19
|
|
|
|
20
|
|
|
$includeParts = $this->query($includeParameterName); |
21
|
|
|
|
22
|
|
|
if (! is_array($includeParts)) { |
23
|
|
|
$includeParts = explode(',', strtolower($this->query($includeParameterName))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return collect($includeParts) |
27
|
|
|
->filter() |
28
|
|
|
->map([Str::class, 'camel']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function appends(): Collection |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$appendParameterName = config('query-builder.parameters.append'); |
34
|
|
|
|
35
|
|
|
$appendParts = $this->query($appendParameterName); |
36
|
|
|
|
37
|
|
|
if (! is_array($appendParts)) { |
38
|
|
|
$appendParts = explode(',', strtolower($appendParts)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return collect($appendParts)->filter(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function filters(): Collection |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$filterParameterName = config('query-builder.parameters.filter'); |
47
|
|
|
|
48
|
|
|
$filterParts = $this->query($filterParameterName, []); |
49
|
|
|
|
50
|
|
|
if (is_string($filterParts)) { |
51
|
|
|
return collect(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$filters = collect($filterParts); |
55
|
|
|
|
56
|
|
|
return $filters->map(function ($value) { |
57
|
|
|
return $this->getFilterValue($value); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function fields(): Collection |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$fieldsParameterName = config('query-builder.parameters.fields'); |
64
|
|
|
|
65
|
|
|
$fieldsPerTable = collect($this->query($fieldsParameterName)); |
66
|
|
|
|
67
|
|
|
if ($fieldsPerTable->isEmpty()) { |
68
|
|
|
return collect(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $fieldsPerTable->map(function ($fields) { |
72
|
|
|
return explode(',', $fields); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function sorts(): Collection |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$sortParameterName = config('query-builder.parameters.sort'); |
79
|
|
|
|
80
|
|
|
$sortParts = $this->query($sortParameterName); |
81
|
|
|
|
82
|
|
|
if (is_string($sortParts)) { |
83
|
|
|
$sortParts = explode(',', $sortParts); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return collect($sortParts)->filter(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $value |
91
|
|
|
* |
92
|
|
|
* @return array|bool |
93
|
|
|
*/ |
94
|
|
|
protected function getFilterValue($value) |
95
|
|
|
{ |
96
|
|
|
if (is_array($value)) { |
97
|
|
|
return collect($value)->map(function ($valueValue) { |
98
|
|
|
return $this->getFilterValue($valueValue); |
99
|
|
|
})->all(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (Str::contains($value, ',')) { |
103
|
|
|
return $this->explodeIgnoringWhitespace(',', $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($value === 'true') { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($value === 'false') { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $delimiter The boundary string. |
119
|
|
|
* @param string $string The input string |
120
|
|
|
* |
121
|
|
|
* @return array The values between the delimiters in the given string, ignoring any spaced delimiters inside values |
122
|
|
|
*/ |
123
|
|
|
private function explodeIgnoringWhitespace(string $delimiter, string $string): array |
124
|
|
|
{ |
125
|
|
|
return preg_split( |
126
|
|
|
'/'.preg_quote($delimiter).'(?=\S)/', |
127
|
|
|
trim($string, $delimiter), |
128
|
|
|
null, |
129
|
|
|
PREG_SPLIT_NO_EMPTY |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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: