- md5
- 8a5ee250f552dcfb3faa47edd23beb25
- sha1
- a8fd6f61d256a3bb257f2b0b0b85397c3e90a4b6
- sha256
- f36dddfbe994fde3f3f5219929342dbbb278fd0b2d408784b0fb9a6c94c1501e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Document;
use Joomla\CMS\Factory;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\ElementInterface;
// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* JsonapiDocument class, provides an easy interface to parse output in JSON-API format.
*
* @link http://www.jsonapi.org/
* @since 4.0.0
*/
class JsonapiDocument extends JsonDocument implements \JsonSerializable
{
/**
* The JsonApi Document object.
*
* @var Document
* @since 4.0.0
*/
protected $document;
/**
* Class constructor.
*
* @param array $options Associative array of options
*
* @since 4.0.0
*/
public function __construct($options = [])
{
parent::__construct($options);
// Set mime type to JSON-API
$this->_mime = 'application/vnd.api+json';
$this->_type = 'jsonapi';
if (\array_key_exists('api_document', $options) && $options['api_document'] instanceof Document) {
$this->document = $options['api_document'];
} else {
$this->document = new Document();
}
}
/**
* Set the data object.
*
* @param ElementInterface $element Element interface.
*
* @return $this
*
* @since 4.0.0
*/
public function setData(ElementInterface $element)
{
$this->document->setData($element);
return $this;
}
/**
* Set the errors array.
*
* @param array $errors Error array.
*
* @return $this
*
* @since 4.0.0
*/
public function setErrors($errors)
{
$this->document->setErrors($errors);
return $this;
}
/**
* Set the JSON-API array.
*
* @param array $jsonapi JSON-API array.
*
* @return $this
*
* @since 4.0.0
*/
public function setJsonapi($jsonapi)
{
$this->document->setJsonapi($jsonapi);
return $this;
}
/**
* Map everything to arrays.
*
* @return array
*
* @since 4.0.0
*/
public function toArray()
{
return $this->document->toArray();
}
/**
* Map to string.
*
* @return string
*
* @since 4.0.0
*/
public function __toString()
{
return json_encode($this->toArray());
}
/**
* Outputs the document.
*
* @param boolean $cache If true, cache the output.
* @param array $params Associative array of attributes.
*
* @return string The rendered data.
*
* @since 4.0.0
*/
public function render($cache = false, $params = [])
{
$app = Factory::getApplication();
if ($mdate = $this->getModifiedDate()) {
$app->modifiedDate = $mdate;
}
$app->mimeType = $this->_mime;
$app->charSet = $this->_charset;
return json_encode($this->document);
}
/**
* Serialize for JSON usage.
*
* @return array
*
* @since 4.0.0
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
/**
* Add a link to the output.
*
* @param string $key The name of the link
* @param string $value The link
*
* @return $this
*
* @since 4.0.0
*/
public function addLink($key, $value)
{
$this->document->addLink($key, $value);
return $this;
}
/**
* Add a link to the output.
*
* @param string $key The name of the metadata key
* @param string $value The value
*
* @return $this
*
* @since 4.0.0
*/
public function addMeta($key, $value)
{
$this->document->addMeta($key, $value);
return $this;
}
}
The same content is in 43 releases, Joomla 4.3.2-rc1 to 4.4.14. See them all, or every version of this file.