-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/ApiResponseServiceProvider.php → src/ResponseServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,75 @@ | ||
<?php | ||
|
||
namespace Ijodkor\LaravelApiResponse\Responses; | ||
namespace Ijodkor\ApiResponse\Responses; | ||
|
||
use Ijodkor\ApiResponse\Supporters\ListPaginator; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
|
||
trait ApiResponse { | ||
|
||
protected function success(array $data = [], $message = 'Muvaffaqiyatli'): JsonResponse { | ||
return response()->json([ | ||
'success' => true, | ||
'data' => [...$data], | ||
'msg' => $message | ||
'message' => $message | ||
]); | ||
} | ||
|
||
protected function created(array $data = [], $message = 'Muvaffaqiyatli'): JsonResponse { | ||
return response()->json([ | ||
'success' => true, | ||
'data' => [...$data], | ||
'msg' => $message | ||
'message' => $message | ||
], 201); | ||
} | ||
|
||
protected function fail($errors = [], $message = 'Muvaffaqiyatsiz', int $status = 400): JsonResponse { | ||
return response()->json([ | ||
'success' => false, | ||
'errors' => $errors, | ||
'msg' => $message | ||
'message' => $message | ||
], $status); | ||
} | ||
|
||
protected function error(string $msg = 'Ichki xatolik yuz berdi!', $errors = []): JsonResponse { | ||
return response()->json([ | ||
'success' => false, | ||
'errors' => $errors, | ||
'msg' => $msg | ||
], 400); | ||
'message' => $msg | ||
], 500); | ||
} | ||
|
||
protected function unAuthorized($msg = "Kirishga ruxsat berilmagan"): JsonResponse { | ||
protected function unAuthorized($message = "Kirishga ruxsat berilmagan"): JsonResponse { | ||
return response()->json([ | ||
'success' => false, | ||
'msg' => $msg | ||
'message' => $message | ||
], 401); | ||
} | ||
|
||
protected function result($data = [], $msg = 'Muvaffaqiyatli'): JsonResponse { | ||
/** Bonuses **/ | ||
protected function result($data = [], $message = 'Muvaffaqiyatli'): JsonResponse { | ||
return response()->json([ | ||
'success' => true, | ||
'data' => $data, | ||
'msg' => $msg | ||
'message' => $message | ||
], options: JSON_NUMERIC_CHECK); | ||
} | ||
|
||
protected function paginated($data, string $key, string|null $message = 'Muvaffaqiyatli'): JsonResponse { | ||
if ($data instanceof LengthAwarePaginator) { | ||
$data = new ListPaginator($data, $key); | ||
} elseif ($data instanceof ResourceCollection) { | ||
$data = new ListPaginator($data, $key); | ||
} | ||
|
||
return response()->json([ | ||
'status' => true, | ||
'data' => [ | ||
$key => $data | ||
], | ||
'message' => $message, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Ijodkor\ApiResponse\Supporters; | ||
|
||
use Illuminate\Contracts\Pagination\LengthAwarePaginator as Paginator; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
|
||
class ListPaginator extends LengthAwarePaginator { | ||
|
||
private string $key; | ||
|
||
public function __construct(Paginator|ResourceCollection $paginator, string $key = "data") { | ||
$this->key = $key; | ||
|
||
$options = [ | ||
'path' => $paginator->path() | ||
]; | ||
parent::__construct($paginator->items(), $paginator->total(), $paginator->perPage(), $paginator->currentPage(), $options); | ||
} | ||
|
||
public function toArray(): array { | ||
$data = parent::toArray(); | ||
|
||
return [ | ||
$this->key => $data['data'], | ||
'links' => $data['links'], | ||
'meta' => [ | ||
'first_page_url' => $data['first_page_url'], | ||
'current_page' => $data['current_page'], | ||
'last_page' => $data['last_page'], | ||
'last_page_url' => $data['first_page_url'], | ||
'per_page' => $data['per_page'], | ||
'total' => $data['total'] | ||
] | ||
]; | ||
} | ||
} |