Back to JsonResponse class

Method __construct

public
__construct
(mixed $response = null, mixed $message = null, mixed $error = false, mixed $ignoreMessages = false)
Constructor
Parameters
  • mixed $response The Response data
  • string $message The main response message
  • bool $error True, if the success flag shall be set to false, defaults to false
  • bool $ignoreMessages True, if the message queue shouldn't be included, defaults to false
Since
  • 3.1
Class: JsonResponse
Project: Joomla

Method __construct - Source code

/**
 * Constructor
 *
 * @param   mixed    $response        The Response data
 * @param   string   $message         The main response message
 * @param   boolean  $error           True, if the success flag shall be set to false, defaults to false
 * @param   boolean  $ignoreMessages  True, if the message queue shouldn't be included, defaults to false
 *
 * @since   3.1
 */
public function __construct($response = null, $message = null, $error = false, $ignoreMessages = false)
{
    $this->message = $message;
    // Get the message queue if requested and available
    $app = Factory::getApplication();
    if (!$ignoreMessages && $app !== null && \is_callable(array($app, 'getMessageQueue'))) {
        $messages = $app->getMessageQueue();
        // Build the sorted messages list
        if (\is_array($messages) && \count($messages)) {
            foreach ($messages as $message) {
                if (isset($message['type']) && isset($message['message'])) {
                    $lists[$message['type']][] = $message['message'];
                }
            }
        }
        // If messages exist add them to the output
        if (isset($lists) && \is_array($lists)) {
            $this->messages = $lists;
        }
    }
    // Check if we are dealing with an error
    if ($response instanceof \Throwable) {
        // Prepare the error response
        $this->success = false;
        $this->message = $response->getMessage();
    } else {
        // Prepare the response data
        $this->success = !$error;
        $this->data = $response;
    }
}