BookStack/app/Api/ListingResponseBuilder.php

145 lines
3.9 KiB
PHP
Raw Normal View History

2021-06-26 17:23:15 +02:00
<?php
namespace BookStack\Api;
2019-12-28 15:58:07 +01:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
2019-12-28 15:58:07 +01:00
class ListingResponseBuilder
{
protected $query;
protected $request;
2019-12-28 15:58:07 +01:00
protected $fields;
protected $hiddenFields;
2019-12-28 15:58:07 +01:00
protected $filterOperators = [
'eq' => '=',
'ne' => '!=',
'gt' => '>',
'lt' => '<',
'gte' => '>=',
'lte' => '<=',
2021-06-26 17:23:15 +02:00
'like' => 'like',
];
2019-12-28 15:58:07 +01:00
/**
* ListingResponseBuilder constructor.
*/
public function __construct(Builder $query, Request $request, array $fields, array $hiddenFields )
2019-12-28 15:58:07 +01:00
{
$this->query = $query;
$this->request = $request;
2019-12-28 15:58:07 +01:00
$this->fields = $fields;
$this->hiddenFields = $hiddenFields;
2019-12-28 15:58:07 +01:00
}
/**
* Get the response from this builder.
*/
public function toResponse()
{
$filteredQuery = $this->filterQuery($this->query);
$total = $filteredQuery->count();
$data = $this->fetchData($filteredQuery);
$data = $data->makeVisible($this->hiddenFields);
2019-12-28 15:58:07 +01:00
return response()->json([
2021-06-26 17:23:15 +02:00
'data' => $data,
2019-12-28 15:58:07 +01:00
'total' => $total,
]);
}
/**
* Fetch the data to return in the response.
*/
protected function fetchData(Builder $query): Collection
2019-12-28 15:58:07 +01:00
{
$query = $this->countAndOffsetQuery($query);
$query = $this->sortQuery($query);
2021-06-26 17:23:15 +02:00
return $query->get($this->fields);
2019-12-28 15:58:07 +01:00
}
/**
* Apply any filtering operations found in the request.
*/
protected function filterQuery(Builder $query): Builder
{
$query = clone $query;
$requestFilters = $this->request->get('filter', []);
if (!is_array($requestFilters)) {
return $query;
}
$queryFilters = collect($requestFilters)->map(function ($value, $key) {
return $this->requestFilterToQueryFilter($key, $value);
})->filter(function ($value) {
return !is_null($value);
})->values()->toArray();
return $query->where($queryFilters);
}
/**
* Convert a request filter query key/value pair into a [field, op, value] where condition.
*/
protected function requestFilterToQueryFilter($fieldKey, $value): ?array
{
$splitKey = explode(':', $fieldKey);
$field = $splitKey[0];
$filterOperator = $splitKey[1] ?? 'eq';
if (!in_array($field, $this->fields)) {
return null;
}
if (!in_array($filterOperator, array_keys($this->filterOperators))) {
$filterOperator = 'eq';
}
$queryOperator = $this->filterOperators[$filterOperator];
2021-06-26 17:23:15 +02:00
return [$field, $queryOperator, $value];
}
2019-12-28 15:58:07 +01:00
/**
* Apply sorting operations to the query from given parameters
* otherwise falling back to the first given field, ascending.
*/
protected function sortQuery(Builder $query): Builder
2019-12-28 15:58:07 +01:00
{
$query = clone $query;
2019-12-28 15:58:07 +01:00
$defaultSortName = $this->fields[0];
$direction = 'asc';
$sort = $this->request->get('sort', '');
2019-12-28 15:58:07 +01:00
if (strpos($sort, '-') === 0) {
$direction = 'desc';
}
$sortName = ltrim($sort, '+- ');
if (!in_array($sortName, $this->fields)) {
$sortName = $defaultSortName;
}
return $query->orderBy($sortName, $direction);
2019-12-28 15:58:07 +01:00
}
/**
* Apply count and offset for paging, based on params from the request while falling
* back to system defined default, taking the max limit into account.
*/
protected function countAndOffsetQuery(Builder $query): Builder
2019-12-28 15:58:07 +01:00
{
$query = clone $query;
$offset = max(0, $this->request->get('offset', 0));
2019-12-28 15:58:07 +01:00
$maxCount = config('api.max_item_count');
$count = $this->request->get('count', config('api.default_item_count'));
2019-12-28 15:58:07 +01:00
$count = max(min($maxCount, $count), 1);
return $query->skip($offset)->take($count);
2019-12-28 15:58:07 +01:00
}
}