Skip to content

Commit

Permalink
fix: error while using new pagination handler stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
rupadana committed Aug 28, 2024
1 parent b099001 commit aa665e9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/Contracts/HasAllowedIncludes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rupadana\ApiService\Contracts;

interface HasAllowedIncludes
{
public static function getAllowedIncludes(): array;
}
63 changes: 63 additions & 0 deletions src/Http/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Routing\Router;
use Rupadana\ApiService\ApiService;
use Rupadana\ApiService\Contracts\HasAllowedFields;
use Rupadana\ApiService\Contracts\HasAllowedFilters;
use Rupadana\ApiService\Contracts\HasAllowedIncludes;
use Rupadana\ApiService\Contracts\HasAllowedSorts;
use Rupadana\ApiService\Traits\HttpResponse;
use Rupadana\ApiService\Transformers\DefaultTransformer;

Expand Down Expand Up @@ -129,6 +133,65 @@ public function panel(Panel $panel)
return $this;
}

public function getAllowedFields(): array
{
$model = static::getModel();
if($model instanceof HasAllowedFields) {
return $model::getAllowedFields();
}

if(property_exists($model, 'allowedFields') && is_array($model::$allowedFields)) {
return $model::$allowedFields;
}

return [];
}

public function getAllowedIncludes(): array
{
$model = static::getModel();

if($model instanceof HasAllowedIncludes) {
return $model::getAllowedIncludes();
}

if(property_exists($model, 'allowedIncludes') && is_array($model::$allowedIncludes)) {
return $model::$allowedIncludes;
}

return [];
}

public function getAllowedSorts(): array
{
$model = static::getModel();

if($model instanceof HasAllowedSorts) {
return $model::getAllowedSorts();
}

if(property_exists($model, 'allowedFields') && is_array($model::$allowedFields)) {
return $model::$allowedFields;
}

return [];
}

public function getAllowedFilters() : array
{
$model = static::getModel();

if($model instanceof HasAllowedFilters) {
return $model::getAllowedFilters();
}

if(property_exists($model, 'allowedFilters') && is_array($model::$allowedFilters)) {
return $model::$allowedFilters;
}

return [];
}

public function getPanel(): Panel
{
return $this->panel;
Expand Down
8 changes: 4 additions & 4 deletions stubs/PaginationHandler.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class PaginationHandler extends Handlers {
$model = static::getModel();

$query = QueryBuilder::for($query)
->allowedFields($model::getAllowedFields() ?? [])
->allowedSorts($model::getAllowedSorts() ?? [])
->allowedFilters($model::getAllowedFilters() ?? [])
->allowedIncludes($model::getAllowedIncludes() ?? [])
->allowedFields($this->getAllowedFields() ?? [])
->allowedSorts($this->getAllowedSorts() ?? [])
->allowedFilters($this->getAllowedFilters() ?? [])
->allowedIncludes($this->getAllowedIncludes() ?? [])
->paginate(request()->query('per_page'))
->appends(request()->query());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,12 @@ public function handler()
{
$model = static::getModel();

$allowedFields = method_exists($model, 'getAllowedFields') && is_array($model::getAllowedFields())
? $model::getAllowedFields()
: (property_exists($model, 'allowedFields') && is_array($model::$allowedFields)
? $model::$allowedFields
: []);

$allowedSorts = method_exists($model, 'getAllowedSorts') && is_array($model::getAllowedSorts())
? $model::getAllowedSorts()
: (property_exists($model, 'allowedSorts') && is_array($model::$allowedSorts)
? $model::$allowedSorts
: []);

$allowedFilters = method_exists($model, 'getAllowedFilters') && is_array($model::getAllowedFilters())
? $model::getAllowedFilters()
: (property_exists($model, 'allowedFilters') && is_array($model::$allowedFilters)
? $model::$allowedFilters
: []);

$query = QueryBuilder::for($model)
->allowedFields($allowedFields)
->allowedSorts($allowedSorts)
->allowedFilters($allowedFilters)
->allowedFields($this->getAllowedFields() ?? [])
->allowedSorts($this->getAllowedSorts() ?? [])
->allowedFilters($this->getAllowedFilters() ?? [])
->allowedIncludes($this->getAllowedIncludes() ?? [])
->paginate(request()->query('per_page'))
->appends(request()->query());

Expand Down

0 comments on commit aa665e9

Please sign in to comment.