ResourceResponse.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Illuminate\Http\Resources\Json;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Contracts\Support\Responsable;
  6. class ResourceResponse implements Responsable
  7. {
  8. /**
  9. * The underlying resource.
  10. *
  11. * @var mixed
  12. */
  13. public $resource;
  14. /**
  15. * Create a new resource response.
  16. *
  17. * @param mixed $resource
  18. * @return void
  19. */
  20. public function __construct($resource)
  21. {
  22. $this->resource = $resource;
  23. }
  24. /**
  25. * Create an HTTP response that represents the object.
  26. *
  27. * @param \Illuminate\Http\Request $request
  28. * @return \Illuminate\Http\JsonResponse
  29. */
  30. public function toResponse($request)
  31. {
  32. return tap(response()->json(
  33. $this->wrap(
  34. $this->resource->resolve($request),
  35. $this->resource->with($request),
  36. $this->resource->additional
  37. ),
  38. $this->calculateStatus()
  39. ), function ($response) use ($request) {
  40. $response->original = $this->resource->resource;
  41. $this->resource->withResponse($request, $response);
  42. });
  43. }
  44. /**
  45. * Wrap the given data if necessary.
  46. *
  47. * @param array $data
  48. * @param array $with
  49. * @param array $additional
  50. * @return array
  51. */
  52. protected function wrap($data, $with = [], $additional = [])
  53. {
  54. if ($data instanceof Collection) {
  55. $data = $data->all();
  56. }
  57. if ($this->haveDefaultWrapperAndDataIsUnwrapped($data)) {
  58. $data = [$this->wrapper() => $data];
  59. } elseif ($this->haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)) {
  60. $data = [($this->wrapper() ?? 'data') => $data];
  61. }
  62. return array_merge_recursive($data, $with, $additional);
  63. }
  64. /**
  65. * Determine if we have a default wrapper and the given data is unwrapped.
  66. *
  67. * @param array $data
  68. * @return bool
  69. */
  70. protected function haveDefaultWrapperAndDataIsUnwrapped($data)
  71. {
  72. return $this->wrapper() && ! array_key_exists($this->wrapper(), $data);
  73. }
  74. /**
  75. * Determine if "with" data has been added and our data is unwrapped.
  76. *
  77. * @param array $data
  78. * @param array $with
  79. * @param array $additional
  80. * @return bool
  81. */
  82. protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)
  83. {
  84. return (! empty($with) || ! empty($additional)) &&
  85. (! $this->wrapper() ||
  86. ! array_key_exists($this->wrapper(), $data));
  87. }
  88. /**
  89. * Get the default data wrapper for the resource.
  90. *
  91. * @return string
  92. */
  93. protected function wrapper()
  94. {
  95. return get_class($this->resource)::$wrap;
  96. }
  97. /**
  98. * Calculate the appropriate status code for the response.
  99. *
  100. * @return int
  101. */
  102. protected function calculateStatus()
  103. {
  104. return $this->resource->resource instanceof Model &&
  105. $this->resource->resource->wasRecentlyCreated ? 201 : 200;
  106. }
  107. }