ApiResponseManager
in package
Class ApiResponseManager
Manages global messages, errors, CORS, and handles API responses following JSON:API specification. Provides features for logging, security, validation, and robust error handling.
Tags
Table of Contents
Properties
- $cors : mixed
- $globalErrors : array<string|int, string>
- Array to store global errors.
- $globalMessages : array<string|int, string>
- Array to store global messages.
Methods
- __construct() : mixed
- ApiResponseManager constructor.
- addError() : void
- Adds an error to the global errors array.
- addMessage() : void
- Adds a message to the global messages array.
- bailOut() : never
- Bail out and respond to the client with a failure response.
- canContinue() : bool
- Checks if the response can continue based on the global errors array.
- handleContinuance() : void
- Handles continuance based on the presence of errors.
- handleCORS() : void
- Handles CORS for the current request using the configured CORS handler.
- respond() : never
- Responds to the client with a JSON-encoded response following JSON:API spec.
- sendEmptyResponse() : never
- Responds to the client with an empty response (no content).
- setCORSHandler() : void
- Sets the CORS handler instance to use for handling CORS requests.
Properties
$cors
public
mixed
$cors
$globalErrors
Array to store global errors.
public
array<string|int, string>
$globalErrors
= []
$globalMessages
Array to store global messages.
public
array<string|int, string>
$globalMessages
= []
Methods
__construct()
ApiResponseManager constructor.
public
__construct(ErrorLogger $errorLogger, APIManager $apiManager) : mixed
Initializes the response manager with an error logger and API manager instance.
Parameters
- $errorLogger : ErrorLogger
-
The logger instance to use for error logging.
- $apiManager : APIManager
-
The API manager instance to use for API operations.
addError()
Adds an error to the global errors array.
public
addError(array<string|int, mixed> $error) : void
Parameters
- $error : array<string|int, mixed>
-
The error to add, consistent with JSON:API specification.
Tags
addMessage()
Adds a message to the global messages array.
public
addMessage(string|array<string|int, mixed> $message) : void
Parameters
- $message : string|array<string|int, mixed>
-
The message to add. It can be either a string or an array.
Tags
bailOut()
Bail out and respond to the client with a failure response.
public
bailOut([int|null $statusCode = 400 ]) : never
This method is useful when you need to stop the execution and respond with an error.
Parameters
- $statusCode : int|null = 400
-
The HTTP status code for the response. Defaults to 400.
Return values
nevercanContinue()
Checks if the response can continue based on the global errors array.
public
canContinue() : bool
Return values
bool —Whether the response can continue.
handleContinuance()
Handles continuance based on the presence of errors.
public
handleContinuance([int|null $statusCode = 400 ]) : void
If there are errors present, the script will bail out with the provided status code.
Parameters
- $statusCode : int|null = 400
-
The HTTP status code to use when bailing out. Defaults to 400.
handleCORS()
Handles CORS for the current request using the configured CORS handler.
public
handleCORS([array<string|int, mixed>|null $allowedOrigins = null ][, bool $allowCredentials = false ][, array<string|int, mixed> $allowedMethods = ['GET', 'POST', 'OPTIONS'] ][, array<string|int, mixed> $allowedHeaders = ['Content-Type', 'Authorization'] ][, array<string|int, mixed> $exposedHeaders = [] ][, int $maxAge = 86400 ]) : void
This method should be called at the start of any request handling.
Parameters
- $allowedOrigins : array<string|int, mixed>|null = null
-
Allowed origins for CORS.
- $allowCredentials : bool = false
-
Whether to allow credentials in CORS requests.
- $allowedMethods : array<string|int, mixed> = ['GET', 'POST', 'OPTIONS']
-
Allowed HTTP methods.
- $allowedHeaders : array<string|int, mixed> = ['Content-Type', 'Authorization']
-
Allowed headers in CORS requests.
- $exposedHeaders : array<string|int, mixed> = []
-
Exposed headers in CORS responses.
- $maxAge : int = 86400
-
Max age for preflight cache.
respond()
Responds to the client with a JSON-encoded response following JSON:API spec.
public
respond([bool $success = true ][, array<string, mixed> $data = [] ][, int|null $statusCode = null ][, array<string|int, string>|null $messages = null ][, array<string|int, string>|null $errors = null ]) : never
Parameters
- $success : bool = true
-
Indicates whether the response represents a successful outcome.
- $data : array<string, mixed> = []
-
The data to include in the response, compliant with JSON:API.
- $statusCode : int|null = null
-
The HTTP status code for the response.
- $messages : array<string|int, string>|null = null
-
An optional array of messages to include in the response.
- $errors : array<string|int, string>|null = null
-
An optional array of errors to include in the response.
Return values
neversendEmptyResponse()
Responds to the client with an empty response (no content).
public
sendEmptyResponse([int $statusCode = 204 ]) : never
Parameters
- $statusCode : int = 204
-
The HTTP status code for the response. Defaults to 204 (No Content).
Tags
Return values
neversetCORSHandler()
Sets the CORS handler instance to use for handling CORS requests.
public
setCORSHandler(CORS $cors) : void
Parameters
- $cors : CORS
-
The CORS handler instance to use.