Skip to content

Commit 6753cfe

Browse files
committed
Some code tidy
1 parent ad02b87 commit 6753cfe

12 files changed

+350
-237
lines changed

src/Contracts/Parser.php

+6-139
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ protected function getUriParser($request)
2727
return $this->uriParser;
2828
}
2929

30-
31-
3230
/**
3331
* Method to add extra request parameters to the request instance.
3432
*
@@ -44,54 +42,7 @@ protected function addCustomParams($request, array $extraParams = []): void
4442
$request->replace($new);
4543
}
4644

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);
7245

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-
}
9546

9647
/**
9748
* Parses our sort parameters.
@@ -130,7 +81,7 @@ protected function parseJoinSorts(Collection $sorts)
13081
$currentTable = self::$model->getTable();
13182

13283
$fields = array_map(function ($field) use ($currentTable) {
133-
return $currentTable.'.'.$field;
84+
return $currentTable . '.' . $field;
13485
}, $this->parseFieldParams());
13586

13687
$this->builder->select($fields);
@@ -154,7 +105,7 @@ protected function parseJoinSorts(Collection $sorts)
154105

155106
$withTable = $relation->getRelated()->getTable();
156107

157-
$withTableName = strpos($withTable, '.') === false ? $withConnection.'.'.$withTable : $withTable;
108+
$withTableName = strpos($withTable, '.') === false ? $withConnection . '.' . $withTable : $withTable;
158109

159110
$this->builder->leftJoin($withTableName, "{$withTableName}.{$foreignKey}", "{$currentTable}.{$localKey}");
160111
$this->builder->orderBy("{$withTableName}.{$key}", $sortD);
@@ -200,68 +151,11 @@ protected function parseFilterParams(): void
200151
} elseif (! in_array($whr['key'], $tableColumns)) {
201152
continue;
202153
}
203-
$this->setQueryBuilderWhereStatement($this->builder, $table.'.'.$whr['key'], $whr);
154+
$this->setQueryBuilderWhereStatement($this->builder, $table . '.' . $whr['key'], $whr);
204155
}
205156
}
206157

207-
protected function setWhereHasClause(array $where): void
208-
{
209-
[$with, $key] = explode('.', $where['key']);
210158

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-
}
265159

266160
/**
267161
* parses the fields to return.
@@ -271,7 +165,9 @@ protected function setQueryBuilderWhereStatement($query, $key, $where): void
271165
protected function parseFieldParams(): array
272166
{
273167
/** @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());
275171

276172
/** @scrutinizer ignore-call */
277173
$tableColumns = $this->getTableColumns();
@@ -285,36 +181,7 @@ protected function parseFieldParams(): array
285181
return $fields;
286182
}
287183

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]);
309184

310-
continue;
311-
}
312-
313-
$fields[$key] = str_replace(Helpers::snake($include).'.', '', $field);
314-
}
315-
316-
return $fields;
317-
}
318185

319186
/**
320187
* parses the limit value.

src/Contracts/Relationships.php

-61
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,6 @@
88

99
trait Relationships
1010
{
11-
/**
12-
* Gets whitelisted methods.
13-
*
14-
* @return array
15-
*/
16-
protected function getIncludesWhitelist(): array
17-
{
18-
return is_array($this->includesWhitelist) ? $this->includesWhitelist : [];
19-
}
20-
21-
/**
22-
* Gets blacklisted methods.
23-
*
24-
* @return array
25-
*/
26-
protected function getIncludesBlacklist(): array
27-
{
28-
return is_array($this->includesBlacklist) ? $this->includesBlacklist : [];
29-
}
30-
31-
/**
32-
* is method blacklisted.
33-
*
34-
* @param string $item
35-
*
36-
* @return bool
37-
*/
38-
public function isBlacklisted($item)
39-
{
40-
return in_array($item, $this->getIncludesBlacklist()) || $this->getIncludesBlacklist() === ['*'];
41-
}
42-
43-
/**
44-
* filters the allowed includes and returns only the ones that are allowed.
45-
*
46-
* @param array $includes
47-
*
48-
* @return array
49-
*/
50-
protected function filterAllowedIncludes(array $includes): array
51-
{
52-
return array_filter(Helpers::camelCaseArray($includes), function ($item) {
53-
$callable = method_exists(self::$model, $item);
54-
55-
if (! $callable) {
56-
return false;
57-
}
58-
59-
//check if in the allowed includes array:
60-
if (in_array($item, Helpers::camelCaseArray($this->getIncludesWhitelist()))) {
61-
return true;
62-
}
63-
64-
if ($this->isBlacklisted($item) || $this->isBlacklisted(Helpers::snake($item))) {
65-
return false;
66-
}
67-
68-
return empty($this->getIncludesWhitelist()) && ! Str::startsWith($item, '_');
69-
});
70-
}
71-
7211
/**
7312
* Method used to store related.
7413
*

src/Generator/ApiControllerMakeCommand.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function confirmModelExists()
8686
$modelClass = $this->parseModel(/** @scrutinizer ignore-type */ $this->option('model'));
8787

8888
if (! class_exists($modelClass, false)) {
89-
if ($this->confirm("A {$modelClass} model does not exist!! Do you want to generate it?", true)) {
89+
if ($this->confirm("A {$modelClass} model does not exist! Do you want to generate it?", true)) {
9090
$this->call('make:api:model', ['name' => $modelClass]);
9191
}
9292

@@ -228,16 +228,16 @@ protected function addRoutes()
228228
$stub
229229
);
230230

231-
// read file
232-
$lines = file($routesFile);
233-
234-
if (! $lines) {
235-
$this->error('could not read routes file, add the following toyour routes file:');
231+
if (! File::exists($routesFile)) {
232+
$this->error('could not read routes file, add the following to your routes file:');
236233
$this->info("\n".$stub."\n");
237234

238235
return false;
239236
}
240237

238+
// read file
239+
$lines = file($routesFile);
240+
241241
$lastLine = trim($lines[count($lines) - 1]);
242242
// modify file
243243
if (strcmp($lastLine, '});') === 0) {
@@ -250,7 +250,7 @@ protected function addRoutes()
250250
$fileResource = fopen($routesFile, 'w');
251251

252252
if (! is_resource($fileResource)) {
253-
$this->error('could not read routes file, add the following toyour routes file:');
253+
$this->error('could not read routes file, add the following to your routes file:');
254254
$this->info("\n".$stub."\n");
255255

256256
return false;

0 commit comments

Comments
 (0)