Skip to content

Commit 616cd62

Browse files
authored
Merge pull request #4 from phpsa/analysis-zOjN5l
Apply fixes from StyleCI
2 parents 5f50010 + fbfb731 commit 616cd62

File tree

5 files changed

+146
-159
lines changed

5 files changed

+146
-159
lines changed

src/Http/Controllers/Api/Controller.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,26 @@
33
namespace Phpsa\LaravelApiController\Http\Api;
44

55
use Illuminate\Http\Request;
6-
use Symfony\Component\HttpFoundation\Response;
7-
use Illuminate\Foundation\Bus\DispatchesJobs;
8-
use Illuminate\Foundation\Validation\ValidatesRequests;
9-
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10-
use Illuminate\Routing\Controller as BaseController;
11-
use Illuminate\Support\Facades\Validator;
12-
136
use Illuminate\Support\Facades\Schema;
147
use Illuminate\Database\Eloquent\Model;
15-
16-
use Illuminate\Database\Eloquent\ModelNotFoundException;
8+
use Illuminate\Support\Facades\Validator;
9+
use Phpsa\LaravelApiController\UriParser;
10+
use Illuminate\Foundation\Bus\DispatchesJobs;
11+
use Phpsa\LaravelApiController\Traits\Parser;
12+
use Symfony\Component\HttpFoundation\Response;
13+
use Illuminate\Routing\Controller as BaseController;
14+
use Illuminate\Foundation\Validation\ValidatesRequests;
1715
use Phpsa\LaravelApiController\Exceptions\ApiException;
16+
use Illuminate\Database\Eloquent\ModelNotFoundException;
17+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
1818
use Phpsa\LaravelApiController\Repository\BaseRepository;
19-
20-
use Phpsa\LaravelApiController\UriParser;
2119
use Phpsa\LaravelApiController\Traits\Response as ApiResponse;
22-
use Phpsa\LaravelApiController\Traits\Parser;
2320

