leeds_backend/src/Libs/ResponseLib.php
2026-02-06 21:20:48 -03:00

60 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Bass\Webclient\Http;
use React\Http\Message\Response;
class ResponseLib
{
public static function sendOk(array $data = [], int $status = 200): Response
{
return self::json(
[
'success' => true,
'data' => $data
],
$status
);
}
public static function sendFail(
string $message,
int $status = 400,
array $details = []
): Response {
$data = ['message' => $message];
if (!empty($details)) {
$data['details'] = $details;
}
return self::json(
[
'success' => false,
'data' => $data
],
$status
);
}
private static function json(array $payload, int $status): Response
{
$json = json_encode($payload, JSON_UNESCAPED_UNICODE);
if ($json === false) {
// fallback extremo: nunca quebrar a API
$json = '{"success":false,"error":"Response encoding error"}';
$status = 500;
}
return new Response(
$status,
[
'Content-Type' => 'application/json',
'Cache-Control' => 'no-store'
],
$json
);
}
}