helpers
Table of Contents
Functions
- applyContentHeaders() : ResponseInterface
- Applies the download content headers (`Content-Type`, `Content-Length`, `Content-Disposition`) to a PSR-7 response, toggled by {@see FileResponseOption}.
- computeETag() : string
- Computes an HTTP entity tag (`ETag`) validator for a file.
- etagMatches() : bool
- Tells whether an `If-None-Match` header value matches a given `ETag`.
- filterLanguages() : array<string, string|null>|null
- Filter an array or object of translations according to the given or available languages.
- getBodyParam() : mixed
- Retrieves a single parameter from the HTTP request body.
- getBodyParams() : array<string|int, mixed>
- Retrieves multiple parameters from the HTTP request body.
- getController() : Controller|null
- Retrieves a controller instance from a PSR-11 container if available.
- getParam() : mixed
- Retrieves a parameter from the HTTP request, supporting query string, body, or both.
- getParamArray() : array<string|int, mixed>|null
- Retrieves a parameter from the HTTP request and ensures it is an array.
- getParamBool() : bool|null
- Retrieves a parameter from the HTTP request and converts it to a boolean.
- getParamFloat() : float|null
- Retrieves a parameter from the HTTP request and ensures it is a float number.
- getParamFloatRange() : float|null
- Retrieves a float parameter from the request and clamps it within a given range.
- getParamI18n() : array<string|int, mixed>|null
- Retrieves an i18n parameter from the HTTP request, optionally filtered by allowed languages.
- getParamInt() : int|null
- Retrieves a parameter from the HTTP request and ensures it is a int number.
- getParamIntRange() : int|null
- Retrieves an integer parameter from the request and clamps it within a given range.
- getParamNumberRange() : int|float|null
- Retrieves a numeric parameter from the request and clamps it within a given range.
- getParamString() : string|null
- Retrieves a parameter from the HTTP request and ensures it is a string.
- getQueryParam() : mixed
- Retrieves a single parameter from the HTTP request query string.
- parseRangeHeader() : array{0: int, 1: int}|false|null
- Parses an HTTP `Range` header into a single satisfiable byte interval.
- translate() : mixed
- Retrieve the translation for a specific language, with optional fallback to a default language.
Functions
applyContentHeaders()
Applies the download content headers (`Content-Type`, `Content-Length`, `Content-Disposition`) to a PSR-7 response, toggled by {@see FileResponseOption}.
applyContentHeaders(ResponseInterface $response, string $file[, string|null $contentType = null ][, array<string|int, mixed> $options = [] ][, bool $defaultOn = true ]) : ResponseInterface
Shared by the file/archive/image/encryption response helpers so the header logic lives in one place.
Parameters
- $response : ResponseInterface
-
The PSR-7 response to decorate.
- $file : string
-
Path of the file whose size/MIME type back the headers (must exist).
- $contentType : string|null = null
-
The
Content-Typeto advertise; when null,mime_content_type($file)is used. - $options : array<string|int, mixed> = []
-
Header switches keyed by FileResponseOption:
useContentType(bool) emitContent-Type.useContentLength(bool) emitContent-Length(file size).useContentDisposition(bool) emitContent-Disposition.contentDisposition(string) value to use (defaults toattachment; filename=<basename>).
- $defaultOn : bool = true
-
Default state of the three switches when absent from
$options.
Tags
Return values
ResponseInterface —The response with the requested headers applied.
computeETag()
Computes an HTTP entity tag (`ETag`) validator for a file.
computeETag(string $file[, bool $weak = false ][, bool $hashContent = false ]) : string
By default the tag is derived from the file metadata (mtime-size, in
hexadecimal) so no content read is required — this keeps large-file streaming
cheap. Pass $hashContent = true to build an exact, byte-level tag from
md5_file() instead (reads the whole file).
The returned value is a quoted opaque tag, prefixed with W/ when $weak is true.
Parameters
- $file : string
-
Absolute path of the file (must exist).
- $weak : bool = false
-
Emit a weak validator (
W/"...") instead of a strong one. - $hashContent : bool = false
-
Derive the tag from the file content (
md5_file()) instead of its metadata.
Tags
Return values
string —The ETag value, e.g. "18f-3e8" or W/"18f-3e8".
etagMatches()
Tells whether an `If-None-Match` header value matches a given `ETag`.
etagMatches(string $header, string $etag) : bool
Implements the RFC 7232 §3.2 semantics used for If-None-Match:
*matches any current representation;- otherwise the header is a comma-separated list of entity tags, compared with the
weak comparison function — the
W/prefix is ignored on both sides, soW/"x"and"x"are considered a match.
Parameters
- $header : string
-
The raw
If-None-Matchheader value. - $etag : string
-
The current
ETagof the resource (as produced by computeETag()).
Tags
Return values
bool —true when the precondition matches (the caller should answer 304 Not Modified).
filterLanguages()
Filter an array or object of translations according to the given or available languages.
filterLanguages(mixed $fields[, array<string|int, string>|null $languages = null ][, callable|null $sanitize = null ]) : array<string, string|null>|null
This helper transforms an input array/object from the client to prepare a multilingual (i18n) property. It keeps only string or null values, allows optional transformation or sanitization via a callback.
Note: this helper is permissive on input shape — invalid inputs (string, scalar, etc.) silently return null rather than throwing. Callers that need to reject invalid shapes (e.g. to return a 422) must validate the raw input upstream before calling this helper.
Parameters
- $fields : mixed
-
Input translations (array<string,string|null> or object). Any other shape (string, scalar, …) is treated as invalid and ignored — the function returns null. Type validation must be done upstream by callers.
- $languages : array<string|int, string>|null = null
-
Optional array of languages to filter the i18n definitions. If null, no filtering is applied.
- $sanitize : callable|null = null
-
Optional callback to transform or sanitize each value. Signature:
fn(string|null $value, string $lang): string|null
Tags
Return values
array<string, string|null>|null —Filtered translations matching the languages, or null if input is empty.
getBodyParam()
Retrieves a single parameter from the HTTP request body.
getBodyParam(ServerRequestInterface|null $request, string $name) : mixed
This helper extracts a value from the parsed request body ($request->getParsedBody()),
supporting dot notation for nested structures (e.g. 'user.address.city').
The body is internally normalized into a fully associative array using toAssociativeArray(), ensuring compatibility with both array and stdClass-based JSON payloads.
It internally uses hasKeyValue() and getKeyValue() from the oihana\core\accessors namespace.
If the request is null, or if the specified parameter does not exist, the function returns null.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or nested key path (e.g.
'geo.latitude').
Tags
Return values
mixed —The parameter value if found, or null otherwise.
getBodyParams()
Retrieves multiple parameters from the HTTP request body.
getBodyParams(ServerRequestInterface|null $request[, array<string|int, mixed> $names = [] ]) : array<string|int, mixed>
This helper extracts one or more values from the parsed request body ($request->getParsedBody()),
supporting dot notation for nested structures (e.g. 'user.address.city').
Each requested key from $names is resolved recursively via
hasKeyValue() and getKeyValue(), and reassembled into a new associative
array using setKeyValue(). The request body is first normalized into a pure associative
array using toAssociativeArray(), ensuring compatibility with both array and stdClass payloads.
If the request is null, the function returns null.
If none of the requested keys exist, an empty array is returned.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance. If
null, no extraction is performed. - $names : array<string|int, mixed> = []
-
A list of parameter names (keys or dot-notated paths) to extract.
Tags
Return values
array<string|int, mixed> —An associative array of extracted values. Nested keys are preserved according to dot notation.
getController()
Retrieves a controller instance from a PSR-11 container if available.
getController([array<string|int, mixed>|string|null|Controller $definition = null ][, ContainerInterface|null $container = null ][, Controller|null $default = null ]) : Controller|null
This function attempts to fetch a controller by its identifier ($id) from the given container.
If the container is provided and contains the specified entry, it is resolved and returned
if it is an instance of Controller. Otherwise, the optional $default controller is returned.
Parameters
- $definition : array<string|int, mixed>|string|null|Controller = null
-
The controller definition within the container.
- $container : ContainerInterface|null = null
-
The PSR-11 container to fetch the controller from (optional).
- $default : Controller|null = null
-
A fallback controller to return if the container does not provide one (optional).
Tags
Return values
Controller|null —The resolved controller instance or the provided default value, or null if none found.
getParam()
Retrieves a parameter from the HTTP request, supporting query string, body, or both.
getParam(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $default = [] ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : mixed
This helper searches for the requested parameter $name in the request according to the
specified $strategy:
HttpParamStrategy::QUERY→ only query string parameters.HttpParamStrategy::BODY→ only parsed body parameters.HttpParamStrategy::BOTH→ query string first, then body.
Nested keys are supported via dot notation (e.g., 'user.profile.email').
Body parameters are normalized to an associative array using toAssociativeArray().
If the parameter is not found:
- Returns the corresponding value in
$default[$name]if present. - Returns
nullif no default is provided. - Throws
DI\NotFoundExceptionif$throwableis true.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $default : array<string|int, mixed> = []
-
Optional default values as an associative array.
- $strategy : string|null = HttpParamStrategy::BOTH
-
One of
HttpParamStrategy::QUERY|BODY|BOTH. Default: BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default: false.
Tags
Return values
mixed —The parameter value if found, otherwise the default value or null.
getParamArray()
Retrieves a parameter from the HTTP request and ensures it is an array.
getParamArray(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, array<string|int, mixed>|null $defaultValue = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : array<string|int, mixed>|null
This helper calls getParam() and checks the returned value:
- If the value is an array, it is returned.
- Otherwise, the
$defaultValueis returned. - If
$throwableis true, aNotFoundExceptionmay be thrown bygetParam().
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $args : array<string|int, mixed> = []
-
Optional default values passed to
getParam(). - $defaultValue : array<string|int, mixed>|null = null
-
Value returned if the parameter is missing or not an array. Default is null.
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default false.
Tags
Return values
array<string|int, mixed>|null —The parameter value if it is an array, otherwise $defaultValue or null.
getParamBool()
Retrieves a parameter from the HTTP request and converts it to a boolean.
getParamBool(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, bool|null $defaultValue = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : bool|null
This helper calls getParam() and converts the returned value to a PHP boolean according to standard boolean representations:
true/false(boolean)"true"/"false"(string)"1"/"0"(string)"yes"/"no"(string)"on"/"off"(string)1/0(integer)
If the value cannot be interpreted as a boolean, the provided $defaultValue is returned.
If $request is null or the parameter is missing, $defaultValue is returned unless
$throwable is set to true, in which case a NotFoundException is thrown.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $args : array<string|int, mixed> = []
-
Optional defaults passed to getParam().
- $defaultValue : bool|null = null
-
Value returned if the parameter is missing or not a boolean. Default is null.
- $strategy : string|null = HttpParamStrategy::BOTH
-
Source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH. - $throwable : bool = false
-
Whether to throw NotFoundException if parameter is missing. Default false.
Tags
Return values
bool|null —The boolean value of the parameter, or $defaultValue/null if not found or unrecognized.
getParamFloat()
Retrieves a parameter from the HTTP request and ensures it is a float number.
getParamFloat(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, float|null $defaultValue = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : float|null
This helper calls getParam() and converts the returned value to a float if set.
- If the value is
nullor missing, the$defaultValueis returned. - If
$throwableis true, a NotFoundException may be thrown bygetParam().
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $args : array<string|int, mixed> = []
-
Optional default values passed to
getParam(). - $defaultValue : float|null = null
-
Value returned if the parameter is missing or not set. Default is null.
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default false.
Tags
Return values
float|null —The parameter value cast to float if present, otherwise $defaultValue or null.
getParamFloatRange()
Retrieves a float parameter from the request and clamps it within a given range.
getParamFloatRange(ServerRequestInterface|null $request, string $name, float $min, float $max[, float|null $defaultValue = null ][, array<string|int, mixed> $args = [] ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : float|null
Wrapper around getParamNumberRange() that ensures float return type.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance. Can be null.
- $name : string
-
The parameter name or dot-notated path.
- $min : float
-
Minimum allowed float value.
- $max : float
-
Maximum allowed float value.
- $defaultValue : float|null = null
-
Value returned if the parameter is missing or not numeric. Default null.
- $args : array<string|int, mixed> = []
-
Optional defaults passed to getParam().
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default BOTH. - $throwable : bool = false
-
Whether to throw NotFoundException if the parameter is missing. Default false.
Tags
Return values
float|null —The float value clamped to [$min, $max], or $defaultValue/null if missing or invalid.
getParamI18n()
Retrieves an i18n parameter from the HTTP request, optionally filtered by allowed languages.
getParamI18n(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $default = [] ][, array<string|int, string>|null $languages = null ][, callable|null $sanitize = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : array<string|int, mixed>|null
This helper retrieves a parameter $name from the PSR-7 $request, supporting query string,
body, or both (HttpParamStrategy). The parameter can be an array or object of translations
(e.g., ['fr' => 'Bonjour', 'en' => 'Hello']). It then filters the values according to $languages
and optionally applies a $sanitize callback on each value.
Nested keys in the request are supported via dot notation (e.g., 'user.profile.email').
If the parameter is not found:
- Returns
$default[$name]if present. - Returns
nullif no default is provided. - Throws
NotFoundExceptionif$throwableis true.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $default : array<string|int, mixed> = []
-
Optional default values as an associative array.
- $languages : array<string|int, string>|null = null
-
Optional array of languages to filter the i18n definitions. If null, no filtering is applied.
- $sanitize : callable|null = null
-
Optional callback to transform or sanitize each value. Signature:
fn(string|null $value, string $lang): string|null - $strategy : string|null = HttpParamStrategy::BOTH
-
One of
HttpParamStrategy::QUERY|BODY|BOTH. Default: BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default: false.
Tags
Return values
array<string|int, mixed>|null —The i18n value if found, otherwise the default value or null.
getParamInt()
Retrieves a parameter from the HTTP request and ensures it is a int number.
getParamInt(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, int|null $defaultValue = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : int|null
This helper calls getParam() and converts the returned value to a int if set.
- If the value is
nullor missing, the$defaultValueis returned. - If
$throwableis true, a NotFoundException may be thrown bygetParam().
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $args : array<string|int, mixed> = []
-
Optional default values passed to
getParam(). - $defaultValue : int|null = null
-
Value returned if the parameter is missing or not set. Default is null.
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default false.
Tags
Return values
int|null —The parameter value cast to int if present, otherwise $defaultValue or null.
getParamIntRange()
Retrieves an integer parameter from the request and clamps it within a given range.
getParamIntRange(ServerRequestInterface|null $request, string $name, int $min, int $max[, int|null $defaultValue = null ][, array<string|int, mixed> $args = [] ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : int|null
Wrapper around getParamNumberRange() that ensures integer return type.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance. Can be null.
- $name : string
-
The parameter name or dot-notated path.
- $min : int
-
Minimum allowed integer value.
- $max : int
-
Maximum allowed integer value.
- $defaultValue : int|null = null
-
Value returned if the parameter is missing or not numeric. Default null.
- $args : array<string|int, mixed> = []
-
Optional defaults passed to getParam().
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default BOTH. - $throwable : bool = false
-
Whether to throw NotFoundException if the parameter is missing. Default false.
Tags
Return values
int|null —The integer value clamped to [$min, $max], or $defaultValue/null if missing or invalid.
getParamNumberRange()
Retrieves a numeric parameter from the request and clamps it within a given range.
getParamNumberRange(ServerRequestInterface|null $request, string $name, int|float $min, int|float $max[, int|float|null $defaultValue = null ][, array<string|int, mixed> $args = [] ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : int|float|null
This helper calls getParam() and:
- Converts the value to int or float.
- Returns
$defaultValueif the parameter is missing or not numeric. - Clamps the value between
$minand$max. - Can throw NotFoundException if
$throwableis true and parameter is missing.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 request instance.
- $name : string
-
Parameter name or dot-notated path.
- $min : int|float
-
Minimum allowed value.
- $max : int|float
-
Maximum allowed value.
- $defaultValue : int|float|null = null
-
Value returned if missing or invalid. Default null.
- $args : array<string|int, mixed> = []
-
Optional defaults passed to getParam().
- $strategy : string|null = HttpParamStrategy::BOTH
-
Source to search: BOTH, QUERY, BODY. Default BOTH.
- $throwable : bool = false
-
Whether to throw NotFoundException if missing. Default false.
Tags
Return values
int|float|null —The numeric value clamped to the range [$min, $max], or $defaultValue/null if missing or invalid.
getParamString()
Retrieves a parameter from the HTTP request and ensures it is a string.
getParamString(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, string|null $defaultValue = null ][, string|null $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : string|null
This helper calls getParam() and converts the returned value to a string if set.
- If the value is
nullor missing, the$defaultValueis returned. - If
$throwableis true, a NotFoundException may be thrown bygetParam().
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance.
- $name : string
-
The parameter name or dot-notated path.
- $args : array<string|int, mixed> = []
-
Optional default values passed to
getParam(). - $defaultValue : string|null = null
-
Value returned if the parameter is missing or null. Default is null.
- $strategy : string|null = HttpParamStrategy::BOTH
-
Which source to search:
HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH. - $throwable : bool = false
-
Whether to throw a
NotFoundExceptionif parameter is missing. Default false.
Tags
Return values
string|null —The parameter value cast to string if present, otherwise $defaultValue or null.
getQueryParam()
Retrieves a single parameter from the HTTP request query string.
getQueryParam(ServerRequestInterface|null $request, string $name) : mixed
This helper extracts a value from the query parameters ($request->getQueryParams()),
supporting dot notation for nested structures (e.g. 'filter.page').
It internally uses hasKeyValue() and getKeyValue() from the
oihana\core\accessors namespace.
If the request is null, or if the specified parameter does not exist, the function returns null.
Parameters
- $request : ServerRequestInterface|null
-
The PSR-7 server request instance. If
null, no extraction is performed. - $name : string
-
The query parameter name or nested key path (e.g.
'filter.page').
Tags
Return values
mixed —The parameter value if found, or null otherwise.
parseRangeHeader()
Parses an HTTP `Range` header into a single satisfiable byte interval.
parseRangeHeader(string $rangeHeader, int $fileSize) : array{0: int, 1: int}|false|null
Supports the common single-range forms: bytes=0-499, bytes=500- (open-ended)
and bytes=-500 (suffix / last N bytes). Multi-range requests are intentionally
not honored and resolve to null (the caller should serve the full content).
Parameters
- $rangeHeader : string
-
The raw
Rangeheader value. - $fileSize : int
-
The total size of the file in bytes.
Tags
Return values
array{0: int, 1: int}|false|null —[start, end](inclusive, clamped) for a satisfiable single range →206;falsewhen a range is present but unsatisfiable →416;nullwhen there is no usable single range (absent / malformed / multi-range) → full200.
translate()
Retrieve the translation for a specific language, with optional fallback to a default language.
translate(array<string, mixed>|object|null $fields[, string|null $lang = null ][, string|null $default = null ]) : mixed
This version is safer: if the requested language and the fallback are missing, it returns null
instead of returning the full array/object.
Example usage:
$translations =
[
'fr' => 'Bonjour',
'en' => 'Hello',
'es' => 'Hola'
];
translate( $translations , 'en' ); // 'Hello'
translate( $translations , 'de' , 'fr' ); // 'Bonjour' (fallback)
translate( $translations , 'it' , 'de' ); // null
translate( $translations ); // ['fr' => 'Bonjour', 'en' => 'Hello', 'es' => 'Hola']
$translationsObj = (object) $translations ;
translate( $translationsObj , null ) ; // (object) ['fr' => 'Bonjour', 'en' => 'Hello', 'es' => 'Hola']
Parameters
- $fields : array<string, mixed>|object|null
-
Array or object of translations keyed by language codes.
- $lang : string|null = null
-
Desired language code. If null, returns all translations.
- $default : string|null = null
-
Optional fallback language code if
$langis missing.
Tags
Return values
mixed —Returns the translation for the requested language, the fallback, all translations (if $lang is null), or null if no match.