-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement collection logic * Implement response logic * Implement support for AnonymousResourceCollection * Add helper traits * Add tests * Update README
- Loading branch information
Showing
13 changed files
with
581 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\CollectsResources; | ||
use Illuminate\Http\Resources\MissingValue; | ||
use Illuminate\Pagination\AbstractPaginator; | ||
use Lampager\Laravel\PaginationResult; | ||
|
||
/** | ||
* Trait CollectsPaginationResult | ||
* | ||
* @mixin \Illuminate\Http\Resources\Json\ResourceCollection | ||
*/ | ||
trait CollectsPaginationResult | ||
{ | ||
use CollectsResources; | ||
|
||
/** | ||
* Map the given collection resource into its individual resources. | ||
* | ||
* @param mixed $resource | ||
* @return mixed | ||
*/ | ||
protected function collectResource($resource) | ||
{ | ||
if ($resource instanceof MissingValue) { | ||
return $resource; | ||
} | ||
|
||
$collects = $this->collects(); | ||
|
||
$this->collection = $collects && !$resource->first() instanceof $collects | ||
? $resource->mapInto($collects) | ||
: $resource->toBase(); | ||
|
||
if ($resource instanceof AbstractPaginator) { | ||
$resource->setCollection($this->collection); | ||
return $resource; | ||
} | ||
if ($resource instanceof PaginationResult) { | ||
$resource->records = $this->collection; | ||
return $resource; | ||
} | ||
|
||
return $this->collection; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Http/Resources/Json/AnonymousPaginationResultAwareResourceCollection.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Http\Resources\Json; | ||
|
||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
use Lampager\Laravel\Http\Resources\CollectsPaginationResult; | ||
|
||
/** | ||
* Class AnonymousPaginationResultAwareResourceCollection | ||
* | ||
* @mixin \Illuminate\Http\Resources\Json\JsonResource | ||
*/ | ||
class AnonymousPaginationResultAwareResourceCollection extends AnonymousResourceCollection | ||
{ | ||
use MakesAnonymousPaginationResultAwareResourceCollection, | ||
CollectsPaginationResult, | ||
RespondsWithPaginationResult; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Http/Resources/Json/MakesAnonymousPaginationResultAwareResourceCollection.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Http\Resources\Json; | ||
|
||
/** | ||
* Trait MakesAnonymousPaginationResultAwareResourceCollection | ||
* | ||
* @mixin \Illuminate\Http\Resources\Json\JsonResource | ||
*/ | ||
trait MakesAnonymousPaginationResultAwareResourceCollection | ||
{ | ||
/** | ||
* Create a new anonymous resource collection. | ||
* | ||
* @param mixed $resource | ||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection | ||
*/ | ||
public static function collection($resource) | ||
{ | ||
return tap(new AnonymousPaginationResultAwareResourceCollection($resource, static::class), function ($collection) { | ||
if (property_exists(static::class, 'preserveKeys')) { | ||
$collection->preserveKeys = (new static([]))->preserveKeys === true; | ||
} | ||
}); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Http/Resources/Json/PaginationResultResourceResponse.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Http\Resources\Json; | ||
|
||
use Illuminate\Http\Resources\Json\PaginatedResourceResponse; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* class PaginationResultResourceResponse | ||
*/ | ||
class PaginationResultResourceResponse extends PaginatedResourceResponse | ||
{ | ||
/** | ||
* Add the pagination information to the response. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
protected function paginationInformation($request) | ||
{ | ||
return Arr::except($this->resource->resource->toArray(), 'records'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Http\Resources\Json; | ||
|
||
use Illuminate\Http\Resources\Json\PaginatedResourceResponse; | ||
use Illuminate\Pagination\AbstractPaginator; | ||
use Lampager\Laravel\PaginationResult; | ||
|
||
/** | ||
* Trait RespondsWithPaginationResult | ||
* | ||
* @mixin \Illuminate\Http\Resources\Json\ResourceCollection | ||
*/ | ||
trait RespondsWithPaginationResult | ||
{ | ||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function toResponse($request) | ||
{ | ||
if ($this->resource instanceof AbstractPaginator) { | ||
return (new PaginatedResourceResponse($this))->toResponse($request); | ||
} | ||
if ($this->resource instanceof PaginationResult) { | ||
return (new PaginationResultResourceResponse($this))->toResponse($request); | ||
} | ||
|
||
return parent::toResponse($request); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel; | ||
|
||
use Lampager\Laravel\Http\Resources\CollectsPaginationResult; | ||
use Lampager\Laravel\Http\Resources\Json\MakesAnonymousPaginationResultAwareResourceCollection; | ||
use Lampager\Laravel\Http\Resources\Json\RespondsWithPaginationResult; | ||
|
||
/** | ||
* Trait LampagerResourceCollectionTrait | ||
*/ | ||
trait LampagerResourceCollectionTrait | ||
{ | ||
use MakesAnonymousPaginationResultAwareResourceCollection, | ||
CollectsPaginationResult, | ||
RespondsWithPaginationResult; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel; | ||
|
||
use Lampager\Laravel\Http\Resources\Json\MakesAnonymousPaginationResultAwareResourceCollection; | ||
|
||
/** | ||
* Trait LampagerResourceTrait | ||
*/ | ||
trait LampagerResourceTrait | ||
{ | ||
use MakesAnonymousPaginationResultAwareResourceCollection; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Tests; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Lampager\Laravel\LampagerResourceTrait; | ||
|
||
/** | ||
* Class PostResource | ||
*/ | ||
class PostResource extends JsonResource | ||
{ | ||
use LampagerResourceTrait; | ||
|
||
public $preserveKeys = true; | ||
|
||
/** | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return parent::toArray($request) + [ | ||
'post_resource' => true, | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Lampager\Laravel\Tests; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Lampager\Laravel\LampagerResourceCollectionTrait; | ||
|
||
/** | ||
* Class PostResourceCollection | ||
*/ | ||
class PostResourceCollection extends ResourceCollection | ||
{ | ||
use LampagerResourceCollectionTrait; | ||
} |
Oops, something went wrong.