From d0af6fd9e8816c4cb76d2a3d28aa1478d88b257a Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 29 Oct 2018 09:39:56 -0400 Subject: [PATCH] Update JsonAPI helper to better handle input data without mangling --- app/views/anime/details.php | 6 ++-- src/API/JsonAPI.php | 31 +++++++++++++------ src/API/Kitsu/Model.php | 13 +++++--- .../Kitsu/Transformer/AnimeTransformer.php | 1 + src/Controller/Anime.php | 31 ++++++++++++------- 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/app/views/anime/details.php b/app/views/anime/details.php index fb500b89..013a804e 100644 --- a/app/views/anime/details.php +++ b/app/views/anime/details.php @@ -89,10 +89,12 @@ 0): ?> -
+

Characters

+ $list): ?> +

- $char): ?> + $char): ?>
generate('character', ['slug' => $char['slug']]) ?> diff --git a/src/API/JsonAPI.php b/src/API/JsonAPI.php index 989bad19..323af807 100644 --- a/src/API/JsonAPI.php +++ b/src/API/JsonAPI.php @@ -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) { @@ -258,8 +255,16 @@ final class JsonAPI { $inlined[$key][$itemId] = $item; 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)) diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 3a0751dc..19d8461e 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -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', ] ]; diff --git a/src/API/Kitsu/Transformer/AnimeTransformer.php b/src/API/Kitsu/Transformer/AnimeTransformer.php index ba23e4b6..2a4ee36a 100644 --- a/src/API/Kitsu/Transformer/AnimeTransformer.php +++ b/src/API/Kitsu/Transformer/AnimeTransformer.php @@ -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']), diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 93c5e37a..6ffe6aec 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -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);