2421
/**
2522
* Class Controller.
2623
*/
2724
abstract class Controller extends BaseController
2825
{
29-
3026
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, ApiResponse, Parser;
3127

3228
/**
@@ -72,7 +68,7 @@ abstract class Controller extends BaseController
7268
protected $user;
7369

7470
/**
75-
* Holds the available table columns
71+
* Holds the available table columns.
7672
*
7773
* @var array
7874
*/
@@ -267,7 +263,7 @@ public function create()
267263
*
268264
* @return Response
269265
*/
270-
public function edit(/** @scrutinizer ignore-unused */ $id)
266+
public function edit(/* @scrutinizer ignore-unused */ $id)
271267
{
272268
return $this->errorNotImplemented();
273269
}
@@ -296,7 +292,7 @@ protected function rulesForCreate()
296292
*
297293
* @return array
298294
*/
299-
protected function rulesForUpdate(/** @scrutinizer ignore-unused */ $id)
295+
protected function rulesForUpdate(/* @scrutinizer ignore-unused */ $id)
300296
{
301297
return [];
302298
}

src/Repository/BaseRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function get(array $columns = ['*'])
243243
* @param $id
244244
* @param array $columns
245245
*
246-
* @throws ModelNotFoundException
246+
* @throws ModelNotFoundException
247247
* @return Collection|Model
248248
*/
249249
public function getById($id, array $columns = ['*'])

src/Traits/Parser.php

Lines changed: 118 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
use Phpsa\LaravelApiController\Exceptions\UnknownColumnException;
66

7-
8-
Trait Parser {
9-
10-
/**
7+
trait Parser
8+
{
9+
/**
1110
* Default Fields to response with.
1211
*
1312
* @var array
@@ -36,167 +35,159 @@
3635
*/
3736
protected $maximumLimit = 0;
3837

39-
/**
40-
* Parses our include joins
41-
*
42-
* @return void
43-
*/
44-
protected function parseIncludeParams() : void
38+
/**
39+
* Parses our include joins.
40+
*
41+
* @return void
42+
*/
43+
protected function parseIncludeParams() : void
4544
{
4645
$field = config('laravel-api-controller.parameters.include');
4746

48-
if (empty($field)) {
47+
if (empty($field)) {
4948
return;
50-
}
49+
}
5150

52-
$with = $this->request->input($field);
51+
$with = $this->request->input($field);
5352

5453
if ($with !== null) {
5554
$this->repository->with(explode(',', $with));
5655
}
57-
}
58-
59-
/**
60-
* Parses our sort parameters
61-
*
62-
* @return void
63-
*/
64-
protected function parseSortParams() : void
56+
}
57+
58+
/**
59+
* Parses our sort parameters.
60+
*
61+
* @return void
62+
*/
63+
protected function parseSortParams() : void
6564
{
65+
$sorts = $this->getSortValue();
6666

67-
$sorts = $this->getSortValue();
67+
foreach ($sorts as $sort) {
68+
$sortP = explode(' ', $sort);
69+
$sortF = $sortP[0];
6870

69-
foreach ($sorts as $sort) {
71+
if (empty($sortF) || ! in_array($sortF, $this->tableColumns)) {
72+
continue;
73+
}
7074

71-
$sortP = explode(' ', $sort);
72-
$sortF = $sortP[0];
75+
$sortD = ! empty($sortP[1]) && strtolower($sortP[1]) == 'desc' ? 'desc' : 'asc';
76+
$this->repository->orderBy($sortF, $sortD);
77+
}
78+
}
7379

74-
if (empty($sortF) || ! in_array($sortF, $this->tableColumns)) {
75-
continue;
76-
}
80+
/**
81+
* gets the sort value.
82+
*
83+
* @returns array
84+
*/
85+
protected function getSortValue() : array
86+
{
87+
$field = config('laravel-api-controller.parameters.sort');
88+
$sort = $field && $this->request->has($field) ? $this->request->input($field) : $this->defaultSort;
7789

78-
$sortD = ! empty($sortP[1]) && strtolower($sortP[1]) == 'desc' ? 'desc' : 'asc';
79-
$this->repository->orderBy($sortF, $sortD);
80-
}
81-
}
90+
if (! $sort) {
91+
return [];
92+
}
8293

83-
/**
84-
* gets the sort value
85-
*
86-
* @returns array
87-
*/
88-
protected function getSortValue() : array
89-
{
94+
return is_array($sort) ? $sort : explode(',', $sort);
95+
}
9096

91-
$field = config('laravel-api-controller.parameters.sort');
92-
$sort = $field && $this->request->has($field) ? $this->request->input($field) : $this->defaultSort;
97+
/**
98+
* parses our filter parameters.
99+
*
100+
* @return void
101+
*/
102+
protected function parseFilterParams() : void
103+
{
104+
$where = $this->uriParser->whereParameters();
105+
if (empty($where)) {
106+
return;
107+
}
93108

94-
if(!$sort){
95-
return [];
96-
}
109+
foreach ($where as $whr) {
110+
if (strpos($whr['key'], '.') > 0) {
111+
//@TODO: test if exists in the withs, if not continue out to exclude from the qbuild
112+
//continue;
113+
} elseif (! in_array($whr['key'], $this->tableColumns)) {
114+
continue;
115+
}
97116

98-
return is_array($sort) ? $sort : explode(',', $sort);
99-
}
117+
$this->setWhereClause($whr);
118+
}
119+
}
100120

