Update JsonAPI helper to better handle input data without mangling
This commit is contained in:
parent
bcc7815ae6
commit
d0af6fd9e8
@ -89,10 +89,12 @@
|
||||
</section>
|
||||
|
||||
<?php if (count($characters) > 0): ?>
|
||||
<br />
|
||||
<hr />
|
||||
<h2>Characters</h2>
|
||||
<?php foreach($characters as $role => $list): ?>
|
||||
<h3><?= ucfirst($role) ?></h3>
|
||||
<section class="media-wrap flex flex-wrap flex-justify-start">
|
||||
<?php foreach($characters as $id => $char): ?>
|
||||
<?php foreach($list as $id => $char): ?>
|
||||
<?php if ( ! empty($char['image']['original'])): ?>
|
||||
<article class="character">
|
||||
<?php $link = $url->generate('character', ['slug' => $char['slug']]) ?>
|
||||
|
@ -195,6 +195,7 @@ final class JsonAPI {
|
||||
}
|
||||
|
||||
$organized[$type][$id] = $newItem;
|
||||
$organized[$type][$id]['original'] = $item;
|
||||
}
|
||||
|
||||
// Second pass, go through and fill missing relationships in the first pass
|
||||
@ -211,14 +212,10 @@ final class JsonAPI {
|
||||
$idKey = $props['data']['id'];
|
||||
$dataType = $props['data']['type'];
|
||||
|
||||
if ( ! array_key_exists($dataType, $organized))
|
||||
{
|
||||
$organized[$dataType] = [];
|
||||
}
|
||||
|
||||
$relationship =& $organized[$type][$id]['relationships'][$relType];
|
||||
unset($relationship['links']);
|
||||
unset($relationship['data']);
|
||||
$relationship['foo'] = TRUE;
|
||||
|
||||
if ($relType === $dataType)
|
||||
{
|
||||
@ -260,6 +257,14 @@ final class JsonAPI {
|
||||
foreach($item['relationships'] as $type => $ids)
|
||||
{
|
||||
$inlined[$key][$itemId]['relationships'][$type] = [];
|
||||
|
||||
if ( ! array_key_exists($type, $included)) continue;
|
||||
|
||||
if (array_key_exists('data', $ids ))
|
||||
{
|
||||
$ids = array_column($ids['data'], 'id');
|
||||
}
|
||||
|
||||
foreach($ids as $id)
|
||||
{
|
||||
$inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id];
|
||||
@ -282,12 +287,19 @@ final class JsonAPI {
|
||||
public static function organizeIncludes(array $includes): array
|
||||
{
|
||||
$organized = [];
|
||||
$types = array_unique(array_column($includes, 'type'));
|
||||
sort($types);
|
||||
|
||||
foreach ($types as $type)
|
||||
{
|
||||
$organized[$type] = [];
|
||||
}
|
||||
|
||||
foreach ($includes as $item)
|
||||
{
|
||||
$type = $item['type'];
|
||||
$id = $item['id'];
|
||||
$organized[$type] = $organized[$type] ?? [];
|
||||
|
||||
$organized[$type][$id] = $item['attributes'];
|
||||
|
||||
if (array_key_exists('relationships', $item))
|
||||
@ -310,17 +322,16 @@ final class JsonAPI {
|
||||
*/
|
||||
public static function organizeRelationships(array $relationships): array
|
||||
{
|
||||
$organized = [];
|
||||
$organized = $relationships;
|
||||
|
||||
foreach($relationships as $key => $data)
|
||||
{
|
||||
$organized[$key] = $organized[$key] ?? [];
|
||||
if ( ! array_key_exists('data', $data))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$organized[$key] = $organized[$key] ?? [];
|
||||
|
||||
foreach ($data['data'] as $item)
|
||||
{
|
||||
if (\is_array($item) && array_key_exists('id', $item))
|
||||
|
@ -372,9 +372,9 @@ final class Model {
|
||||
return new Anime();
|
||||
}
|
||||
|
||||
$transformed = $this->animeTransformer->transform($baseData);
|
||||
$transformed['included'] = JsonAPI::organizeIncluded($baseData['included']);
|
||||
return $transformed;
|
||||
return $this->animeTransformer->transform($baseData);
|
||||
// $transformed['included'] = JsonAPI::organizeIncluded($baseData['included']);
|
||||
// return $transformed;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -962,11 +962,14 @@ final class Model {
|
||||
'slug' => $slug
|
||||
],
|
||||
'fields' => [
|
||||
'characters' => 'slug,name,image'
|
||||
'categories' => 'slug,title',
|
||||
'characters' => 'slug,name,image',
|
||||
'mappings' => 'externalSite,externalId',
|
||||
'animeCharacters' => 'character,role',
|
||||
],
|
||||
'include' => ($type === 'anime')
|
||||
? 'staff,staff.person,categories,mappings,streamingLinks,animeCharacters.character'
|
||||
: 'staff,staff.person,categories,mappings,mangaCharacters.character,castings.character',
|
||||
: 'staff,staff.person,categories,mappings,mangaCharacters.character',
|
||||
]
|
||||
];
|
||||
|
||||
|
@ -50,6 +50,7 @@ final class AnimeTransformer extends AbstractTransformer {
|
||||
'episode_length' => $item['episodeLength'],
|
||||
'genres' => $item['genres'],
|
||||
'id' => $item['id'],
|
||||
'included' => $item['included'],
|
||||
'show_type' => $this->string($item['showType'])->upperCaseFirst()->__toString(),
|
||||
'slug' => $item['slug'],
|
||||
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']),
|
||||
|
@ -291,13 +291,19 @@ final class Anime extends BaseController {
|
||||
return;
|
||||
}
|
||||
|
||||
if (array_key_exists('characters', $data['included']))
|
||||
if (array_key_exists('animeCharacters', $data['included']))
|
||||
{
|
||||
$animeCharacters = $data['included']['animeCharacters'];
|
||||
|
||||
|
||||
foreach($data['included']['characters'] as $id => $character)
|
||||
foreach ($animeCharacters as $rel)
|
||||
{
|
||||
$characters[$id] = $character['attributes'];
|
||||
$charId = $rel['relationships']['character']['data']['id'];
|
||||
$role = $rel['role'];
|
||||
|
||||
if (array_key_exists($charId, $data['included']['characters']))
|
||||
{
|
||||
$characters[$role][$charId] = $data['included']['characters'][$charId];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -305,13 +311,10 @@ final class Anime extends BaseController {
|
||||
{
|
||||
foreach ($data['included']['mediaStaff'] as $id => $person)
|
||||
{
|
||||
$personDetails = [];
|
||||
foreach ($person['relationships']['person']['people'] as $p)
|
||||
{
|
||||
$personDetails = $p['attributes'];
|
||||
}
|
||||
$personId = $person['relationships']['person']['data']['id'];
|
||||
$personDetails = $data['included']['people'][$personId];
|
||||
|
||||
$role = $person['attributes']['role'];
|
||||
$role = $person['role'];
|
||||
|
||||
if ( ! array_key_exists($role, $staff))
|
||||
{
|
||||
@ -325,9 +328,15 @@ final class Anime extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
uasort($characters, function ($a, $b) {
|
||||
uasort($characters['main'], function ($a, $b) {
|
||||
return $a['name'] <=> $b['name'];
|
||||
});
|
||||
uasort($characters['supporting'], function ($a, $b) {
|
||||
return $a['name'] <=> $b['name'];
|
||||
});
|
||||
|
||||
ksort($characters);
|
||||
ksort($staff);
|
||||
|
||||
// dump($characters);
|
||||
// dump($staff);
|
||||
|
Loading…
Reference in New Issue
Block a user