OutputDocumentsTrait uses trait:short, trait:short
Table of Contents
Properties
- $baseUrl : string
- The application's base URL.
Methods
- 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.
- getPath() : string
- Generates a path based on the base URL and a provided relative path.
- initializeBaseUrl() : static
- Initializes the internal `baseUrl` property.
- 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.
- documentsResponse() : object|null
- The internal documents response returns strategy.
- getDocumentUrl() : string
- Invoked in the documentsResponse method to returns the document url.
- outputDocuments() : array<string|int, mixed>|object|null
- Outputs a list of documents.
Properties
$baseUrl
The application's base URL.
public
string
$baseUrl
= \oihana\enums\Char::EMPTY
Used as a prefix for all generated URLs.
Methods
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
getPath()
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
initializeBaseUrl()
Initializes the internal `baseUrl` property.
public
initializeBaseUrl([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
The value can come from:
- the
$init
array (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
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.
documentsResponse()
The internal documents response returns strategy.
protected
documentsResponse([ServerRequestInterface|null $request = null ][, ResponseInterface|null $response = null ][, array<string|int, mixed>|null $documents = null ][, array<string|int, mixed> $params = [] ][, array<string|int, mixed>|null $options = null ]) : object|null
Parameters
- $request : ServerRequestInterface|null = null
- $response : ResponseInterface|null = null
- $documents : array<string|int, mixed>|null = null
- $params : array<string|int, mixed> = []
- $options : array<string|int, mixed>|null = null
Return values
object|nullgetDocumentUrl()
Invoked in the documentsResponse method to returns the document url.
protected
getDocumentUrl([ServerRequestInterface|null $request = null ][, array<string|int, mixed> $params = [] ]) : string
By default, the method use the BaseUrlTrait::getCurrentPath() method, you can overrides it.
Parameters
- $request : ServerRequestInterface|null = null
- $params : array<string|int, mixed> = []
Return values
stringoutputDocuments()
Outputs a list of documents.
protected
outputDocuments([ServerRequestInterface|null $request = null ][, ResponseInterface|null|null $response = null ][, array<string|int, mixed>|null $documents = null ][, array<string|int, mixed> $params = [] ][, array<string|int, mixed>|null $options = null ]) : array<string|int, mixed>|object|null
Parameters
- $request : ServerRequestInterface|null = null
- $response : ResponseInterface|null|null = null
- $documents : array<string|int, mixed>|null = null
- $params : array<string|int, mixed> = []
- $options : array<string|int, mixed>|null = null