CResponse
in package
implements
IHttpStatus
Manages the response to user requests (manages views).
Table of Contents
Interfaces
- IHttpStatus
- Interface defining HTTP status code constants.
Properties
- $cache : ICache|null
- $format : string
- $logger : ILogger|null
- $request : CRequest
- $status_code : int
- $user : CUser|null
Methods
- __construct() : mixed
- Constructor
- AddMessage() : void
- Return an notify message
- ClearMessages() : void
- Clear all messages.
- CreateInstance() : CResponse
- Factory method to create a new CResponse instance.
- GetMessages() : array<string|int, mixed>
- Get all messages for the current request.
- GetStatusCode() : int
- Get the HTML status code
- Output() : string
- Method output
- SetStatusCode() : void
- Set the HTML status code
- SetUser() : void
- Set the user object.
- AddMessagesOnData() : void
- Add messages to the data array for output.
- AsDEBUG() : string
- Encode all data in Text format (for debug)
- AsJSON() : string
- Encode all data in JSON format.
- AsPLAIN() : string
- Encode all data in CSV/Plain Text format
- AsXML() : string
- Encode all data in XML format
- CreateComponentRenderer() : CComponent
- Create a configured CComponent instance for API responses.
- LoadPaletteCss() : string
- Load the CSS for the requested component palette.
- ProcessComponentsInData() : array<string|int, mixed>|null
- Process component tags in data array recursively.
- RenderComponentTags() : string
- Render all component tags in a string.
- SetHttpHeaders() : void
- Set HTTP headers for the response.
Properties
$cache
protected
ICache|null
$cache
The cache manager
$format
protected
string
$format
= DEFAULT_FORMAT_OUTPUT
The output format (JSON, XML, HTML, PLAIN)
$logger
protected
ILogger|null
$logger
The logger object
$request
protected
CRequest
$request
The request object
$status_code
protected
int
$status_code
= self::HTTP_STATUS_OK
The HTTP status code (default: 200 OK)
$user
protected
CUser|null
$user
The user object
Methods
__construct()
Constructor
public
__construct(CRequest $request, ICache|null $cache[, CUser|null $user = null ][, ILogger|null $logger = null ]) : mixed
Parameters
AddMessage()
Return an notify message
public
AddMessage(string $message, MessageType $type) : void
Parameters
- $message : string
-
text of notify
- $type : MessageType
ClearMessages()
Clear all messages.
public
ClearMessages() : void
CreateInstance()
Factory method to create a new CResponse instance.
public
static CreateInstance(CRequest $request, ICache|null $cache[, CUser|null $user = null ][, ILogger|null $logger = null ]) : CResponse
Parameters
- $request : CRequest
-
The request object.
- $cache : ICache|null
-
The cache manager.
- $user : CUser|null = null
-
The user object (optional).
- $logger : ILogger|null = null
-
The logger object (optional).
Return values
CResponse —A new CResponse instance.
GetMessages()
Get all messages for the current request.
public
GetMessages() : array<string|int, mixed>
Return values
array<string|int, mixed> —An array of messages.
GetStatusCode()
Get the HTML status code
public
GetStatusCode() : int
Return values
int —the status code
Output()
Method output
public
Output(array<string|int, mixed> $result) : string
Return the data results in the required format
Parameters
- $result : array<string|int, mixed>
-
data to tranform
Return values
string —all data results
SetStatusCode()
Set the HTML status code
public
SetStatusCode(int $status_code) : void
Parameters
- $status_code : int
SetUser()
Set the user object.
public
SetUser([CUser|null $user = null ]) : void
Parameters
- $user : CUser|null = null
-
The user object to set.
AddMessagesOnData()
Add messages to the data array for output.
protected
AddMessagesOnData(array<string|int, mixed> &$data) : void
If data is a numeric array (list), wrap it in {elements: [...], messages: [...]} to preserve JSON array format.
Parameters
- $data : array<string|int, mixed>
-
The data array to modify.
AsDEBUG()
Encode all data in Text format (for debug)
protected
AsDEBUG(array<string|int, array<string|int, mixed>|string>|null $data) : string
Parameters
- $data : array<string|int, array<string|int, mixed>|string>|null
-
array or object to send to client
Return values
string —data transformed into text format (for debugging)
AsJSON()
Encode all data in JSON format.
protected
AsJSON(array<string|int, mixed>|null $data) : string
By default, component tags are passed through as-is for client-side interpretation. If the path contains "/rendered/true", components are rendered server-side.
Parameters
- $data : array<string|int, mixed>|null
-
Array to send to client.
Return values
string —Data transformed into JSON format.
AsPLAIN()
Encode all data in CSV/Plain Text format
protected
AsPLAIN(array<string|int, array<string|int, mixed>|string>|null $data) : string
Parameters
- $data : array<string|int, array<string|int, mixed>|string>|null
-
array or object to send to client
Return values
string —data transformed into text format
AsXML()
Encode all data in XML format
protected
AsXML(array<string|int, mixed> $data[, bool $full = true ]) : string
Parameters
- $data : array<string|int, mixed>
-
array or object to send to client
- $full : bool = true
-
if true, it also exposes empty data
Return values
string —transforms the data into a string containing XML format
CreateComponentRenderer()
Create a configured CComponent instance for API responses.
protected
CreateComponentRenderer() : CComponent
Uses CURRENT_VIEW_FOLDER with fallback to DEFAULT_VIEW_FOLDER for framework-agnostic components.
Return values
CComponent —Configured component renderer.
LoadPaletteCss()
Load the CSS for the requested component palette.
protected
LoadPaletteCss() : string
Looks for the palette file in views/default/components/_palettes/. Falls back to the default palette if the requested one doesn't exist.
Return values
string —CSS content, or empty string if no palette file found.
ProcessComponentsInData()
Process component tags in data array recursively.
protected
ProcessComponentsInData(array<string|int, mixed>|null $data, array<string|int, mixed> &$component_assets) : array<string|int, mixed>|null
Scans all string values for
Parameters
- $data : array<string|int, mixed>|null
-
Data array to process.
- $component_assets : array<string|int, mixed>
-
Accumulated CSS/JS from components.
Return values
array<string|int, mixed>|null —Processed data with rendered components.
RenderComponentTags()
Render all component tags in a string.
protected
RenderComponentTags(string $content, CComponent $renderer, string &$css_accumulator, string &$js_accumulator) : string
Parameters
- $content : string
-
Content with component tags.
- $renderer : CComponent
-
Component renderer instance.
- $css_accumulator : string
-
CSS code accumulator.
- $js_accumulator : string
-
JS code accumulator.
Return values
string —Content with rendered components.
SetHttpHeaders()
Set HTTP headers for the response.
protected
SetHttpHeaders([bool $is_html_page = false ]) : void
Parameters
- $is_html_page : bool = false
-
True if rendering an HTML page, false for API response.