Source for file json.php

Documentation is available at json.php

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

Documentation generated on Tue, 19 Nov 2013 15:06:18 +0100 by phpDocumentor 1.4.3