Update JsonAPI helper to better handle input data without mangling

This commit is contained in:
Timothy Warren 2018-10-29 09:39:56 -04:00
parent 50b65d66e1
commit 2a6929c6ff
5 changed files with 54 additions and 28 deletions

View File

@ -89,10 +89,12 @@
</section> </section>
<?php if (count($characters) > 0): ?> <?php if (count($characters) > 0): ?>
<br /> <hr />
<h2>Characters</h2> <h2>Characters</h2>
<?php foreach($characters as $role => $list): ?>
<h3><?= ucfirst($role) ?></h3>
<section class="media-wrap flex flex-wrap flex-justify-start"> <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'])): ?> <?php if ( ! empty($char['image']['original'])): ?>
<article class="character"> <article class="character">
<?php $link = $url->generate('character', ['slug' => $char['slug']]) ?> <?php $link = $url->generate('character', ['slug' => $char['slug']]) ?>

View File

@ -195,6 +195,7 @@ final class JsonAPI {
} }
$organized[$type][$id] = $newItem; $organized[$type][$id] = $newItem;
$organized[$type][$id]['original'] = $item;
} }
// Second pass, go through and fill missing relationships in the first pass // Second pass, go through and fill missing relationships in the first pass
@ -211,14 +212,10 @@ final class JsonAPI {
$idKey = $props['data']['id']; $idKey = $props['data']['id'];
$dataType = $props['data']['type']; $dataType = $props['data']['type'];
if ( ! array_key_exists($dataType, $organized))
{
$organized[$dataType] = [];
}
$relationship =& $organized[$type][$id]['relationships'][$relType]; $relationship =& $organized[$type][$id]['relationships'][$relType];
unset($relationship['links']); unset($relationship['links']);
unset($relationship['data']); unset($relationship['data']);
$relationship['foo'] = TRUE;
if ($relType === $dataType) if ($relType === $dataType)
{ {
@ -258,8 +255,16 @@ final class JsonAPI {
$inlined[$key][$itemId] = $item; $inlined[$key][$itemId] = $item;
foreach($item['relationships'] as $type => $ids) foreach($item['relationships'] as $type => $ids)
{ {
$inlined[$key][$itemId]['relationships'][$type] = []; $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) foreach($ids as $id)
{ {
$inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id]; $inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id];
@ -282,12 +287,19 @@ final class JsonAPI {
public static function organizeIncludes(array $includes): array public static function organizeIncludes(array $includes): array
{ {
$organized = []; $organized = [];
$types = array_unique(array_column($includes, 'type'));
sort($types);
foreach ($types as $type)
{
$organized[$type] = [];
}
foreach ($includes as $item) foreach ($includes as $item)
{ {
$type = $item['type']; $type = $item['type'];
$id = $item['id']; $id = $item['id'];
$organized[$type] = $organized[$type] ?? [];
$organized[$type][$id] = $item['attributes']; $organized[$type][$id] = $item['attributes'];
if (array_key_exists('relationships', $item)) if (array_key_exists('relationships', $item))
@ -310,17 +322,16 @@ final class JsonAPI {
*/ */
public static function organizeRelationships(array $relationships): array public static function organizeRelationships(array $relationships): array
{ {
$organized = []; $organized = $relationships;
foreach($relationships as $key => $data) foreach($relationships as $key => $data)
{ {
$organized[$key] = $organized[$key] ?? [];
if ( ! array_key_exists('data', $data)) if ( ! array_key_exists('data', $data))
{ {
continue; continue;
} }
$organized[$key] = $organized[$key] ?? [];
foreach ($data['data'] as $item) foreach ($data['data'] as $item)
{ {
if (\is_array($item) && array_key_exists('id', $item)) if (\is_array($item) && array_key_exists('id', $item))

View File

@ -372,9 +372,9 @@ final class Model {
return new Anime(); return new Anime();
} }
$transformed = $this->animeTransformer->transform($baseData); return $this->animeTransformer->transform($baseData);
$transformed['included'] = JsonAPI::organizeIncluded($baseData['included']); // $transformed['included'] = JsonAPI::organizeIncluded($baseData['included']);
return $transformed; // return $transformed;
} }
/** /**
@ -962,11 +962,14 @@ final class Model {
'slug' => $slug 'slug' => $slug
], ],
'fields' => [ 'fields' => [
'characters' => 'slug,name,image' 'categories' => 'slug,title',
'characters' => 'slug,name,image',
'mappings' => 'externalSite,externalId',
'animeCharacters' => 'character,role',
], ],
'include' => ($type === 'anime') 'include' => ($type === 'anime')
? 'staff,staff.person,categories,mappings,streamingLinks,animeCharacters.character' ? 'staff,staff.person,categories,mappings,streamingLinks,animeCharacters.character'
: 'staff,staff.person,categories,mappings,mangaCharacters.character,castings.character', : 'staff,staff.person,categories,mappings,mangaCharacters.character',
] ]
]; ];

View File

@ -50,6 +50,7 @@ final class AnimeTransformer extends AbstractTransformer {
'episode_length' => $item['episodeLength'], 'episode_length' => $item['episodeLength'],
'genres' => $item['genres'], 'genres' => $item['genres'],
'id' => $item['id'], 'id' => $item['id'],
'included' => $item['included'],
'show_type' => $this->string($item['showType'])->upperCaseFirst()->__toString(), 'show_type' => $this->string($item['showType'])->upperCaseFirst()->__toString(),
'slug' => $item['slug'], 'slug' => $item['slug'],
'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']), 'status' => Kitsu::getAiringStatus($item['startDate'], $item['endDate']),

View File

@ -291,13 +291,19 @@ final class Anime extends BaseController {
return; return;
} }
if (array_key_exists('characters', $data['included'])) if (array_key_exists('animeCharacters', $data['included']))
{ {
$animeCharacters = $data['included']['animeCharacters'];
foreach ($animeCharacters as $rel)
foreach($data['included']['characters'] as $id => $character)
{ {
$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) foreach ($data['included']['mediaStaff'] as $id => $person)
{ {
$personDetails = []; $personId = $person['relationships']['person']['data']['id'];
foreach ($person['relationships']['person']['people'] as $p) $personDetails = $data['included']['people'][$personId];
{
$personDetails = $p['attributes'];
}
$role = $person['attributes']['role']; $role = $person['role'];
if ( ! array_key_exists($role, $staff)) 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']; return $a['name'] <=> $b['name'];
}); });
uasort($characters['supporting'], function ($a, $b) {
return $a['name'] <=> $b['name'];
});
ksort($characters);
ksort($staff);
// dump($characters); // dump($characters);
// dump($staff); // dump($staff);