- md5
- 3a073e30d68931dfeccae8f17d89a98a
- sha1
- 0020d63740b434ad1861eab43e7b3d14165f924a
- sha256
- 3ad4ac5e1335646a1315b60dba7d9531583eb183c7affac66446964b478d22ce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
<?php
/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tobscure\JsonApi;
use JsonSerializable;
class Document implements JsonSerializable
{
use LinksTrait;
use MetaTrait;
const MEDIA_TYPE = 'application/vnd.api+json';
/**
* The included array.
*
* @var array
*/
protected $included = [];
/**
* The errors array.
*
* @var array
*/
protected $errors;
/**
* The jsonapi array.
*
* @var array
*/
protected $jsonapi;
/**
* The data object.
*
* @var ElementInterface
*/
protected $data;
/**
* @param ElementInterface $data
*/
public function __construct(?ElementInterface $data = null)
{
$this->data = $data;
}
/**
* Get included resources.
*
* @param \Tobscure\JsonApi\ElementInterface $element
* @param bool $includeParent
*
* @return \Tobscure\JsonApi\Resource[]
*/
protected function getIncluded(ElementInterface $element, $includeParent = false)
{
$included = [];
foreach ($element->getResources() as $resource) {
if ($resource->isIdentifier()) {
continue;
}
if ($includeParent) {
$included = $this->mergeResource($included, $resource);
} else {
$type = $resource->getType();
$id = $resource->getId();
}
foreach ($resource->getUnfilteredRelationships() as $relationship) {
$includedElement = $relationship->getData();
if (! $includedElement instanceof ElementInterface) {
continue;
}
foreach ($this->getIncluded($includedElement, true) as $child) {
// If this resource is the same as the top-level "data"
// resource, then we don't want it to show up again in the
// "included" array.
if (! $includeParent && $child->getType() === $type && $child->getId() === $id) {
continue;
}
$included = $this->mergeResource($included, $child);
}
}
}
$flattened = [];
array_walk_recursive($included, function ($a) use (&$flattened) {
$flattened[] = $a;
});
return $flattened;
}
/**
* @param \Tobscure\JsonApi\Resource[] $resources
* @param \Tobscure\JsonApi\Resource $newResource
*
* @return \Tobscure\JsonApi\Resource[]
*/
protected function mergeResource(array $resources, Resource $newResource)
{
$type = $newResource->getType();
$id = $newResource->getId();
if (isset($resources[$type][$id])) {
$resources[$type][$id]->merge($newResource);
} else {
$resources[$type][$id] = $newResource;
}
return $resources;
}
/**
* Set the data object.
*
* @param \Tobscure\JsonApi\ElementInterface $element
*
* @return $this
*/
public function setData(ElementInterface $element)
{
$this->data = $element;
return $this;
}
/**
* Set the errors array.
*
* @param array $errors
*
* @return $this
*/
public function setErrors($errors)
{
$this->errors = $errors;
return $this;
}
/**
* Set the jsonapi array.
*
* @param array $jsonapi
*
* @return $this
*/
public function setJsonapi($jsonapi)
{
$this->jsonapi = $jsonapi;
return $this;
}
/**
* Map everything to arrays.
*
* @return array
*/
public function toArray()
{
$document = [];
if (! empty($this->links)) {
$document['links'] = $this->links;
}
if (! empty($this->data)) {
$document['data'] = $this->data->toArray();
$resources = $this->getIncluded($this->data);
if (count($resources)) {
$document['included'] = array_map(function (Resource $resource) {
return $resource->toArray();
}, $resources);
}
}
if (! empty($this->meta)) {
$document['meta'] = $this->meta;
}
if (! empty($this->errors)) {
$document['errors'] = $this->errors;
}
if (! empty($this->jsonapi)) {
$document['jsonapi'] = $this->jsonapi;
}
return $document;
}
/**
* Map to string.
*
* @return string
*/
public function __toString()
{
return json_encode($this->toArray());
}
/**
* Serialize for JSON usage.
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->toArray();
}
}
The same content is in 95 releases, Joomla 5.3.0-beta3 to 6.3.0-alpha1. See them all, or every version of this file.