Skip to content

Commit

Permalink
fix toArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed Feb 14, 2022
1 parent ae15fbe commit b475d48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Resources/Json/Resource.php
Expand Up @@ -29,11 +29,11 @@ public function __construct($resource, ?string $message = null)
* Create a new anonymous resource collection.
*
* @param mixed $resource
* @return \App\Jsonable\Resources\ResourceCollection
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public static function collection($resource, ?string $message = null)
public static function collection($resource)
{
return tap(new ResourceCollection($resource, $message ?? static::$message ?? null), function ($collection) {
return tap(new ResourceCollection($resource, static::class, static::$message ?? null), function ($collection) {
if (property_exists(static::class, 'preserveKeys')) {
$collection->preserveKeys = (new static([]))->preserveKeys === true;
}
Expand All @@ -48,6 +48,6 @@ public static function collection($resource, ?string $message = null)
*/
public function toResponse($request)
{
return json()->success($this->resource->toArray(), static::$message ?? null);
return json()->success($this->toArray($request), static::$message ?? null);
}
}
30 changes: 24 additions & 6 deletions src/Resources/Json/ResourceCollection.php
Expand Up @@ -3,24 +3,41 @@
namespace Pharaonic\Laravel\Jsonable\Resources\Json;

use Illuminate\Http\Resources\Json\ResourceCollection as IlluminateResourceCollection;
use Illuminate\Http\Resources\Json\ResourceResponse;
use Illuminate\Pagination\AbstractCursorPaginator;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Arr;

class ResourceCollection extends IlluminateResourceCollection
{
/**
* Create a new resource instance.
* The name of the resource being collected.
*
* @var string
*/
public $collects;

/**
* Response message
*
* @var string
*/
public static $message = null;

/**
* Create a new anonymous resource collection.
*
* @param mixed $resource
* @param string $collects
* @param string|null $message
* @return void
*/
public function __construct($resource, ?string $message = null)
public function __construct($resource, $collects, ?string $message = null)
{
parent::__construct($resource);
$this->collects = $collects;
self::$message = $message;

$this->resource = $this->collectResource($resource);
if ($message) $this->message = $message;
parent::__construct($resource);
}

/**
Expand All @@ -33,8 +50,9 @@ public function toResponse($request)
{
if ($this->resource instanceof AbstractPaginator || $this->resource instanceof AbstractCursorPaginator)
return $this->preparePaginatedResponse($request);


return json()->success($this->resource->toArray(), $this->message ?? null);
return json()->success($this->toArray($request), static::$message ?? null);
}

/**
Expand Down

0 comments on commit b475d48

Please sign in to comment.