You've already forked Php-Template
feat: implement GenericResponse class and update controllers to use it for consistent JSON responses
Some checks failed
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Failing after 2m28s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m31s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Failing after 2m52s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m7s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m54s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 2m31s
Some checks failed
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Failing after 2m28s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m31s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Failing after 2m52s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m7s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m54s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Failing after 2m31s
This commit is contained in:
@@ -8,6 +8,7 @@ use Illuminate\Database\PostgresConnection;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\App\Http\Responses\GenericResponse;
|
||||
use Siteworxpro\App\Models\Model;
|
||||
use Siteworxpro\App\Services\Facades\Redis;
|
||||
use Siteworxpro\HttpStatus\CodesEnum;
|
||||
@@ -53,7 +54,7 @@ class HealthcheckController extends Controller
|
||||
}
|
||||
|
||||
return JsonResponseFactory::createJsonResponse(
|
||||
['status_code' => 200, 'message' => 'Healthcheck OK']
|
||||
new GenericResponse('Healthcheck OK', CodesEnum::OK->value)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use Siteworxpro\App\Docs\TokenSecurity;
|
||||
use Siteworxpro\App\Docs\UnauthorizedResponse;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Siteworxpro\App\Http\Responses\GenericResponse;
|
||||
|
||||
/**
|
||||
* Class IndexController
|
||||
@@ -31,15 +32,12 @@ class IndexController extends Controller
|
||||
#[OA\Response(
|
||||
response: '200',
|
||||
description: 'An Example Response',
|
||||
content: new OA\JsonContent(properties: [
|
||||
new OA\Property('status_code', type: 'integer'),
|
||||
new OA\Property('message', type: 'string'),
|
||||
])
|
||||
content: new OA\JsonContent(ref: '#/components/schemas/GenericResponse')
|
||||
)]
|
||||
#[UnauthorizedResponse]
|
||||
public function get(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||
return JsonResponseFactory::createJsonResponse(['message' => 'Server is running']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,16 +51,11 @@ class IndexController extends Controller
|
||||
#[OA\Response(
|
||||
response: '200',
|
||||
description: 'An Example Response',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property('status_code', type: 'integer'),
|
||||
new OA\Property('message', type: 'string'),
|
||||
]
|
||||
)
|
||||
content: new OA\JsonContent(ref: '#/components/schemas/GenericResponse')
|
||||
)]
|
||||
#[UnauthorizedResponse]
|
||||
public function post(ServerRequest $request): ResponseInterface
|
||||
{
|
||||
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
|
||||
return JsonResponseFactory::createJsonResponse(new GenericResponse('POST request received'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ class OpenApiController extends Controller
|
||||
$openapi = new Generator()->generate([
|
||||
__DIR__ . '/../Controllers',
|
||||
__DIR__ . '/../Models',
|
||||
__DIR__ . '/../Http/Responses',
|
||||
]);
|
||||
|
||||
$response = new Response();
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Http;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Nyholm\Psr7\Response;
|
||||
use Siteworxpro\HttpStatus\CodesEnum;
|
||||
|
||||
@@ -17,13 +18,19 @@ class JsonResponseFactory
|
||||
/**
|
||||
* Create a JSON response with the given data and status code.
|
||||
*
|
||||
* @param array $data The data to include in the response.
|
||||
* @param array|Arrayable $data The data to include in the response.
|
||||
* @param CodesEnum $statusCode The HTTP status code for the response.
|
||||
* @return Response The JSON response.
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public static function createJsonResponse(array $data, CodesEnum $statusCode = CodesEnum::OK): Response
|
||||
{
|
||||
public static function createJsonResponse(
|
||||
array|Arrayable $data,
|
||||
CodesEnum $statusCode = CodesEnum::OK
|
||||
): Response {
|
||||
if ($data instanceof Arrayable) {
|
||||
$data = $data->toArray();
|
||||
}
|
||||
|
||||
return new Response(
|
||||
status: $statusCode->value,
|
||||
headers: [
|
||||
|
||||
32
src/Http/Responses/GenericResponse.php
Normal file
32
src/Http/Responses/GenericResponse.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Http\Responses;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use OpenApi\Attributes as OA;
|
||||
|
||||
#[OA\Schema(
|
||||
schema: 'GenericResponse',
|
||||
properties: [
|
||||
new OA\Property(property: 'message', type: 'string', example: 'Operation completed successfully.'),
|
||||
new OA\Property(property: 'status_code', type: 'integer', example: 200),
|
||||
]
|
||||
)]
|
||||
readonly class GenericResponse implements Arrayable
|
||||
{
|
||||
public function __construct(
|
||||
private string $message = '',
|
||||
private int $statusCode = 200
|
||||
) {
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'message' => $this->message,
|
||||
'status_code' => $this->statusCode,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user