Skip to content

Commit

Permalink
Api response name modified
Browse files Browse the repository at this point in the history
  • Loading branch information
yetakchi committed Jul 25, 2024
1 parent ebfd1e7 commit 878413c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Ijodkor\\LaravelApiResponse\\": "src/"
"Ijodkor\\ApiResponse\\": "src/"
}
},
"authors": [
Expand All @@ -33,7 +33,7 @@
"extra": {
"laravel": {
"providers": [
"Ijodkor\\LaravelApiResponse\\ApiResponseServiceProvider"
"Ijodkor\\ApiResponse\\ResponseServiceProvider"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Requests/RestRequest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Ijodkor\LaravelApiResponse\Requests;
namespace Ijodkor\ApiResponse\Requests;

use Ijodkor\LaravelApiResponse\Responses\ApiResponse;
use Ijodkor\ApiResponse\Responses\ApiResponse;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Ijodkor\LaravelApiResponse;
namespace Ijodkor\ApiResponse;

use Illuminate\Support\ServiceProvider;

class ApiResponseServiceProvider extends ServiceProvider {
class ResponseServiceProvider extends ServiceProvider {

/**
* Register any application services.
Expand Down
40 changes: 30 additions & 10 deletions src/Responses/ApiResponse.php
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,
]);
}
}
38 changes: 38 additions & 0 deletions src/Supporters/ListPaginator.php
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']
]
];
}
}

0 comments on commit 878413c

Please sign in to comment.