[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Response/ -> JsonResponse.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Response;
  11  
  12  use Joomla\CMS\Factory;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * JSON Response class.
  20   *
  21   * This class serves to provide the Joomla Platform with a common interface to access
  22   * response variables for e.g. Ajax requests.
  23   *
  24   * @since  3.1
  25   */
  26  class JsonResponse
  27  {
  28      /**
  29       * Determines whether the request was successful
  30       *
  31       * @var    boolean
  32       *
  33       * @since  3.1
  34       */
  35      public $success = true;
  36  
  37      /**
  38       * The main response message
  39       *
  40       * @var    string
  41       *
  42       * @since  3.1
  43       */
  44      public $message = null;
  45  
  46      /**
  47       * Array of messages gathered in the Application object
  48       *
  49       * @var    array
  50       *
  51       * @since  3.1
  52       */
  53      public $messages = null;
  54  
  55      /**
  56       * The response data
  57       *
  58       * @var    mixed
  59       *
  60       * @since  3.1
  61       */
  62      public $data = null;
  63  
  64      /**
  65       * Constructor
  66       *
  67       * @param   mixed    $response        The Response data
  68       * @param   string   $message         The main response message
  69       * @param   boolean  $error           True, if the success flag shall be set to false, defaults to false
  70       * @param   boolean  $ignoreMessages  True, if the message queue shouldn't be included, defaults to false
  71       *
  72       * @since   3.1
  73       */
  74      public function __construct($response = null, $message = null, $error = false, $ignoreMessages = false)
  75      {
  76          $this->message = $message;
  77  
  78          // Get the message queue if requested and available
  79          $app = Factory::getApplication();
  80  
  81          if (!$ignoreMessages && $app !== null && \is_callable(array($app, 'getMessageQueue'))) {
  82              $messages = $app->getMessageQueue();
  83  
  84              // Build the sorted messages list
  85              if (\is_array($messages) && \count($messages)) {
  86                  foreach ($messages as $message) {
  87                      if (isset($message['type']) && isset($message['message'])) {
  88                          $lists[$message['type']][] = $message['message'];
  89                      }
  90                  }
  91              }
  92  
  93              // If messages exist add them to the output
  94              if (isset($lists) && \is_array($lists)) {
  95                  $this->messages = $lists;
  96              }
  97          }
  98  
  99          // Check if we are dealing with an error
 100          if ($response instanceof \Throwable) {
 101              // Prepare the error response
 102              $this->success = false;
 103              $this->message = $response->getMessage();
 104          } else {
 105              // Prepare the response data
 106              $this->success = !$error;
 107              $this->data    = $response;
 108          }
 109      }
 110  
 111      /**
 112       * Magic toString method for sending the response in JSON format
 113       *
 114       * @return  string  The response in JSON format
 115       *
 116       * @since   3.1
 117       */
 118      public function __toString()
 119      {
 120          return json_encode($this);
 121      }
 122  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer