WizdomWeb/app/Utilities/Response.php

144 lines
4.0 KiB
PHP

<?php
namespace WizdomNetworks\WizeWeb\Utilities;
class Response
{
/**
* Send a JSON response.
*
* @param array $data The response data.
* @param int $status HTTP status code (default: 200).
* @param array $headers Additional headers to include in the response.
* @return void
*/
public static function json(array $data, int $statusCode = 200): void
{
if (headers_sent()) {
Logger::logError("Headers already sent. Unable to send JSON response.");
return;
}
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
/**
* Send an HTML response.
*
* @param string $content The HTML content.
* @param int $status HTTP status code (default: 200).
* @param array $headers Additional headers to include in the response.
* @return void
*/
public static function html(string $content, int $status = 200, array $headers = []): void
{
http_response_code($status);
header('Content-Type: text/html');
self::sendHeaders($headers);
echo $content;
self::logResponse(['content' => $content], $status);
exit;
}
/**
* Send a file download response.
*
* @param string $filePath The file path.
* @param string|null $downloadName The name for the downloaded file (optional).
* @param array $headers Additional headers to include in the response.
* @return void
*/
public static function file(string $filePath, ?string $downloadName = null, array $headers = []): void
{
if (!file_exists($filePath)) {
self::error('File not found.', 404);
}
$downloadName = $downloadName ?? basename($filePath);
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$downloadName\"");
header('Content-Length: ' . filesize($filePath));
self::sendHeaders($headers);
readfile($filePath);
self::logResponse(['file' => $downloadName], 200);
exit;
}
/**
* Send an error response.
*
* @param string $message The error message.
* @param int $status HTTP status code (default: 500).
* @param array $headers Additional headers to include in the response.
* @return void
*/
public static function error(string $message, int $status = 500, array $headers = []): void
{
self::json(['success' => false, 'message' => $message], $status, $headers);
}
/**
* Predefined response for 400 Bad Request.
*
* @param string $message The error message.
* @return void
*/
public static function badRequest(string $message): void
{
self::error($message, 400);
}
/**
* Predefined response for 404 Not Found.
*
* @param string $message The error message.
* @return void
*/
public static function notFound(string $message): void
{
self::error($message, 404);
}
/**
* Predefined response for 500 Internal Server Error.
*
* @param string $message The error message.
* @return void
*/
public static function serverError(string $message): void
{
self::error($message, 500);
}
/**
* Send custom headers.
*
* @param array $headers Headers to include in the response.
* @return void
*/
private static function sendHeaders(array $headers): void
{
foreach ($headers as $key => $value) {
header("$key: $value");
}
}
/**
* Log the response if debugging is enabled.
*
* @param array $data The response data.
* @param int $status HTTP status code.
* @return void
*/
private static function logResponse(array $data, int $status): void
{
if (getenv('DEBUG') === 'true') {
Logger::logInfo("Response sent with status $status: " . json_encode($data));
}
}
}