-
-
Save ArielMejiaDev/d142e0ba248190d32e0c5b84cd67318a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Resources; | |
use Illuminate\Http\Resources\Json\ResourceCollection; | |
use Illuminate\Pagination\UrlWindow; | |
use Illuminate\Support\Arr; | |
use Barryvdh\Debugbar\Facade as DebugbarFacade; | |
use Illuminate\Container\Container; | |
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; | |
class ApiPaginationResource extends ResourceCollection | |
{ | |
/** | |
* Indicates wheather the debugbar data need | |
* to be added to api response. | |
* | |
* @var bool | |
*/ | |
public $needDegugbar = true; | |
/** | |
* Create a new Paginated Response | |
* | |
**/ | |
public function __construct(LengthAwarePaginatorContract $paginatedInstance,bool $needDegugbar = true) | |
{ | |
parent::__construct($paginatedInstance); | |
$this->needDegugbar = $needDegugbar; | |
} | |
/** | |
* Transform the resource into an array. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
public function toArray($request) | |
{ | |
return parent::toArray($request); | |
} | |
/** | |
* Get additional data that should be returned with the resource array. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
public function with($request) | |
{ | |
$withArray = []; | |
$trueCallback = function(){ | |
return true; | |
}; | |
// $falseCallback = function(){ | |
// return false; | |
// }; | |
$metaData = [ | |
'full_url' => [ | |
'value' => $request->fullUrl(), | |
'canBeAdded' => $trueCallback, | |
], | |
'page_name' => [ | |
'value' => $this->resource->getPageName(), | |
'canBeAdded' => $trueCallback, | |
], | |
'path_info' => [ | |
'value' => $request->getPathInfo(), | |
'canBeAdded' => $trueCallback, | |
], | |
'query_parms' => [ | |
'value' => Arr::except($request->query(),[/**$this->resource->getPageName() **/]), | |
'canBeAdded' => $trueCallback, | |
], | |
'query_string' => [ | |
'value' => $request->getQueryString(), | |
'canBeAdded' => $trueCallback, | |
], | |
'request_uri' => [ | |
'value' => $request->getRequestUri(), | |
'canBeAdded' => $trueCallback, | |
], | |
]; | |
foreach ($metaData as $metaDataKey => $eachMeta) { | |
if(call_user_func($eachMeta['canBeAdded'])): | |
$withArray['meta'][$metaDataKey] = $eachMeta['value']; | |
endif; | |
} | |
$metaData = [ | |
'paginator_url' => [ | |
'value' => UrlWindow::make($this->resource), | |
'canBeAdded' => $trueCallback, | |
], | |
]; | |
foreach ($metaData as $metaDataKey => $eachMeta) { | |
if(call_user_func($eachMeta['canBeAdded'])): | |
$withArray['links'][$metaDataKey] = $eachMeta['value']; | |
endif; | |
} | |
if($this->needDegugbar && Container::getInstance()->bound('debugbar') && Container::getInstance()->make('debugbar')->isEnabled()): | |
$withArray['debugbar_profiling'] = DebugbarFacade::getData(); | |
endif; | |
return $withArray; | |
} | |
/** | |
* Customize the outgoing response for the resource. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Illuminate\Http\Response $response | |
* @return void | |
*/ | |
public function withResponse($request, $response) | |
{ | |
// $response->header('X-Value', 'FooBar'); | |
} | |
} | |
//usage: | |
// $users = User::query() | |
// ->select([ | |
// 'users.id', | |
// 'users.name', | |
// 'users.email', | |
// ]) | |
// ->paginate(2,['*'],'customPage') | |
// ->onEachSide(2) | |
// ->appends(request()->all()); | |
// return new ApiPaginationResource($users,true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment