Skip to content

Commit

Permalink
Merge branch 'master' of github.com:laramore/requests
Browse files Browse the repository at this point in the history
  • Loading branch information
NastuzziSamy committed Apr 26, 2022
2 parents c6556ee + 0031208 commit e47256d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 42 deletions.
20 changes: 19 additions & 1 deletion src/Eloquent/FilterMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class FilterMeta implements BuilderFilter, CollectionFilter, ModelFilter, Relate
*/
protected $filters = [];

/**
* Paginate.
*
* @var bool
*/
protected $paginate = false;

public function __construct($request)
{
$this->request = $request;
Expand All @@ -53,6 +60,16 @@ public function getMeta(): LaramoreMeta
return $this->getRequest()->modelClass()::getMeta();
}

public function paginate($paginate = true)
{
$this->paginate = $paginate;
}

public function doesPaginate()
{
return $this->paginate;
}

public function buildParams($params): Collection
{
// For example: ?filter=value (one call for the filter with one single value).
Expand Down Expand Up @@ -163,7 +180,8 @@ public function hasFilter(string $name)
public function getFilter(string $name)
{
if (! $this->hasFilter($name)) {
if ($name === '_method') return;
if ($this->doesPaginate() && in_array($name, ['cursor', 'page'])) return;
// if ($name === '_method') return; ??

throw new FilterException($name, "The filter $name does not exist");
}
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Filters/OrderBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Laramore\Contracts\Http\Filters\{
BuilderFilter, CollectionFilter
};
use Laramore\Exceptions\FilterException;
use Laramore\Traits\Http\Filters\HasFieldParameter;

class OrderBy extends BaseFilter implements BuilderFilter, CollectionFilter
Expand All @@ -40,7 +41,7 @@ public function checkValue($value=null, Collection $params=null)
{
if (\in_array($value, $this->allowedValues)) {
if ($value === 'random' && !\is_null($params['field'])) {
throw new \Exception('Cannot be random and have a field');
throw new FilterException($this, 'Cannot be random and have a field');
}

return $value;
Expand All @@ -52,7 +53,7 @@ public function checkValue($value=null, Collection $params=null)
}, $value);
}

throw new \Exception('Use right value');
throw new FilterException($this, 'Use right value');
}

public function checkField($fieldName=null, array $params=[])
Expand Down
27 changes: 0 additions & 27 deletions src/Http/Filters/Page.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Http/Filters/PerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Support\Collection;
use Laramore\Contracts\Eloquent\LaramoreBuilder;
use Laramore\Contracts\Http\Filters\BuilderFilter;
use Laramore\Exceptions\FilterException;

class PerPage extends BaseFilter implements BuilderFilter
{
Expand All @@ -34,7 +35,7 @@ public function checkValue($value=null)
$perPage = (int) $value;

if ($perPage < $this->min || $perPage > $this->max) {
throw new \Exception("Min per page `{$this->min}` and max `{$this->max}`");
throw new FilterException($this, "Min per page `{$this->min}` and max `{$this->max}`");
}

return $perPage;
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Filters/Related.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
};
use Laramore\Contracts\Http\Filters\BuilderFilter;
use Laramore\Elements\OperatorElement;
use Laramore\Exceptions\FilterException;
use Laramore\Traits\Http\Filters\HasOperatorParameter;

class Related extends BaseFilter implements BuilderFilter
Expand Down Expand Up @@ -55,7 +56,7 @@ protected function owned()
$this->field = $meta->getField($this->getName());

if (! ($this->field instanceof RelationField)) {
throw new \Exception("The field {$this->getName()} is not a relation field");
throw new FilterException($this, "The field {$this->getName()} is not a relation field");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Filters/Trash.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Support\Collection;
use Laramore\Contracts\Eloquent\LaramoreBuilder;
use Laramore\Contracts\Http\Filters\BuilderFilter;
use Laramore\Exceptions\FilterException;

class Trash extends BaseFilter implements BuilderFilter
{
Expand All @@ -31,7 +32,7 @@ public function checkValue($value=null)
return $value;
}

throw new \Exception("{$value} is not allowed for filter. Use only ".\implode(', ', $this->allowedValues));
throw new FilterException($this, "{$value} is not allowed for filter. Use only ".\implode(', ', $this->allowedValues));
}

public function filterBuilder(LaramoreBuilder $builder, Collection $params): ?LaramoreBuilder
Expand Down
48 changes: 39 additions & 9 deletions src/Traits/Http/Requests/HasLaramoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ trait HasLaramoreRequest
*
* @var Collection
*/
protected $paginate;
protected $pagination;

/**
* All inputs.
Expand Down Expand Up @@ -167,13 +167,33 @@ public function getModel()
return $this->filterMeta()->filterModel($model, $this->filters());
}

/**
* Resolve pagination.
*
* @return Collection|null
*/
public function resolvePagination()
{
$query = $this->generateModelQuery();

if ($this->has('cursor')) {
return $query->cursorPaginate();
}

return $query->paginate();
}

/**
* Resolve models.
*
* @return Collection|null
*/
public function resolveModels()
{
if ($this->filterMeta()->doesPaginate()) {
$this->resolvePagination()->getCollection();
}

return $this->generateModelQuery()->get();
}

Expand All @@ -194,11 +214,15 @@ public function getModels()
*
* @return Paginator|mixed
*/
public function getPaginate()
public function getPagination()
{
$paginate = $this->generateModelQuery()->paginate();
$pagination = $this->resolvePagination();

return $this->filterMeta()->filterCollection($paginate, $this->filters());
$pagination->setCollection(
$this->filterMeta()->filterCollection($pagination->getCollection(), $this->filters())
);

return $pagination;
}

/**
Expand Down Expand Up @@ -234,7 +258,11 @@ public function model()
public function models()
{
if (\is_null($this->models)) {
$this->models = $this->getModels();
if ($this->filterMeta()->doesPaginate()) {
$this->pagination();
} else {
$this->models = $this->getModels();
}
}

return $this->models;
Expand All @@ -245,13 +273,15 @@ public function models()
*
* @return Paginator|mixed
*/
public function paginate()
public function pagination()
{
if (\is_null($this->paginate)) {
$this->paginate = $this->getPaginate();
if (\is_null($this->pagination)) {
$this->pagination = $this->getPagination();

$this->models = $this->pagination->getCollection();
}

return $this->paginate;
return $this->pagination;
}

public static function resolveFieldsFromMeta(LaramoreMeta $meta)
Expand Down

0 comments on commit e47256d

Please sign in to comment.