JsonResource.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Illuminate\Http\Resources\Json;
  3. use ArrayAccess;
  4. use JsonSerializable;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Contracts\Routing\UrlRoutable;
  9. use Illuminate\Contracts\Support\Responsable;
  10. use Illuminate\Http\Resources\DelegatesToResource;
  11. use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
  12. class JsonResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable
  13. {
  14. use ConditionallyLoadsAttributes, DelegatesToResource;
  15. /**
  16. * The resource instance.
  17. *
  18. * @var mixed
  19. */
  20. public $resource;
  21. /**
  22. * The additional data that should be added to the top-level resource array.
  23. *
  24. * @var array
  25. */
  26. public $with = [];
  27. /**
  28. * The additional meta data that should be added to the resource response.
  29. *
  30. * Added during response construction by the developer.
  31. *
  32. * @var array
  33. */
  34. public $additional = [];
  35. /**
  36. * The "data" wrapper that should be applied.
  37. *
  38. * @var string
  39. */
  40. public static $wrap = 'data';
  41. /**
  42. * Create a new resource instance.
  43. *
  44. * @param mixed $resource
  45. * @return void
  46. */
  47. public function __construct($resource)
  48. {
  49. $this->resource = $resource;
  50. }
  51. /**
  52. * Create a new resource instance.
  53. *
  54. * @param dynamic $parameters
  55. * @return static
  56. */
  57. public static function make(...$parameters)
  58. {
  59. return new static(...$parameters);
  60. }
  61. /**
  62. * Create new anonymous resource collection.
  63. *
  64. * @param mixed $resource
  65. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  66. */
  67. public static function collection($resource)
  68. {
  69. return new AnonymousResourceCollection($resource, get_called_class());
  70. }
  71. /**
  72. * Resolve the resource to an array.
  73. *
  74. * @param \Illuminate\Http\Request|null $request
  75. * @return array
  76. */
  77. public function resolve($request = null)
  78. {
  79. $data = $this->toArray(
  80. $request = $request ?: Container::getInstance()->make('request')
  81. );
  82. if (is_array($data)) {
  83. $data = $data;
  84. } elseif ($data instanceof Arrayable || $data instanceof Collection) {
  85. $data = $data->toArray();
  86. } elseif ($data instanceof JsonSerializable) {
  87. $data = $data->jsonSerialize();
  88. }
  89. return $this->filter((array) $data);
  90. }
  91. /**
  92. * Transform the resource into an array.
  93. *
  94. * @param \Illuminate\Http\Request $request
  95. * @return array
  96. */
  97. public function toArray($request)
  98. {
  99. return $this->resource->toArray();
  100. }
  101. /**
  102. * Get any additional data that should be returned with the resource array.
  103. *
  104. * @param \Illuminate\Http\Request $request
  105. * @return array
  106. */
  107. public function with($request)
  108. {
  109. return $this->with;
  110. }
  111. /**
  112. * Add additional meta data to the resource response.
  113. *
  114. * @param array $data
  115. * @return $this
  116. */
  117. public function additional(array $data)
  118. {
  119. $this->additional = $data;
  120. return $this;
  121. }
  122. /**
  123. * Customize the response for a request.
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @param \Illuminate\Http\JsonResponse $response
  127. * @return void
  128. */
  129. public function withResponse($request, $response)
  130. {
  131. //
  132. }
  133. /**
  134. * Set the string that should wrap the outer-most resource array.
  135. *
  136. * @param string $value
  137. * @return void
  138. */
  139. public static function wrap($value)
  140. {
  141. static::$wrap = $value;
  142. }
  143. /**
  144. * Disable wrapping of the outer-most resource array.
  145. *
  146. * @return void
  147. */
  148. public static function withoutWrapping()
  149. {
  150. static::$wrap = null;
  151. }
  152. /**
  153. * Transform the resource into an HTTP response.
  154. *
  155. * @param \Illuminate\Http\Request|null $request
  156. * @return \Illuminate\Http\JsonResponse
  157. */
  158. public function response($request = null)
  159. {
  160. return $this->toResponse(
  161. $request ?: Container::getInstance()->make('request')
  162. );
  163. }
  164. /**
  165. * Create an HTTP response that represents the object.
  166. *
  167. * @param \Illuminate\Http\Request $request
  168. * @return \Illuminate\Http\JsonResponse
  169. */
  170. public function toResponse($request)
  171. {
  172. return (new ResourceResponse($this))->toResponse($request);
  173. }
  174. /**
  175. * Prepare the resource for JSON serialization.
  176. *
  177. * @return array
  178. */
  179. public function jsonSerialize()
  180. {
  181. return $this->resolve(Container::getInstance()->make('request'));
  182. }
  183. }