From 1725a106fa95d5cfa9a1f77b4f32983d7964c7c3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 17 Aug 2020 14:01:55 -0400 Subject: [PATCH] Get character details page from GraphQL, still need to do castings section, see #27 --- app/views/character/details.php | 16 ++--- src/AnimeClient/API/Kitsu/Model.php | 13 +--- .../Kitsu/Queries/CharacterDetails.graphql | 60 ++++++++++++++++++- .../Transformer/CharacterTransformer.php | 59 ++++++++++++++++++ 4 files changed, 128 insertions(+), 20 deletions(-) diff --git a/app/views/character/details.php b/app/views/character/details.php index e079621a..8cbb7c79 100644 --- a/app/views/character/details.php +++ b/app/views/character/details.php @@ -42,15 +42,15 @@ use Aviat\AnimeClient\API\Kitsu; $anime): ?>
generate('anime.details', ['id' => $anime['attributes']['slug']]); - $titles = Kitsu::filterTitles($anime['attributes']); + $link = $url->generate('anime.details', ['id' => $anime['slug']]); + $titles = Kitsu::getTitles($anime['titles']); ?> - picture("images/anime/{$id}.webp") ?> + picture("images/anime/{$anime['id']}.webp") ?>
- +
@@ -70,15 +70,15 @@ use Aviat\AnimeClient\API\Kitsu; $manga): ?>
generate('manga.details', ['id' => $manga['attributes']['slug']]); - $titles = Kitsu::filterTitles($manga['attributes']); + $link = $url->generate('manga.details', ['id' => $manga['slug']]); + $titles = Kitsu::getTitles($manga['titles']); ?> - picture("images/manga/{$id}.webp") ?> + picture("images/manga/{$manga['id']}.webp") ?>
- +
diff --git a/src/AnimeClient/API/Kitsu/Model.php b/src/AnimeClient/API/Kitsu/Model.php index 29132130..b5fa5581 100644 --- a/src/AnimeClient/API/Kitsu/Model.php +++ b/src/AnimeClient/API/Kitsu/Model.php @@ -190,17 +190,8 @@ final class Model { */ public function getCharacter(string $slug): array { - return $this->requestBuilder->getRequest('characters', [ - 'query' => [ - 'filter' => [ - 'slug' => $slug, - ], - 'fields' => [ // For some characters, these filters cause issues...so leave them out - // 'anime' => 'canonicalTitle,abbreviatedTitles,titles,slug,posterImage', - // 'manga' => 'canonicalTitle,abbreviatedTitles,titles,slug,posterImage' - ], - 'include' => 'castings.person,castings.media' - ] + return $this->requestBuilder->runQuery('CharacterDetails', [ + 'slug' => $slug ]); } diff --git a/src/AnimeClient/API/Kitsu/Queries/CharacterDetails.graphql b/src/AnimeClient/API/Kitsu/Queries/CharacterDetails.graphql index 41e39a2f..6054f9e0 100644 --- a/src/AnimeClient/API/Kitsu/Queries/CharacterDetails.graphql +++ b/src/AnimeClient/API/Kitsu/Queries/CharacterDetails.graphql @@ -6,10 +6,11 @@ query ($slug: String!) { url } } + description names { alternatives canonical - #canonicalLocale + canonicalLocale localized }, primaryMedia { @@ -23,6 +24,63 @@ query ($slug: String!) { } type }, + media { + nodes { + media { + id + slug + titles { + alternatives + canonical + canonicalLocale + localized + } + posterImage { + original { + height + name + url + width + } + views { + height + name + url + width + } + } + type + } + voices { + nodes { + id + licensor { + name + } + locale + person { + id + slug + name + names { + alternatives + canonical + canonicalLocale + localized + } + image { + original { + height + width + url + name + } + } + } + } + } + } + } slug } } \ No newline at end of file diff --git a/src/AnimeClient/API/Kitsu/Transformer/CharacterTransformer.php b/src/AnimeClient/API/Kitsu/Transformer/CharacterTransformer.php index 4b19b8f6..12872b85 100644 --- a/src/AnimeClient/API/Kitsu/Transformer/CharacterTransformer.php +++ b/src/AnimeClient/API/Kitsu/Transformer/CharacterTransformer.php @@ -31,6 +31,65 @@ final class CharacterTransformer extends AbstractTransformer { * @return Character */ public function transform($characterData): Character + { + $data = $characterData['data']['findCharacterBySlug'] ?? []; + $castings = []; + $media = [ + 'anime' => [], + 'manga' => [], + ]; + + $names = array_unique( + array_merge( + [$data['names']['canonical']], + array_values($data['names']['localized']) + ) + ); + $name = array_shift($names); + + if (isset($data['media']['nodes'])) + { + [$media, $castings] = $this->organizeMediaAndVoices($data['media']['nodes']); + } + + return Character::from([ + 'castings' => $castings, + 'description' => $data['description']['en'], + 'id' => $data['id'], + 'media' => $media, + 'name' => $name, + 'names' => $names, + 'otherNames' => $data['names']['alternatives'], + ]); + } + + protected function organizeMediaAndVoices (array $data): array + { + if (empty($data)) + { + return [[], []]; + } + + $rawMedia = array_column($data, 'media'); + $rawAnime = array_filter($rawMedia, fn ($item) => $item['type'] === 'Anime'); + $rawManga = array_filter($rawMedia, fn ($item) => $item['type'] === 'Manga'); + + uasort($rawAnime, fn ($a, $b) => $a['titles']['canonical'] <=> $b['titles']['canonical']); + uasort($rawManga, fn ($a, $b) => $a['titles']['canonical'] <=> $b['titles']['canonical']); + + $media = [ + 'anime' => $rawAnime, + 'manga' => $rawManga, + ]; + + return [$media, []]; + } + + /** + * @param array $characterData + * @return Character + */ + public function oldTransform($characterData): Character { $data = JsonAPI::organizeData($characterData); $attributes = $data[0]['attributes'];