JsonapiParse.php in JSON:API Include 8
File
src/JsonapiParse.php
View source
<?php
namespace Drupal\jsonapi_include;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
class JsonapiParse implements JsonapiParseInterface {
protected $included;
public function parse($response) {
$content = $this
->parseJsonContent($response);
return is_string($content) ? $content : Json::encode($content);
}
protected function isAssoc(array $arr) {
if ([] === $arr) {
return FALSE;
}
return array_keys($arr) !== range(0, count($arr) - 1);
}
protected function groupIncludes($object) {
$result = [];
$included = !empty($object['included']) ? $object['included'] : [];
array_walk($included, function ($resource, $index) use (&$result) {
$result[$resource['type']][$resource['id']] = $resource;
});
return $result;
}
protected function resolveAttributes($item) {
$resource = $item;
if (!empty($resource['attributes'])) {
foreach ($resource['attributes'] as $name => $value) {
$resource[$name] = $value;
}
unset($resource['attributes']);
}
return $resource;
}
protected function flattenIncluded($resource) {
if (isset($this->included[$resource['type']][$resource['id']])) {
$object = $this
->resolveAttributes($this->included[$resource['type']][$resource['id']]);
if (isset($resource['meta'])) {
$object['meta'] = $resource['meta'];
}
}
else {
$object = $resource;
}
$result = $this
->resolveRelationships($object);
return $result;
}
protected function isIncluded($resource) {
return isset($resource['type']) && isset($this->included[$resource['type']]);
}
protected function resolveData($data) {
if ($this
->isIncluded($data)) {
return $this
->flattenIncluded($data);
}
else {
return $data;
}
}
protected function resolveRelationshipData($links) {
if (empty($links['data'])) {
return $links;
}
$output = [];
if (!$this
->isAssoc($links['data'])) {
foreach ($links['data'] as $item) {
$output[] = $this
->resolveData($item);
}
}
else {
$output = $this
->resolveData($links['data']);
}
return $output;
}
protected function resolveRelationships($resource) {
if (empty($resource['relationships'])) {
return $resource;
}
foreach ($resource['relationships'] as $key => $value) {
$resource[$key] = $this
->resolveRelationshipData($value);
}
unset($resource['relationships']);
return $resource;
}
protected function parseResource($item) {
$attributes = $this
->resolveAttributes($item);
return $this
->resolveRelationships($attributes);
}
protected function parseJsonContent($response) {
if (is_string($response)) {
$json = Json::decode($response);
}
elseif (is_object($response)) {
$json = $response;
}
else {
return $response;
}
if (NestedArray::getValue($json, [
'jsonapi',
'parsed',
])) {
return $json;
}
if (isset($json['errors']) || empty($json['data'])) {
return $json;
}
$this->included = $this
->groupIncludes($json);
$data = [];
if (!$this
->isAssoc($json['data'])) {
foreach ($json['data'] as $item) {
$data[] = $this
->parseResource($item);
}
}
else {
$data = $this
->parseResource($json['data']);
}
if (isset($json['included'])) {
unset($json['included']);
}
$json['jsonapi']['parsed'] = TRUE;
$json['data'] = $data;
return $json;
}
}