StatusTrait uses trait:short, trait:short, trait:short
Table of Contents
Constants
- LOGGABLE = 'loggable'
- The 'loggable' parameter constant.
- LOGGER = 'logger'
- The 'logger' parameter constant.
Properties
- $baseUrl : string
- The application's base URL.
- $jsonOptions : int
- The default json options used in the controller.
- $loggable : bool
- The loggable flag.
Methods
- alert() : void
- Action must be taken immediately.
- critical() : void
- Critical conditions.
- debug() : void
- Detailed debug information.
- emergency() : void
- System is unusable.
- error() : void
- Runtime errors that do not require immediate action but should typically be logged and monitored.
- fail() : ResponseInterface|null
- Formats a specific error status message with a code and an errors array representation of all errors.
- getCurrentPath() : string
- Returns the current application path relative to the base URL.
- getFullPath() : string
- Returns the full application URL including the base URL and optional parameters.
- getLogger() : LoggerInterface|null
- Returns the logger reference.
- getPath() : string
- Generates a path based on the base URL and a provided relative path.
- info() : void
- Interesting events.
- initializeBaseUrl() : static
- Initializes the internal `baseUrl` property.
- initializeJsonOptions() : static
- Initialize the internal $jsonOptions property.
- initializeLoggable() : static
- Initialize the loggable flag.
- initializeLogger() : static
- Initializes the logger reference for the current instance.
- jsonResponse() : ResponseInterface
- Return a JSON response
- log() : void
- Logs with an arbitrary level.
- notice() : void
- Normal but significant events.
- status() : ResponseInterface|null
- Outputs a response status message.
- success() : mixed
- Outputs a success message with a JSON response. If the $response parameter is null, returns the $data parameter value.
- warning() : void
- Exceptional occurrences that are not errors.
Constants
LOGGABLE
The 'loggable' parameter constant.
public
mixed
LOGGABLE
= 'loggable'
LOGGER
The 'logger' parameter constant.
public
mixed
LOGGER
= 'logger'
Properties
$baseUrl
The application's base URL.
public
string
$baseUrl
= \oihana\enums\Char::EMPTY
Used as a prefix for all generated URLs.
$jsonOptions
The default json options used in the controller.
public
int
$jsonOptions
= \oihana\enums\JsonParam::JSON_NONE
$loggable
The loggable flag.
public
bool
$loggable
= false
Methods
alert()
Action must be taken immediately.
public
alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
critical()
Critical conditions.
public
critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Application component unavailable, unexpected exception.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
debug()
Detailed debug information.
public
debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
emergency()
System is unusable.
public
emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
error()
Runtime errors that do not require immediate action but should typically be logged and monitored.
public
error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
fail()
Formats a specific error status message with a code and an errors array representation of all errors.
public
fail(ResponseInterface|null $response[, int|string|null $code = 400 ][, string|null $details = null ][, array<string|int, mixed> $options = [] ]) : ResponseInterface|null
Ex: A 'not acceptable' http request with a failed validation process
return $this->getError( $response , 406 , 'fields validation failed' , [ 'firstName' => 'firstName is required' , 'lastName' => 'lastName must be a string' ] ] ) ;
``
Parameters
- $response : ResponseInterface|null
-
The Response reference.
- $code : int|string|null = 400
-
The status code of the response.
- $details : string|null = null
-
The optional error message to overrides the default status message.
- $options : array<string|int, mixed> = []
-
The optional array to inject in the json object (with an errors)
Return values
ResponseInterface|nullgetCurrentPath()
Returns the current application path relative to the base URL.
public
getCurrentPath([ServerRequestInterface|null $request = null ][, array<string|int, mixed> $params = [] ][, bool $useNow = false ]) : string
Uses the Request object if provided, otherwise falls back to $_SERVER['REQUEST_URI'].
Allows adding GET parameters via $params.
Parameters
- $request : ServerRequestInterface|null = null
-
Optional HTTP request
- $params : array<string|int, mixed> = []
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_parameter with the current timestamp to prevent caching
Return values
string —Full path including the base URL and query parameters
getFullPath()
Returns the full application URL including the base URL and optional parameters.
public
getFullPath([array<string|int, mixed>|null $params = null ][, bool $useNow = false ]) : string
Parameters
- $params : array<string|int, mixed>|null = null
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_parameter with the current timestamp
Return values
string —Full URL
getLogger()
Returns the logger reference.
public
getLogger() : LoggerInterface|null
Return values
LoggerInterface|nullgetPath()
Generates a path based on the base URL and a provided relative path.
public
getPath([string $path = Char::EMPTY ][, array<string|int, mixed>|null $params = null ][, bool $useNow = false ]) : string
Parameters
- $path : string = Char::EMPTY
-
Relative path to append to the base URL
- $params : array<string|int, mixed>|null = null
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_parameter with the current timestamp
Return values
string —Full path
info()
Interesting events.
public
info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: User logs in, SQL logs.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
initializeBaseUrl()
Initializes the internal `baseUrl` property.
public
initializeBaseUrl([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
The value can come from:
- the
$initarray (keyControllerParam::BASE_URL), - the DI container if provided and contains the key
ControllerParam::BASE_URL, - otherwise it remains an empty string.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array
- $container : ContainerInterface|null = null
-
Optional DI container to fetch the base URL
Tags
Return values
static —Returns the current instance for method chaining
initializeJsonOptions()
Initialize the internal $jsonOptions property.
public
initializeJsonOptions([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
Parameters
- $init : array<string|int, mixed> = []
- $container : ContainerInterface|null = null
Tags
Return values
staticinitializeLoggable()
Initialize the loggable flag.
public
initializeLoggable([bool|array<string|int, mixed>|null $init = null ][, ContainerInterface|null $container = null ][, bool|array<string|int, mixed>|null $defaultValue = false ]) : static
Parameters
- $init : bool|array<string|int, mixed>|null = null
-
The definition to initialize the loggable property.
- $container : ContainerInterface|null = null
- $defaultValue : bool|array<string|int, mixed>|null = false
-
The default value if the $init argument is not defined.
Tags
Return values
staticinitializeLogger()
Initializes the logger reference for the current instance.
public
initializeLogger([array<string|int, mixed>|LoggerInterface|string|null $init = null ][, ContainerInterface|null $container = null ][, bool $useDefault = true ]) : static
This method accepts either:
- A LoggerInterface instance
- An associative array containing a logger reference under the static::LOGGER key
- A string representing a service ID or class name resolvable by the container
nullor an empty value, which will default to LoggerInterface::class depending on the$useDefaultparameter.
If a dependency injection container is provided, the method will attempt to
resolve the logger service from it. If no valid logger can be resolved, the
$this->logger property will be set to null.
Parameters
- $init : array<string|int, mixed>|LoggerInterface|string|null = null
-
Logger initialization data. May be an instance, an array with a logger entry, a string service ID/class name, or
null. - $container : ContainerInterface|null = null
-
Optional dependency injection container used to resolve the logger service.
- $useDefault : bool = true
-
Whether to use LoggerInterface::class as a fallback if
$initdoes not provide a valid logger string. Defaults totrue.
Tags
Return values
static —Returns the current instance for method chaining.
jsonResponse()
Return a JSON response
public
jsonResponse(ResponseInterface $response[, mixed $data = null ][, int $status = HttpStatusCode::OK ]) : ResponseInterface
Parameters
- $response : ResponseInterface
- $data : mixed = null
- $status : int = HttpStatusCode::OK
Return values
ResponseInterfacelog()
Logs with an arbitrary level.
public
log(mixed $level, string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $level : mixed
- $message : string|Stringable
- $context : array<string|int, mixed> = []
notice()
Normal but significant events.
public
notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
status()
Outputs a response status message.
public
status(ResponseInterface|null $response[, mixed $message = Char::EMPTY ][, int|string|null $code = 200 ][, array<string|int, mixed>|null $options = null ]) : ResponseInterface|null
Parameters
- $response : ResponseInterface|null
- $message : mixed = Char::EMPTY
-
The message to send.
- $code : int|string|null = 200
-
The status code.
- $options : array<string|int, mixed>|null = null
-
The options to passed-in in the status definition.
Tags
Return values
ResponseInterface|nullsuccess()
Outputs a success message with a JSON response. If the $response parameter is null, returns the $data parameter value.
public
success(ServerRequestInterface|null $request, ResponseInterface|null $response[, mixed $data = null ][, array<string|int, mixed>|null $init = null ]) : mixed
Ex: return $this->success( $request , $response , $data , [ Output::PARAMS => $request->getParams() ] ) ;
Parameters
- $request : ServerRequestInterface|null
-
The HTTP request reference.
- $response : ResponseInterface|null
-
The HTTP Response reference.
- $data : mixed = null
-
The data object to returns (output a JSON object).
- $init : array<string|int, mixed>|null = null
-
An associative definition to initialize the output object with the optional properties :
-
count (int) - The optional number of elements.
owner (object|array) - The optional owner reference.
options (array) - An associative array of optional properties to add in the output object.
params (array) - The optional params to passed-in the getCurrentPath() method when the url option is null.
status (int) - The optional status of the response.
total (int) - The optional total number of elements.
url (string) - The optional url to display.
warning()
Exceptional occurrences that are not errors.
public
warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []