Skip to content

Commit 2bbd7e6

Browse files
committed
fix code style and structure problems
1 parent 140de62 commit 2bbd7e6

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

src/BuilderParamsApplierTrait.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ public function applyParams(Builder $query, RequestParamsInterface $params): Len
3838
$query->limit($pagination->getLimit());
3939
$query->offset($pagination->getPage() * $pagination->getLimit());
4040

41-
$paginator = $query->paginate($params->getPagination()->getLimit(), ['*'], 'page', $params->getPagination()->getPage());
41+
$paginator = $query->paginate(
42+
$params->getPagination()->getLimit(),
43+
['*'],
44+
'page',
45+
$params->getPagination()->getPage()
46+
);
4247
} else {
4348
$paginator = $query->paginate($query->count(), ['*'], 'page', 1);
4449
}
4550

46-
4751
return $paginator;
4852
}
4953

@@ -55,7 +59,6 @@ protected function applyFilter(Builder $query, Filter $filter): void
5559
$value = $filter->getValue();
5660
$method = 'where';
5761
$clauseOperator = null;
58-
$databaseField = null;
5962

6063
switch ($operator) {
6164
case 'ct':
@@ -67,7 +70,7 @@ protected function applyFilter(Builder $query, Filter $filter): void
6770
$clauseOperator = 'NOT LIKE';
6871
break;
6972
case 'sw':
70-
$value = $value . '%';
73+
$value .= '%';
7174
$clauseOperator = 'LIKE';
7275
break;
7376
case 'ew':
@@ -99,9 +102,10 @@ protected function applyFilter(Builder $query, Filter $filter): void
99102
if ($operator === 'in') {
100103
$query->whereIn($filter, explode('|', $value));
101104
} else {
102-
call_user_func_array([$query, $method], [
103-
$field, $clauseOperator, $value
104-
]);
105+
call_user_func_array(
106+
[$query, $method],
107+
[$field, $clauseOperator, $value]
108+
);
105109
}
106110
}
107111

src/Params/Pagination.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Pagination implements PaginationInterface
77
protected $limit;
88
protected $page;
99

10-
public function __construct(int $limit = null, int $page = null)
10+
public function __construct(?int $limit = null, ?int $page = null)
1111
{
1212
$this->setLimit($limit);
1313
$this->setPage($page);

src/RequestQueryParser.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,32 @@ public function parse(Request $request): RequestParamsInterface
3232

3333
protected function parseFilters(Request $request): void
3434
{
35-
if ($request->has('filter')) {
36-
foreach ($request->get('filter') as $filter) {
37-
$filterDatas = explode(':', $filter, 3);
35+
$filters = $request->has('filter') ? $request->get('filter') : [];
3836

39-
if (count($filterDatas) < 3) {
40-
throw new UnprocessableEntityHttpException('Filter must contains field and value!');
41-
}
42-
list($field, $operator, $value) = $filterDatas;
37+
foreach ($filters as $filter) {
38+
$filterDatas = explode(':', $filter, 3);
4339

44-
$this->requestParams->addFilter(new Filter($field, $operator, $value));
40+
if (count($filterDatas) < 3) {
41+
throw new UnprocessableEntityHttpException('Filter must contains field and value!');
4542
}
43+
[$field, $operator, $value] = $filterDatas;
44+
45+
$this->requestParams->addFilter(new Filter($field, $operator, $value));
4646
}
4747
}
4848

4949
protected function parseSort(Request $request): void
5050
{
51-
if ($request->has('sort')) {
52-
foreach ($request->get('sort') as $sort) {
53-
list($field, $direction) = explode(':', $sort);
51+
$sorts = $request->has('sort') ? $request->get('sort') : [];
5452

55-
if (empty($field)) {
56-
throw new UnprocessableEntityHttpException('Sort must contains field!');
57-
}
53+
foreach ($sorts as $sort) {
54+
[$field, $direction] = explode(':', $sort);
5855

59-
$this->requestParams->addSort(new Sort($field, $direction));
56+
if ($field === '') {
57+
throw new UnprocessableEntityHttpException('Sort must contains field!');
6058
}
59+
60+
$this->requestParams->addSort(new Sort($field, $direction));
6161
}
6262
}
6363

0 commit comments

Comments
 (0)