Skip to content

Commit

Permalink
fix: collection resource 格式包含 with 时数据结构错误(fixed #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Mar 20, 2021
1 parent 136d7cc commit 9b0aca0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 11 additions & 10 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra
*/
public function success($data = null, string $message = '', $code = 200, array $headers = [], $option = 0)
{
if ($data instanceof ResourceCollection && ($data->resource instanceof AbstractPaginator)) {
return $this->formatPaginatedResourceResponse(...func_get_args());
if ($data instanceof ResourceCollection) {
return $this->formatResourceCollectionResponse(...func_get_args());
}

if ($data instanceof JsonResource) {
Expand Down Expand Up @@ -305,7 +305,7 @@ protected function formatPaginatedData(array $paginated)
}

/**
* Format paginated resource response.
* Format collection resource response.
*
* @param JsonResource $resource
* @param string $message
Expand All @@ -315,16 +315,17 @@ protected function formatPaginatedData(array $paginated)
*
* @return mixed
*/
protected function formatPaginatedResourceResponse($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
protected function formatResourceCollectionResponse($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
{
$paginated = $resource->resource->toArray();
$dataField = Config::get('response.format.paginated_resource.data_field', 'data');

$paginationInformation = $this->formatPaginatedData($paginated);

$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data');
$resourceData = array_merge_recursive([$paginationDataField => $resource->resolve(request())], $resource->with(request()), $resource->additional);
$data = array_merge_recursive([$dataField => $resource->resolve(request())], $resource->with(request()), $resource->additional);
if ($resource->resource instanceof AbstractPaginator) {
$paginated = $resource->resource->toArray();
$paginationInformation = $this->formatPaginatedData($paginated);

$data = array_merge_recursive($resourceData, $paginationInformation);
$data = array_merge_recursive($data, $paginationInformation);
}

return tap(
$this->response($this->formatData($data, $message, $code), $code, $headers, $option),
Expand Down
2 changes: 1 addition & 1 deletion tests/SuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testSuccessWithCollectionData()
'status' => 'success',
'code' => 200,
'message' => ResponseCodeEnum::fromValue(200)->description,
'data' => $data,
'data' => ['data' => $data],
'error' => (object) [],
]);
$this->assertJsonStringEqualsJsonString($expectedJson, $response->getContent());
Expand Down

0 comments on commit 9b0aca0

Please sign in to comment.