dolibarr 25.0.0-alpha
JsonResponse Class Reference

Class JsonResponse used for ajax responses in Dolibarr. More...

Collaboration diagram for JsonResponse:

Public Member Functions

 __construct ()
 JsonResponse constructor.
 
 getResponse ()
 return json encoded of object and send the HTTP response code if headers are not already sent
 
 getHttpResponseCode ()
 Get the HTTP response code that will be sent.
 
 setError ($msg='', $httpCode=self::HTTP_OK)
 Set the current response as an error response.
 
 setSuccess ($msg='', $httpCode=self::HTTP_OK)
 Set the response as a success.
 
 setHttpResponseCode ($httpCode)
 Define the HTTP response code used when sending the JSON response.
 
 output ()
 Send the JSON response to the client (JSON content-type header, HTTP response code and payload) and stop the script.
 

Public Attributes

 $changeHeaderForErrors = false
 When enabled, an error response sent with a 2xx HTTP code is upgraded to self\HTTP_BAD_REQUEST.
 
 $msg = ''
 returned message used usually as set event message
 

Private Member Functions

 isValidHttpErrorCode ($code)
 Check if HTTP code is a valid error code (4xx or 5xx).
 
 isValidHttpSuccessCode ($code)
 Check if HTTP code is a valid success code (2xx).
 

Detailed Description

Class JsonResponse used for ajax responses in Dolibarr.

Definition at line 23 of file jsonResponse.class.php.

Member Function Documentation

◆ getHttpResponseCode()

JsonResponse::getHttpResponseCode ( )

Get the HTTP response code that will be sent.

If no code was explicitly set, keep the historical behavior: 200 on success, 400 on error.

Returns
int

Definition at line 187 of file jsonResponse.class.php.

References isValidHttpSuccessCode().

Referenced by getResponse().

◆ getResponse()

JsonResponse::getResponse ( )

return json encoded of object and send the HTTP response code if headers are not already sent

Returns
string JSON

Definition at line 158 of file jsonResponse.class.php.

References $msg, and getHttpResponseCode().

Referenced by output().

◆ isValidHttpErrorCode()

JsonResponse::isValidHttpErrorCode ( $code)
private

Check if HTTP code is a valid error code (4xx or 5xx).

Parameters
int$codeHTTP code
Returns
bool

Definition at line 263 of file jsonResponse.class.php.

Referenced by setError(), and setHttpResponseCode().

◆ isValidHttpSuccessCode()

JsonResponse::isValidHttpSuccessCode ( $code)
private

Check if HTTP code is a valid success code (2xx).

Parameters
int$codeHTTP code
Returns
bool

Definition at line 284 of file jsonResponse.class.php.

Referenced by getHttpResponseCode(), setHttpResponseCode(), and setSuccess().

◆ output()

JsonResponse::output ( )

Send the JSON response to the client (JSON content-type header, HTTP response code and payload) and stop the script.

The database handler is closed by dol_shutdown().

Returns
void

Definition at line 299 of file jsonResponse.class.php.

References getResponse(), and top_httphead().

◆ setError()

JsonResponse::setError ( $msg = '',
$httpCode = self::HTTP_OK )

Set the current response as an error response.

Parameters
string$msgError message returned in the JSON response.
int$httpCodeHTTP response code. Defaults to 200 for application errors, as the JSON response already contains the error status and message through the "result" and "msg" fields. Use 4xx/5xx codes when the error must also be reported at the HTTP protocol level (permissions, invalid requests, server failures...).
Returns
void

Definition at line 211 of file jsonResponse.class.php.

References $msg, isValidHttpErrorCode(), and setHttpResponseCode().

◆ setHttpResponseCode()

JsonResponse::setHttpResponseCode ( $httpCode)

Define the HTTP response code used when sending the JSON response.

Allowed codes are the HTTP_* constants of this class.

Parameters
int$httpCodeHTTP response code.
Returns
bool True if the response code is allowed and applied, false otherwise.

Definition at line 247 of file jsonResponse.class.php.

References isValidHttpErrorCode(), and isValidHttpSuccessCode().

Referenced by setError(), and setSuccess().

◆ setSuccess()

JsonResponse::setSuccess ( $msg = '',
$httpCode = self::HTTP_OK )

Set the response as a success.

Parameters
string$msgSuccess message to return.
int$httpCodeHTTP status code (2xx only, default: 200).
Returns
void

Definition at line 229 of file jsonResponse.class.php.

References $msg, isValidHttpSuccessCode(), and setHttpResponseCode().


The documentation for this class was generated from the following file: