123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
-
- namespace Illuminate\Http\Resources\Json;
-
- use ArrayAccess;
- use JsonSerializable;
- use Illuminate\Support\Collection;
- use Illuminate\Container\Container;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Routing\UrlRoutable;
- use Illuminate\Contracts\Support\Responsable;
- use Illuminate\Http\Resources\DelegatesToResource;
- use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
-
- class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable
- {
- use ConditionallyLoadsAttributes, DelegatesToResource;
-
- /**
- * The resource instance.
- *
- * @var mixed
- */
- public $resource;
-
- /**
- * The additional data that should be added to the top-level resource array.
- *
- * @var array
- */
- public $with = [];
-
- /**
- * The additional meta data that should be added to the resource response.
- *
- * Added during response construction by the developer.
- *
- * @var array
- */
- public $additional = [];
-
- /**
- * The "data" wrapper that should be applied.
- *
- * @var string
- */
- public static $wrap = 'data';
-
- /**
- * Create a new resource instance.
- *
- * @param mixed $resource
- * @return void
- */
- public function __construct($resource)
- {
- $this->resource = $resource;
- }
-
- /**
- * Create a new resource instance.
- *
- * @param dynamic $parameters
- * @return static
- */
- public static function make(...$parameters)
- {
- return new static(...$parameters);
- }
-
- /**
- * Create new anonymous resource collection.
- *
- * @param mixed $resource
- * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
- */
- public static function collection($resource)
- {
- return new AnonymousResourceCollection($resource, get_called_class());
- }
-
- /**
- * Resolve the resource to an array.
- *
- * @param \Illuminate\Http\Request|null $request
- * @return array
- */
- public function resolve($request = null)
- {
- $data = $this->toArray(
- $request = $request ?: Container::getInstance()->make('request')
- );
-
- if (is_array($data)) {
- $data = $data;
- } elseif ($data instanceof Arrayable || $data instanceof Collection) {
- $data = $data->toArray();
- } elseif ($data instanceof JsonSerializable) {
- $data = $data->jsonSerialize();
- }
-
- return $this->filter((array) $data);
- }
-
- /**
- * Transform the resource into an array.
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- public function toArray($request)
- {
- return $this->resource->toArray();
- }
-
- /**
- * Get any additional data that should be returned with the resource array.
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- public function with($request)
- {
- return $this->with;
- }
-
- /**
- * Add additional meta data to the resource response.
- *
- * @param array $data
- * @return $this
- */
- public function additional(array $data)
- {
- $this->additional = $data;
-
- return $this;
- }
-
- /**
- * Customize the response for a request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Illuminate\Http\JsonResponse $response
- * @return void
- */
- public function withResponse($request, $response)
- {
- //
- }
-
- /**
- * Set the string that should wrap the outer-most resource array.
- *
- * @param string $value
- * @return void
- */
- public static function wrap($value)
- {
- static::$wrap = $value;
- }
-
- /**
- * Disable wrapping of the outer-most resource array.
- *
- * @return void
- */
- public static function withoutWrapping()
- {
- static::$wrap = null;
- }
-
- /**
- * Transform the resource into an HTTP response.
- *
- * @param \Illuminate\Http\Request|null $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function response($request = null)
- {
- return $this->toResponse(
- $request ?: Container::getInstance()->make('request')
- );
- }
-
- /**
- * Create an HTTP response that represents the object.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function toResponse($request)
- {
- return (new ResourceResponse($this))->toResponse($request);
- }
-
- /**
- * Prepare the resource for JSON serialization.
- *
- * @return array
- */
- public function jsonSerialize()
- {
- return $this->resolve(Container::getInstance()->make('request'));
- }
- }
|