@@ -27,8 +27,6 @@ protected function getUriParser($request)
27
27
return $ this ->uriParser ;
28
28
}
29
29
30
-
31
-
32
30
/**
33
31
* Method to add extra request parameters to the request instance.
34
32
*
@@ -44,54 +42,7 @@ protected function addCustomParams($request, array $extraParams = []): void
44
42
$ request ->replace ($ new );
45
43
}
46
44
47
- /**
48
- * Parses our include joins.
49
- */
50
- protected function parseIncludeParams (): void
51
- {
52
- $ field = config ('laravel-api-controller.parameters.include ' );
53
-
54
- if (empty ($ field )) {
55
- return ;
56
- }
57
-
58
- $ includes = $ this ->request ->input ($ field );
59
-
60
- if (empty ($ includes )) {
61
- return ;
62
- }
63
-
64
- $ withs = array_flip (
65
- $ this ->/** @scrutinizer ignore-call */ filterAllowedIncludes (explode (', ' , $ includes ))
66
- );
67
-
68
- foreach ($ withs as $ with => $ idx ) {
69
- /** @scrutinizer ignore-call */
70
- $ sub = $ this ->getRelatedModel ($ with );
71
- $ fields = $ this ->getIncludesFields ($ with );
72
45
73
- $ where = array_filter ($ this ->uriParser ->whereParameters (), function ($ where ) use ($ with ) {
74
- return strpos ($ where ['key ' ], Helpers::snake ($ with ).'. ' ) !== false ;
75
- });
76
-
77
- if (! empty ($ fields )) {
78
- $ fields [] = $ sub ->getKeyName ();
79
- }
80
-
81
- if (! empty ($ where )) {
82
- $ where = array_map (function ($ whr ) use ($ with , $ sub ) {
83
- $ key = str_replace (Helpers::snake ($ with ).'. ' , '' , $ whr ['key ' ]);
84
- $ whr ['key ' ] = $ sub ->qualifyColumn ($ key );
85
-
86
- return $ whr ;
87
- }, $ where );
88
- }
89
-
90
- $ withs [$ with ] = $ this ->setWithQuery ($ where , $ fields );
91
- }
92
-
93
- $ this ->builder ->with ($ withs );
94
- }
95
46
96
47
/**
97
48
* Parses our sort parameters.
@@ -130,7 +81,7 @@ protected function parseJoinSorts(Collection $sorts)
130
81
$ currentTable = self ::$ model ->getTable ();
131
82
132
83
$ fields = array_map (function ($ field ) use ($ currentTable ) {
133
- return $ currentTable. '. ' . $ field ;
84
+ return $ currentTable . '. ' . $ field ;
134
85
}, $ this ->parseFieldParams ());
135
86
136
87
$ this ->builder ->select ($ fields );
@@ -154,7 +105,7 @@ protected function parseJoinSorts(Collection $sorts)
154
105
155
106
$ withTable = $ relation ->getRelated ()->getTable ();
156
107
157
- $ withTableName = strpos ($ withTable , '. ' ) === false ? $ withConnection. '. ' . $ withTable : $ withTable ;
108
+ $ withTableName = strpos ($ withTable , '. ' ) === false ? $ withConnection . '. ' . $ withTable : $ withTable ;
158
109
159
110
$ this ->builder ->leftJoin ($ withTableName , "{$ withTableName }. {$ foreignKey }" , "{$ currentTable }. {$ localKey }" );
160
111
$ this ->builder ->orderBy ("{$ withTableName }. {$ key }" , $ sortD );
@@ -200,68 +151,11 @@ protected function parseFilterParams(): void
200
151
} elseif (! in_array ($ whr ['key ' ], $ tableColumns )) {
201
152
continue ;
202
153
}
203
- $ this ->setQueryBuilderWhereStatement ($ this ->builder , $ table. '. ' . $ whr ['key ' ], $ whr );
154
+ $ this ->setQueryBuilderWhereStatement ($ this ->builder , $ table . '. ' . $ whr ['key ' ], $ whr );
204
155
}
205
156
}
206
157
207
- protected function setWhereHasClause (array $ where ): void
208
- {
209
- [$ with , $ key ] = explode ('. ' , $ where ['key ' ]);
210
158
211
- /** @scrutinizer ignore-call */
212
- $ sub = $ this ->getRelatedModel ($ with );
213
- /** @scrutinizer ignore-call */
214
- $ fields = $ this ->getTableColumns ($ sub );
215
-
216
- if (! in_array ($ key , $ fields )) {
217
- return ;
218
- }
219
- $ subKey = $ sub ->qualifyColumn ($ key );
220
-
221
- $ this ->builder ->whereHas (Helpers::camel ($ with ), function ($ q ) use ($ where , $ subKey ) {
222
- $ this ->setQueryBuilderWhereStatement ($ q , $ subKey , $ where );
223
- });
224
- }
225
-
226
- protected function setWithQuery (?array $ where = null , ?array $ fields = null ): callable
227
- {
228
- //dd($fields);
229
- return function ($ query ) use ($ where , $ fields ) {
230
- if ($ fields !== null && count ($ fields ) > 0 ) {
231
- $ query ->select (array_unique ($ fields ));
232
- }
233
-
234
- if ($ where !== null && count ($ where ) > 0 ) {
235
- foreach ($ where as $ whr ) {
236
- $ this ->setQueryBuilderWhereStatement ($ query , $ whr ['key ' ], $ whr );
237
- }
238
- }
239
- };
240
- }
241
-
242
- protected function setQueryBuilderWhereStatement ($ query , $ key , $ where ): void
243
- {
244
- switch ($ where ['type ' ]) {
245
- case 'In ' :
246
- if (! empty ($ where ['values ' ])) {
247
- $ query ->whereIn ($ key , $ where ['values ' ]);
248
- }
249
- break ;
250
- case 'NotIn ' :
251
- if (! empty ($ where ['values ' ])) {
252
- $ query ->whereNotIn ($ key , $ where ['values ' ]);
253
- }
254
- break ;
255
- case 'Basic ' :
256
- if ($ where ['value ' ] !== 'NULL ' ) {
257
- $ query ->where ($ key , $ where ['operator ' ], $ where ['value ' ]);
258
-
259
- return ;
260
- }
261
-
262
- $ where ['operator ' ] === '= ' ? $ query ->whereNull ($ key ) : $ query ->whereNotNull ($ key );
263
- }
264
- }
265
159
266
160
/**
267
161
* parses the fields to return.
@@ -271,7 +165,9 @@ protected function setQueryBuilderWhereStatement($query, $key, $where): void
271
165
protected function parseFieldParams (): array
272
166
{
273
167
/** @scrutinizer ignore-call */
274
- $ fields = Helpers::filterFieldsFromRequest ($ this ->request , $ this ->/** @scrutinizer ignore-call */ getDefaultFields ());
168
+ $ fields = Helpers::filterFieldsFromRequest ($ this ->request , $ this ->
169
+ /** @scrutinizer ignore-call */
170
+ getDefaultFields ());
275
171
276
172
/** @scrutinizer ignore-call */
277
173
$ tableColumns = $ this ->getTableColumns ();
@@ -285,36 +181,7 @@ protected function parseFieldParams(): array
285
181
return $ fields ;
286
182
}
287
183
288
- /**
289
- * Parses an includes fields and returns as an array.
290
- *
291
- * @param string $include - the table definer
292
- *
293
- * @return array
294
- */
295
- protected function getIncludesFields (string $ include ): array
296
- {
297
- /** @scrutinizer ignore-call */
298
- $ fields = Helpers::filterFieldsFromRequest ($ this ->request , $ this ->/** @scrutinizer ignore-call */ getDefaultFields ());
299
-
300
- $ relation = self ::$ model ->{$ include }();
301
- $ type = $ relation ->getRelated ();
302
- /** @scrutinizer ignore-call */
303
- $ tableColumns = $ this ->getTableColumns ($ type );
304
-
305
- foreach ($ fields as $ key => $ field ) {
306
- $ parts = explode ('. ' , $ field );
307
- if (strpos ($ field , Helpers::snake ($ include ).'. ' ) === false || ! in_array (end ($ parts ), $ tableColumns )) {
308
- unset($ fields [$ key ]);
309
184
310
- continue ;
311
- }
312
-
313
- $ fields [$ key ] = str_replace (Helpers::snake ($ include ).'. ' , '' , $ field );
314
- }
315
-
316
- return $ fields ;
317
- }
318
185
319
186
/**
320
187
* parses the limit value.
0 commit comments