Commaaa2
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Illuminate\Http\Resources;
|
||||
|
||||
use Illuminate\Pagination\AbstractCursorPaginator;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use ReflectionClass;
|
||||
|
||||
trait CollectsResources
|
||||
{
|
||||
@@ -30,7 +32,7 @@ trait CollectsResources
|
||||
? $resource->mapInto($collects)
|
||||
: $resource->toBase();
|
||||
|
||||
return $resource instanceof AbstractPaginator
|
||||
return ($resource instanceof AbstractPaginator || $resource instanceof AbstractCursorPaginator)
|
||||
? $resource->setCollection($this->collection)
|
||||
: $this->collection;
|
||||
}
|
||||
@@ -47,16 +49,36 @@ trait CollectsResources
|
||||
}
|
||||
|
||||
if (Str::endsWith(class_basename($this), 'Collection') &&
|
||||
class_exists($class = Str::replaceLast('Collection', '', get_class($this)))) {
|
||||
(class_exists($class = Str::replaceLast('Collection', '', get_class($this))) ||
|
||||
class_exists($class = Str::replaceLast('Collection', 'Resource', get_class($this))))) {
|
||||
return $class;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON serialization options that should be applied to the resource response.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function jsonOptions()
|
||||
{
|
||||
$collects = $this->collects();
|
||||
|
||||
if (! $collects) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (new ReflectionClass($collects))
|
||||
->newInstanceWithoutConstructor()
|
||||
->jsonOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an iterator for the resource collection.
|
||||
*
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
{
|
||||
return $this->collection->getIterator();
|
||||
|
||||
@@ -64,6 +64,7 @@ trait DelegatesToResource
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->resource[$offset]);
|
||||
@@ -75,6 +76,7 @@ trait DelegatesToResource
|
||||
* @param mixed $offset
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->resource[$offset];
|
||||
@@ -87,6 +89,7 @@ trait DelegatesToResource
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->resource[$offset] = $value;
|
||||
@@ -98,6 +101,7 @@ trait DelegatesToResource
|
||||
* @param mixed $offset
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->resource[$offset]);
|
||||
|
||||
@@ -42,7 +42,7 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
|
||||
/**
|
||||
* The "data" wrapper that should be applied.
|
||||
*
|
||||
* @var string
|
||||
* @var string|null
|
||||
*/
|
||||
public static $wrap = 'data';
|
||||
|
||||
@@ -69,7 +69,7 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new anonymous resource collection.
|
||||
* Create a new anonymous resource collection.
|
||||
*
|
||||
* @param mixed $resource
|
||||
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
||||
@@ -108,7 +108,7 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
@@ -164,6 +164,16 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JSON serialization options that should be applied to the resource response.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function jsonOptions()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the response for a request.
|
||||
*
|
||||
@@ -226,6 +236,7 @@ class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRou
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->resolve(Container::getInstance()->make('request'));
|
||||
|
||||
@@ -23,7 +23,9 @@ class PaginatedResourceResponse extends ResourceResponse
|
||||
$this->resource->additional
|
||||
)
|
||||
),
|
||||
$this->calculateStatus()
|
||||
$this->calculateStatus(),
|
||||
[],
|
||||
$this->resource->jsonOptions()
|
||||
), function ($response) use ($request) {
|
||||
$response->original = $this->resource->resource->map(function ($item) {
|
||||
return is_array($item) ? Arr::get($item, 'resource') : $item->resource;
|
||||
@@ -43,10 +45,16 @@ class PaginatedResourceResponse extends ResourceResponse
|
||||
{
|
||||
$paginated = $this->resource->resource->toArray();
|
||||
|
||||
return [
|
||||
$default = [
|
||||
'links' => $this->paginationLinks($paginated),
|
||||
'meta' => $this->meta($paginated),
|
||||
];
|
||||
|
||||
if (method_exists($this->resource, 'paginationInformation')) {
|
||||
return $this->resource->paginationInformation($request, $paginated, $default);
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Illuminate\Http\Resources\Json;
|
||||
|
||||
use Countable;
|
||||
use Illuminate\Http\Resources\CollectsResources;
|
||||
use Illuminate\Pagination\AbstractCursorPaginator;
|
||||
use Illuminate\Pagination\AbstractPaginator;
|
||||
use IteratorAggregate;
|
||||
|
||||
@@ -84,6 +85,7 @@ class ResourceCollection extends JsonResource implements Countable, IteratorAggr
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count()
|
||||
{
|
||||
return $this->collection->count();
|
||||
@@ -93,7 +95,7 @@ class ResourceCollection extends JsonResource implements Countable, IteratorAggr
|
||||
* Transform the resource into a JSON array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
@@ -108,7 +110,7 @@ class ResourceCollection extends JsonResource implements Countable, IteratorAggr
|
||||
*/
|
||||
public function toResponse($request)
|
||||
{
|
||||
if ($this->resource instanceof AbstractPaginator) {
|
||||
if ($this->resource instanceof AbstractPaginator || $this->resource instanceof AbstractCursorPaginator) {
|
||||
return $this->preparePaginatedResponse($request);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,9 @@ class ResourceResponse implements Responsable
|
||||
$this->resource->with($request),
|
||||
$this->resource->additional
|
||||
),
|
||||
$this->calculateStatus()
|
||||
$this->calculateStatus(),
|
||||
[],
|
||||
$this->resource->jsonOptions()
|
||||
), function ($response) use ($request) {
|
||||
$response->original = $this->resource->resource;
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class MergeValue
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* Create new merge value instance.
|
||||
* Create a new merge value instance.
|
||||
*
|
||||
* @param \Illuminate\Support\Collection|\JsonSerializable|array $data
|
||||
* @return void
|
||||
|
||||
Reference in New Issue
Block a user