Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,112 @@
<?php
namespace App\Helpers;
/**
* Class Response
* Standardized JSON REST API Response Builder (PSR-7 Style)
*
* @package App\Helpers
*/
class Response
{
/**
* Send raw JSON response
*
* @param mixed $data
* @param int $status HTTP Status Code
* @param array $headers Additional headers
*/
public static function json($data, int $status = 200, array $headers = []): void
{
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
header('X-Powered-By: TTMQMS Enterprise API Engine v2.0');
foreach ($headers as $key => $val) {
header("{$key}: {$val}");
}
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
exit();
}
/**
* Send API Success Response
*
* @param string $message
* @param mixed $data
* @param int $status
*/
public static function success(string $message = 'Success', $data = null, int $status = 200): void
{
self::json([
'success' => true,
'status_code' => $status,
'message' => $message,
'data' => $data,
'timestamp' => date('Y-m-d H:i:s'),
], $status);
}
/**
* Send API Error Response
*
* @param string $message
* @param int $status
* @param array $errors
*/
public static function error(string $message = 'Error occurred', int $status = 400, array $errors = []): void
{
self::json([
'success' => false,
'status_code' => $status,
'error' => [
'message' => $message,
'details' => $errors,
],
'timestamp' => date('Y-m-d H:i:s'),
], $status);
}
/**
* Send Paginated API Response
*
* @param array $items
* @param int $total
* @param int $page
* @param int $limit
* @param string $message
*/
public static function paginate(array $items, int $total, int $page = 1, int $limit = 20, string $message = 'Data retrieved successfully'): void
{
$totalPages = $limit > 0 ? (int)ceil($total / $limit) : 1;
self::json([
'success' => true,
'status_code' => 200,
'message' => $message,
'data' => $items,
'pagination' => [
'total_items' => $total,
'current_page' => $page,
'items_per_page' => $limit,
'total_pages' => $totalPages,
'has_next' => $page < $totalPages,
'has_prev' => $page > 1,
],
'timestamp' => date('Y-m-d H:i:s'),
], 200);
}
/**
* Redirect to URL (For Frontend Views)
*
* @param string $url
* @param int $status
*/
public static function redirect(string $url, int $status = 302): void
{
http_response_code($status);
header("Location: {$url}");
exit();
}
}