Skip to content

Commit

Permalink
fix code style and structure problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ngabor84 committed Dec 4, 2019
1 parent 140de62 commit 2bbd7e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
18 changes: 11 additions & 7 deletions src/BuilderParamsApplierTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ public function applyParams(Builder $query, RequestParamsInterface $params): Len
$query->limit($pagination->getLimit());
$query->offset($pagination->getPage() * $pagination->getLimit());

$paginator = $query->paginate($params->getPagination()->getLimit(), ['*'], 'page', $params->getPagination()->getPage());
$paginator = $query->paginate(
$params->getPagination()->getLimit(),
['*'],
'page',
$params->getPagination()->getPage()
);
} else {
$paginator = $query->paginate($query->count(), ['*'], 'page', 1);
}


return $paginator;
}

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

switch ($operator) {
case 'ct':
Expand All @@ -67,7 +70,7 @@ protected function applyFilter(Builder $query, Filter $filter): void
$clauseOperator = 'NOT LIKE';
break;
case 'sw':
$value = $value . '%';
$value .= '%';
$clauseOperator = 'LIKE';
break;
case 'ew':
Expand Down Expand Up @@ -99,9 +102,10 @@ protected function applyFilter(Builder $query, Filter $filter): void
if ($operator === 'in') {
$query->whereIn($filter, explode('|', $value));
} else {
call_user_func_array([$query, $method], [
$field, $clauseOperator, $value
]);
call_user_func_array(
[$query, $method],
[$field, $clauseOperator, $value]
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Params/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Pagination implements PaginationInterface
protected $limit;
protected $page;

public function __construct(int $limit = null, int $page = null)
public function __construct(?int $limit = null, ?int $page = null)
{
$this->setLimit($limit);
$this->setPage($page);
Expand Down
30 changes: 15 additions & 15 deletions src/RequestQueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ public function parse(Request $request): RequestParamsInterface

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

if (count($filterDatas) < 3) {
throw new UnprocessableEntityHttpException('Filter must contains field and value!');
}
list($field, $operator, $value) = $filterDatas;
foreach ($filters as $filter) {
$filterDatas = explode(':', $filter, 3);

$this->requestParams->addFilter(new Filter($field, $operator, $value));
if (count($filterDatas) < 3) {
throw new UnprocessableEntityHttpException('Filter must contains field and value!');
}
[$field, $operator, $value] = $filterDatas;

$this->requestParams->addFilter(new Filter($field, $operator, $value));
}
}

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

if (empty($field)) {
throw new UnprocessableEntityHttpException('Sort must contains field!');
}
foreach ($sorts as $sort) {
[$field, $direction] = explode(':', $sort);

$this->requestParams->addSort(new Sort($field, $direction));
if ($field === '') {
throw new UnprocessableEntityHttpException('Sort must contains field!');
}

$this->requestParams->addSort(new Sort($field, $direction));
}
}

Expand Down

0 comments on commit 2bbd7e6

Please sign in to comment.