Skip to content

Commit 5f50010

Browse files
committed
Refactor to improve Code quality & updated docs
1 parent a8feed5 commit 5f50010

File tree

4 files changed

+117
-76
lines changed

4 files changed

+117
-76
lines changed

README.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ You can override the methods by simply putting in your own methods to override -
4040

4141
For the get command you can filter by using the following url patterns
4242

43-
| Seperator | Description | Example | Result |
44-
| --- | --- | --- | --- |
45-
| *`=`* | Equals | ?field=hello | select ... where field = 'hello' |
46-
| *`!=`* | Not Equals | ?field!=hello | select ... where field != 'hello' |
47-
| *`<>`* | Not Equals (alt) | ?field<>hello | select ... where field != 'hello' |
48-
| *`>`* | Greater Than | ?field>5 | select ... where field > 5 |
49-
| *`>=`* | Greater Or Equal to | ?field=>5 | select ... where field >= 5 |
50-
| *`<`* | Less Than | ?field<5 | select ... where field <> 5 |
51-
| *`<=`* | Less Or Equal to | ?field=<5 | select ... where field <= 5 |
52-
| *`~`* | Contains (LIKE with wildcard on both sides)| ?field~hello | select ... where field like '%hello%' |
53-
| *`^`* | Starts with (LIKE with wildcard on end)| ?field^hello | select ... where field like 'hello%' |
54-
| *`$`* | Ends with (LIKE with wildcard on start)| ?field$hello | select ... where field like 'hello%' |
55-
| *`!~`* | Not Contains (LIKE with wildcard on both sides)| ?field!~hello | select ... where field not like '%hello%' |
56-
| *`!^`* | Not Starts with (LIKE with wildcard on end)| ?field!^hello | select ... where field not like 'hello%' |
57-
| *`!$`* | Not Ends with (LIKE with wildcard on start)| ?field!$hello | select ... where field not like 'hello%' |
43+
| Seperator | Description | Example | Result |
44+
| --- | --- | --- | --- |
45+
| *`=`* | Equals | ?filter[field]=hello | select ... where field = 'hello' |
46+
| *`!=`* | Not Equals | ?filter[field!]=hello | select ... where field != 'hello' |
47+
| *`<>`* | Not Equals (alt) | ?filter[field<>]=hello | select ... where field != 'hello' |
48+
| *`>`* | Greater Than | ?filter[field>]=5 | select ... where field > 5 |
49+
| *`>=`* | Greater Or Equal to | ?filter[field=>]=5 | select ... where field >= 5 |
50+
| *`<`* | Less Than | ?filter[field<]=5 | select ... where field <> 5 |
51+
| *`<=`* | Less Or Equal to | ?filter[field=<]=5 | select ... where field <= 5 |
52+
| *`~`* | Contains (LIKE with wildcard on both sides) | ?filter[field~]=hello | select ... where field like '%hello%' |
53+
| *`^`* | Starts with (LIKE with wildcard on end) | ?filter[field^]=hello | select ... where field like 'hello%' |
54+
| *`$`* | Ends with (LIKE with wildcard on start) | ?filter[field$]=hello | select ... where field like 'hello%' |
55+
| *`!~`* | Not Contains (LIKE with wildcard on both sides) | ?filter[field!~]=hello | select ... where field not like '%hello%' |
56+
| *`!^`* | Not Starts with (LIKE with wildcard on end) | ?filter[field!^]=hello | select ... where field not like 'hello%' |
57+
| *`!$`* | Not Ends with (LIKE with wildcard on start) | ?filter[field!$]=hello | select ... where field not like 'hello%' |
5858

5959

6060
# Fields, Relationships, Sorting & Pagination
@@ -80,8 +80,28 @@ By default all fields are returned, you can limit that to specific fields in the
8080
* pagination can also be passed via the url using `limit=xx&page=y`
8181
* pagination can also be limited to a max per page by overriding the `protected $maximumLimit = false;` parameter
8282

83+
## Validation
84+
* When Posting a new record, validation can be done by adding a `rulesForCreate` method to your controller returning an array eg
85+
```php
86+
[
87+
'email' => 'required|email',
88+
'games' => 'required|numeric',
89+
]
90+
```
91+
see https://laravel.com/docs/5.8/validation#conditionally-adding-rules
92+
* for updating a record, add a method `rulesForUpdate` per above.
93+
94+
## Defaults
8395

96+
The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:
8497

