[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright   (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Document;
  11  
  12  use Joomla\CMS\Factory;
  13  use Tobscure\JsonApi\Document;
  14  use Tobscure\JsonApi\ElementInterface;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * JsonapiDocument class, provides an easy interface to parse output in JSON-API format.
  22   *
  23   * @link   http://www.jsonapi.org/
  24   * @since  4.0.0
  25   */
  26  class JsonapiDocument extends JsonDocument implements \JsonSerializable
  27  {
  28      /**
  29       * The JsonApi Document object.
  30       *
  31       * @var    Document
  32       * @since  4.0.0
  33       */
  34      protected $document;
  35  
  36      /**
  37       * Class constructor.
  38       *
  39       * @param   array  $options  Associative array of options
  40       *
  41       * @since  4.0.0
  42       */
  43      public function __construct($options = array())
  44      {
  45          parent::__construct($options);
  46  
  47          // Set mime type to JSON-API
  48          $this->_mime = 'application/vnd.api+json';
  49          $this->_type = 'jsonapi';
  50  
  51          if (\array_key_exists('api_document', $options) && $options['api_document'] instanceof Document) {
  52              $this->document = $options['api_document'];
  53          } else {
  54              $this->document = new Document();
  55          }
  56      }
  57  
  58      /**
  59       * Set the data object.
  60       *
  61       * @param   ElementInterface  $element  Element interface.
  62       *
  63       * @return  $this
  64       *
  65       * @since  4.0.0
  66       */
  67      public function setData(ElementInterface $element)
  68      {
  69          $this->document->setData($element);
  70  
  71          return $this;
  72      }
  73  
  74      /**
  75       * Set the errors array.
  76       *
  77       * @param   array  $errors  Error array.
  78       *
  79       * @return   $this
  80       *
  81       * @since  4.0.0
  82       */
  83      public function setErrors($errors)
  84      {
  85          $this->document->setErrors($errors);
  86  
  87          return $this;
  88      }
  89  
  90      /**
  91       * Set the JSON-API array.
  92       *
  93       * @param   array  $jsonapi  JSON-API array.
  94       *
  95       * @return   $this
  96       *
  97       * @since  4.0.0
  98       */
  99      public function setJsonapi($jsonapi)
 100      {
 101          $this->document->setJsonapi($jsonapi);
 102  
 103          return $this;
 104      }
 105  
 106      /**
 107       * Map everything to arrays.
 108       *
 109       * @return array
 110       *
 111       * @since  4.0.0
 112       */
 113      public function toArray()
 114      {
 115          return $this->document->toArray();
 116      }
 117  
 118      /**
 119       * Map to string.
 120       *
 121       * @return string
 122       *
 123       * @since  4.0.0
 124       */
 125      public function __toString()
 126      {
 127          return json_encode($this->toArray());
 128      }
 129  
 130      /**
 131       * Outputs the document.
 132       *
 133       * @param   boolean  $cache   If true, cache the output.
 134       * @param   array    $params  Associative array of attributes.
 135       *
 136       * @return  string  The rendered data.
 137       *
 138       * @since  4.0.0
 139       */
 140      public function render($cache = false, $params = array())
 141      {
 142          $app = Factory::getApplication();
 143  
 144          if ($mdate = $this->getModifiedDate()) {
 145              $app->modifiedDate = $mdate;
 146          }
 147  
 148          $app->mimeType = $this->_mime;
 149          $app->charSet  = $this->_charset;
 150  
 151          return json_encode($this->document);
 152      }
 153  
 154      /**
 155       * Serialize for JSON usage.
 156       *
 157       * @return array
 158       *
 159       * @since  4.0.0
 160       */
 161      #[\ReturnTypeWillChange]
 162      public function jsonSerialize()
 163      {
 164          return $this->toArray();
 165      }
 166  
 167      /**
 168       * Add a link to the output.
 169       *
 170       * @param   string  $key    The name of the link
 171       * @param   string  $value  The link
 172       *
 173       * @return  $this
 174       *
 175       * @since  4.0.0
 176       */
 177      public function addLink($key, $value)
 178      {
 179          $this->document->addLink($key, $value);
 180  
 181          return $this;
 182      }
 183  
 184      /**
 185       * Add a link to the output.
 186       *
 187       * @param   string  $key    The name of the metadata key
 188       * @param   string  $value  The value
 189       *
 190       * @return  $this
 191       *
 192       * @since  4.0.0
 193       */
 194      public function addMeta($key, $value)
 195      {
 196          $this->document->addMeta($key, $value);
 197  
 198          return $this;
 199      }
 200  }


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