This commit is contained in:
119
app/Utils/ApiResponse.php
Normal file
119
app/Utils/ApiResponse.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/3
|
||||
* Time: 下午8:30
|
||||
* Description:
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Hyperf\HttpMessage\Server\Response;
|
||||
use Hyperf\HttpMessage\Stream\SwooleStream;
|
||||
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2025/6/3
|
||||
* Time: 下午8:31
|
||||
* Description: A utility class for creating API responses.
|
||||
*
|
||||
* (c) ykxiao <yk_9001@hotmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
class ApiResponse
|
||||
{
|
||||
protected string $token = ''; // Stores the authorization token
|
||||
|
||||
protected Response $response; // Holds the response object
|
||||
|
||||
/**
|
||||
* Constructor to initialize ApiResponse object with a response object.
|
||||
*
|
||||
* @param Response $response the response object to be used for sending API responses
|
||||
*/
|
||||
public function __construct(Response $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a successful API response with optional data, message, and status code.
|
||||
*
|
||||
* @param array $data the data to be returned in the response
|
||||
* @param string $message a success message to be returned
|
||||
* @param int $code the HTTP status code for the response
|
||||
* @return Response the constructed response with success data
|
||||
*/
|
||||
public function success(array $data = [], string $message = 'Success', int $code = 200): Response
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
return self::jsonResponse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an error API response with an error message, optional data, and status code.
|
||||
*
|
||||
* @param string $message the error message to be returned
|
||||
* @param int $code the HTTP status code for the error response
|
||||
* @param null $data optional additional data to be returned with the error
|
||||
* @return Response the constructed response with error data
|
||||
*/
|
||||
public function error(string $message, int $code = 400, $data = null): Response
|
||||
{
|
||||
$result = [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
'data' => $data,
|
||||
];
|
||||
|
||||
return self::jsonResponse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an authorization token to be included in the response headers.
|
||||
*
|
||||
* @param string $token the authorization token
|
||||
* @return static the current instance of ApiResponse with the token set
|
||||
*/
|
||||
public function setToken(string $token): static
|
||||
{
|
||||
$this->token = $token;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts data into a JSON response with an optional status code.
|
||||
* If a token has been set, it includes the token in the authorization header.
|
||||
*
|
||||
* @param array $data the data to encode into JSON and send in the response
|
||||
* @param int $statusCode the HTTP status code for the response
|
||||
* @return Response the constructed JSON response
|
||||
*/
|
||||
public function jsonResponse(array $data, int $statusCode = 200): Response
|
||||
{
|
||||
$response = $this->response;
|
||||
if ($this->token) {
|
||||
// Adds authorization header to the response if a token is set
|
||||
$response->withHeader('Authorization', 'Bearer ' . $this->token);
|
||||
}
|
||||
// Constructs and returns the response with the specified data and headers
|
||||
return $response
|
||||
->withStatus($statusCode)
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withBody(new SwooleStream(json_encode($data)));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user