[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Document/ -> JsonDocument.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 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\Document;
  11  
  12  use Joomla\CMS\Factory as CmsFactory;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * JsonDocument class, provides an easy interface to parse and display JSON output
  20   *
  21   * @link   http://www.json.org/
  22   * @since  1.7.0
  23   */
  24  class JsonDocument extends Document
  25  {
  26      /**
  27       * Document name
  28       *
  29       * @var    string
  30       * @since  1.7.0
  31       */
  32      protected $_name = 'joomla';
  33  
  34      /**
  35       * Class constructor
  36       *
  37       * @param   array  $options  Associative array of options
  38       *
  39       * @since  1.7.0
  40       */
  41      public function __construct($options = array())
  42      {
  43          parent::__construct($options);
  44  
  45          // Set mime type
  46          if (
  47              isset($_SERVER['HTTP_ACCEPT'])
  48              && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') === false
  49              && strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false
  50          ) {
  51              // Internet Explorer < 10
  52              $this->_mime = 'text/plain';
  53          } else {
  54              $this->_mime = 'application/json';
  55          }
  56  
  57          // Set document type
  58          $this->_type = 'json';
  59      }
  60  
  61      /**
  62       * Render the document.
  63       *
  64       * @param   boolean  $cache   If true, cache the output
  65       * @param   array    $params  Associative array of attributes
  66       *
  67       * @return  string  The rendered data
  68       *
  69       * @since  1.7.0
  70       */
  71      public function render($cache = false, $params = array())
  72      {
  73          /** @var \Joomla\CMS\Application\CMSApplication $app */
  74          $app = CmsFactory::getApplication();
  75  
  76          $app->allowCache($cache);
  77  
  78          if ($this->_mime === 'application/json') {
  79              // Browser other than Internet Explorer < 10
  80              $app->setHeader('Content-Disposition', 'attachment; filename="' . $this->getName() . '.json"', true);
  81          }
  82  
  83          parent::render($cache, $params);
  84  
  85          return $this->getBuffer();
  86      }
  87  
  88      /**
  89       * Returns the document name
  90       *
  91       * @return  string
  92       *
  93       * @since  1.7.0
  94       */
  95      public function getName()
  96      {
  97          return $this->_name;
  98      }
  99  
 100      /**
 101       * Sets the document name
 102       *
 103       * @param   string  $name  Document name
 104       *
 105       * @return  JsonDocument instance of $this to allow chaining
 106       *
 107       * @since   1.7.0
 108       */
 109      public function setName($name = 'joomla')
 110      {
 111          $this->_name = $name;
 112  
 113          return $this;
 114      }
 115  }


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