101-
/**
102-
* parses our filter parameters
103-
*
104-
* @return void
105-
*/
106-
protected function parseFilterParams() : void
121+
/**
122+
* set the Where clause.
123+
*
124+
* @param array $where the where clause
125+
*
126+
* @return void
127+
*/
128+
protected function setWhereClause($where) : void
107129
{
108-
$where = $this->uriParser->whereParameters();
109-
if(empty($where)){
110-
return;
111-
}
112-
113-
foreach ($where as $whr) {
114-
if (strpos($whr['key'], '.') > 0) {
115-
//@TODO: test if exists in the withs, if not continue out to exclude from the qbuild
116-
//continue;
117-
} elseif (! in_array($whr['key'], $this->tableColumns)) {
118-
continue;
119-
}
120-
121-
$this->setWhereClause($whr);
122-
123-
}
124-
125-
}
126-
127-
/**
128-
* set the Where clause
129-
*
130-
* @param array $where the where clause
131-
*
132-
* @return void
133-
*/
134-
protected function setWhereClause($where) : void
135-
{
136-
switch ($where['type']) {
137-
case 'In':
138-
if (! empty($where['values'])) {
139-
$this->repository->whereIn($where['key'], $where['values']);
140-
}
141-
break;
142-
case 'NotIn':
143-
if (! empty($where['values'])) {
144-
$this->repository->whereNotIn($where['key'], $where['values']);
145-
}
146-
break;
147-
case 'Basic':
148-
$this->repository->where($where['key'], $where['value'], $where['operator']);
149-
break;
150-
}
151-
}
152-
153-
/**
154-
* parses the fields to return
155-
*
156-
* @throws UnknownColumnException
157-
* @return array
158-
*/
159-
protected function parseFieldParams() : array
130+
switch ($where['type']) {
131+
case 'In':
132+
if (! empty($where['values'])) {
133+
$this->repository->whereIn($where['key'], $where['values']);
134+
}
135+
break;
136+
case 'NotIn':
137+
if (! empty($where['values'])) {
138+
$this->repository->whereNotIn($where['key'], $where['values']);
139+
}
140+
break;
141+
case 'Basic':
142+
$this->repository->where($where['key'], $where['value'], $where['operator']);
143+
break;
144+
}
145+
}
146+
147+
/**
148+
* parses the fields to return.
149+
*
150+
* @throws UnknownColumnException
151+
* @return array
152+
*/
153+
protected function parseFieldParams() : array
160154
{
161155
$attributes = $this->model->attributesToArray();
162156
$fields = $this->request->has('fields') && ! empty($this->request->input('fields')) ? explode(',', $this->request->input('fields')) : $this->defaultFields;
163157
foreach ($fields as $k => $field) {
164-
if (
165-
$field === '*' ||
166-
in_array($field, $this->tableColumns) ||
167-
array_key_exists($field, $attributes)
168-
) {
158+
if (
159+
$field === '*' ||
160+
in_array($field, $this->tableColumns) ||
161+
array_key_exists($field, $attributes)
162+
) {
169163
continue;
170164
}
171165
if (strpos($field, '.') > 0) {
172166
//@TODO check if mapped field exists
173167
//@todo
174168
unset($fields[$k]);
175169
continue;
176-
}
177-
178-
unset($fields[$k]);
170+
}
179171

172+
unset($fields[$k]);
180173
}
181174

182175
return $fields;
183-
}
184-
185-
/**
186-
* parses the limit value
187-
*
188-
* @return int
189-
*/
190-
protected function parseLimitParams() : int
191-
{
176+
}
192177

193-
$limit = $this->request->has('limit') ? intval($this->request->input('limit')) : $this->defaultLimit;
178+
/**
179+
* parses the limit value.
180+
*
181+
* @return int
182+
*/
183+
protected function parseLimitParams() : int
184+
{
185+
$limit = $this->request->has('limit') ? intval($this->request->input('limit')) : $this->defaultLimit;
194186

195-
if ($this->maximumLimit && ($limit > $this->maximumLimit || ! $limit)) {
187+
if ($this->maximumLimit && ($limit > $this->maximumLimit || ! $limit)) {
196188
$limit = $this->maximumLimit;
197189
}
198190

199-
return $limit;
200-
}
201-
202-
}
191+
return $limit;
192+
}
193+
}

0 commit comments

Comments
 (0)