/
home
/
suroeste
/
public_html
/
payments.transportessuroeste.com
/
src
/
Utils
/
/home/suroeste/public_html/payments.transportessuroeste.com/src/Utils
mkdir
upload
Name
Size
Mode
Actions
JsonResponse.php
2169
0644
edit
dl
rm
Router.php
2519
0644
edit
dl
rm
Edit:
/home/suroeste/public_html/payments.transportessuroeste.com/src/Utils/JsonResponse.php
(2169B)
<?php /** * ============================================================================ * JSON RESPONSE - Respuestas estandarizadas * ============================================================================ */ namespace TransportesSuroeste\Utils; class JsonResponse { public static function send(array $data, int $statusCode = 200): void { http_response_code($statusCode); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; } public static function success($data, string $message = 'OK', int $code = 200): void { self::send([ 'success' => true, 'message' => $message, 'data' => $data, 'meta' => [ 'timestamp' => date('Y-m-d\TH:i:s.uP'), 'request_id' => $_SERVER['HTTP_X_REQUEST_ID'] ?? bin2hex(random_bytes(8)) ] ], $code); } public static function error(string $message, int $code = 400, array $errors = []): void { $response = [ 'success' => false, 'message' => $message, 'error' => [ 'code' => $code, 'type' => self::getErrorType($code) ], 'meta' => [ 'timestamp' => date('Y-m-d\TH:i:s.uP'), 'request_id' => $_SERVER['HTTP_X_REQUEST_ID'] ?? bin2hex(random_bytes(8)) ] ]; if (!empty($errors)) { $response['error']['details'] = $errors; } self::send($response, $code); } private static function getErrorType(int $code): string { return match($code) { 400 => 'bad_request', 401 => 'unauthorized', 403 => 'forbidden', 404 => 'not_found', 405 => 'method_not_allowed', 409 => 'conflict', 413 => 'payload_too_large', 415 => 'unsupported_media_type', 422 => 'validation_error', 429 => 'rate_limit_exceeded', 500 => 'internal_error', 503 => 'service_unavailable', default => 'error' }; } }
Save
cmd:
run