123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
-
- namespace Illuminate\Http\Resources\Json;
-
- use Illuminate\Support\Collection;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Contracts\Support\Responsable;
-
- class ResourceResponse implements Responsable
- {
- /**
- * The underlying resource.
- *
- * @var mixed
- */
- public $resource;
-
- /**
- * Create a new resource response.
- *
- * @param mixed $resource
- * @return void
- */
- public function __construct($resource)
- {
- $this->resource = $resource;
- }
-
- /**
- * Create an HTTP response that represents the object.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function toResponse($request)
- {
- return tap(response()->json(
- $this->wrap(
- $this->resource->resolve($request),
- $this->resource->with($request),
- $this->resource->additional
- ),
- $this->calculateStatus()
- ), function ($response) use ($request) {
- $response->original = $this->resource->resource;
-
- $this->resource->withResponse($request, $response);
- });
- }
-
- /**
- * Wrap the given data if necessary.
- *
- * @param array $data
- * @param array $with
- * @param array $additional
- * @return array
- */
- protected function wrap($data, $with = [], $additional = [])
- {
- if ($data instanceof Collection) {
- $data = $data->all();
- }
-
- if ($this->haveDefaultWrapperAndDataIsUnwrapped($data)) {
- $data = [$this->wrapper() => $data];
- } elseif ($this->haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)) {
- $data = [($this->wrapper() ?? 'data') => $data];
- }
-
- return array_merge_recursive($data, $with, $additional);
- }
-
- /**
- * Determine if we have a default wrapper and the given data is unwrapped.
- *
- * @param array $data
- * @return bool
- */
- protected function haveDefaultWrapperAndDataIsUnwrapped($data)
- {
- return $this->wrapper() && ! array_key_exists($this->wrapper(), $data);
- }
-
- /**
- * Determine if "with" data has been added and our data is unwrapped.
- *
- * @param array $data
- * @param array $with
- * @param array $additional
- * @return bool
- */
- protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)
- {
- return (! empty($with) || ! empty($additional)) &&
- (! $this->wrapper() ||
- ! array_key_exists($this->wrapper(), $data));
- }
-
- /**
- * Get the default data wrapper for the resource.
- *
- * @return string
- */
- protected function wrapper()
- {
- return get_class($this->resource)::$wrap;
- }
-
- /**
- * Calculate the appropriate status code for the response.
- *
- * @return int
- */
- protected function calculateStatus()
- {
- return $this->resource->resource instanceof Model &&
- $this->resource->resource->wasRecentlyCreated ? 201 : 200;
- }
- }
|