- Complete GGZ Ecademy Laravel backend application - RESTful API for learning products, members, filters - Authentication and authorization system - Database migrations and seeders - Custom CRUD generator commands - Email notification system - Integration with frontend applications
This commit is contained in:
55
app/Http/Controllers/Response/ApiResponse.php
Normal file
55
app/Http/Controllers/Response/ApiResponse.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Response;
|
||||
|
||||
use App\Http\Controllers\Error\ErrorInterface;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ApiResponse
|
||||
{
|
||||
public static function success(
|
||||
array $content,
|
||||
int $status = Response::HTTP_OK,
|
||||
array $headers = []
|
||||
): Response
|
||||
{
|
||||
return static::determineResponse($content, $status, false, $headers);
|
||||
}
|
||||
|
||||
private static function determineResponse(
|
||||
array $content,
|
||||
int $status,
|
||||
bool $error,
|
||||
array $headers = []
|
||||
): Response
|
||||
{
|
||||
return response(
|
||||
[
|
||||
'error' => $error,
|
||||
'status' => [
|
||||
'code' => $status,
|
||||
'text' => Response::$statusTexts[$status],
|
||||
],
|
||||
'content' => $content,
|
||||
],
|
||||
$status,
|
||||
$headers,
|
||||
);
|
||||
}
|
||||
|
||||
public static function error(
|
||||
ErrorInterface $error,
|
||||
array $headers = []
|
||||
): Response
|
||||
{
|
||||
return static::determineResponse(
|
||||
[
|
||||
'code' => $error->getErrorCode(),
|
||||
'message' => $error->getErrorMessage(),
|
||||
],
|
||||
$error->getStatusCode(),
|
||||
true,
|
||||
$headers,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user