98+
* `protected $resourceKeySingular = 'data';` Resource key for an item.
99+
* `protected $resourceKeyPlural = 'data';` Resource key for a collection.
100+
* `protected $defaultFields = ['*'];` Default Fields to respond with
101+
* `protected $defaultSort = null;` Set the default sorting for queries.
102+
* `protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
103+
* `protected $maximumLimit = 0;` Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
104+
* `protected $unguard = false;` Do we need to unguard the model before create/update?
85105

86106
## Security
87107

src/Http/Controllers/Api/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Phpsa\LaravelApiController\Http\Api;
44

55
use Illuminate\Http\Request;
6-
use Illuminate\Http\Response;
6+
use Symfony\Component\HttpFoundation\Response;
77
use Illuminate\Foundation\Bus\DispatchesJobs;
88
use Illuminate\Foundation\Validation\ValidatesRequests;
99
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

src/Traits/Parser.php

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,39 @@ protected function parseIncludeParams() : void
6363
*/
6464
protected function parseSortParams() : void
6565
{
66-
$field = config('laravel-api-controller.parameters.sort');
67-
$sort = $field && $this->request->has($field) ? $this->request->input($field) : $this->defaultSort;
6866

69-
if ($sort) {
70-
$sorts = is_array($sort) ? $sort : explode(',', $sort);
71-
if (empty($sorts)) {
72-
return;
73-
}
67+
$sorts = $this->getSortValue();
7468

75-
foreach ($sorts as $sort) {
76-
if (empty($sort)) {
77-
continue;
78-
}
79-
$sortP = explode(' ', $sort);
69+
foreach ($sorts as $sort) {
8070

81-
$sortF = $sortP[0];
71+
$sortP = explode(' ', $sort);
72+
$sortF = $sortP[0];
8273

83-
if (! in_array($sortF, $this->tableColumns)) {
84-
continue;
85-
}
74+
if (empty($sortF) || ! in_array($sortF, $this->tableColumns)) {
75+
continue;
76+
}
8677

87-
$sortD = ! empty($sortP[1]) && strtolower($sortP[1]) == 'asc' ? 'asc' : 'desc';
88-
$this->repository->orderBy($sortF, $sortD);
89-
}
90-
}
78+
$sortD = ! empty($sortP[1]) && strtolower($sortP[1]) == 'desc' ? 'desc' : 'asc';
79+
$this->repository->orderBy($sortF, $sortD);
80+
}
81+
}
82+
83+
/**
84+
* gets the sort value
85+
*
86+
* @returns array
87+
*/
88+
protected function getSortValue() : array
89+
{
90+
91+
$field = config('laravel-api-controller.parameters.sort');
92+
$sort = $field && $this->request->has($field) ? $this->request->input($field) : $this->defaultSort;
93+
94+
if(!$sort){
95+
return [];
96+
}
97+
98+
return is_array($sort) ? $sort : explode(',', $sort);
9199
}
92100

93101
/**
@@ -97,35 +105,49 @@ protected function parseSortParams() : void
97105
*/
98106
protected function parseFilterParams() : void
99107
{
100-
$where = $this->uriParser->whereParameters();
101-
if (! empty($where)) {
102-
foreach ($where as $whr) {
103-
if (strpos($whr['key'], '.') > 0) {
104-
//test if exists in the withs, if not continue out to exclude from the qbuild
105-
//continue;
106-
} else {
107-
if (! in_array($whr['key'], $this->tableColumns)) {
108-
continue;
109-
}
110-
}
111-
switch ($whr['type']) {
112-
case 'In':
113-
if (! empty($whr['values'])) {
114-
$this->repository->whereIn($whr['key'], $whr['values']);
115-
}
116-
break;
117-
case 'NotIn':
118-
if (! empty($whr['values'])) {
119-
$this->repository->whereNotIn($whr['key'], $whr['values']);
120-
}
121-
break;
122-
case 'Basic':
123-
$this->repository->where($whr['key'], $whr['value'], $whr['operator']);
124-
125-
break;
126-
}
127-
}
128-
}
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+
}
129151
}
130152

131153
/**
@@ -139,23 +161,22 @@ protected function parseFieldParams() : array
139161
$attributes = $this->model->attributesToArray();
140162
$fields = $this->request->has('fields') && ! empty($this->request->input('fields')) ? explode(',', $this->request->input('fields')) : $this->defaultFields;
141163
foreach ($fields as $k => $field) {
142-
if ($field === '*') {
164+
if (
165+
$field === '*' ||
166+
in_array($field, $this->tableColumns) ||
167+
array_key_exists($field, $attributes)
168+
) {
143169
continue;
144170
}
145171
if (strpos($field, '.') > 0) {
146-
//check if mapped field exists
172+
//@TODO check if mapped field exists
147173
//@todo
148174
unset($fields[$k]);
149175
continue;
150-
}
151-
if (! in_array($field, $this->tableColumns)) {
152-
//does the attribute exist ?
176+
}
177+
178+
unset($fields[$k]);
153179

154-
if (! array_key_exists($field, $attributes)) {
155-
throw new UnknownColumnException($field.' does not exist in table');
156-
}
157-
unset($fields[$k]);
158-
}
159180
}
160181

161182
return $fields;

src/Traits/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Phpsa\LaravelApiController\Traits;
44

5-
use Illuminate\Http\Response as Res;
5+
use Symfony\Component\HttpFoundation\Response as Res;
66
use Illuminate\Pagination\LengthAwarePaginator;
77

88
Trait Response {

0 commit comments

Comments
 (0)