Some checks failed
continuous-integration/drone/push Build is failing
- 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
56 lines
1.2 KiB
PHP
56 lines
1.2 KiB
PHP
<?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,
|
|
);
|
|
}
|
|
}
|