From 3842df13dbf910c208a24b8c4665503c6563507a Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 29 Oct 2018 15:48:54 -0400 Subject: [PATCH] Small code consistency update --- src/API/Kitsu/Model.php | 6 +- .../Kitsu/Transformer/MangaTransformer.php | 31 +++++----- ...MangaTransformerTest__testTransform__1.php | 62 +++++++++++++++++++ 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/src/API/Kitsu/Model.php b/src/API/Kitsu/Model.php index 27c46cbc..b8905775 100644 --- a/src/API/Kitsu/Model.php +++ b/src/API/Kitsu/Model.php @@ -373,8 +373,6 @@ final class Model { } return $this->animeTransformer->transform($baseData); - // $transformed['included'] = JsonAPI::organizeIncluded($baseData['included']); - // return $transformed; } /** @@ -614,9 +612,7 @@ final class Model { return new MangaPage([]); } - $transformed = $this->mangaTransformer->transform($baseData); - $transformed['included'] = JsonAPI::organizeIncluded($baseData['included']); - return $transformed; + return $this->mangaTransformer->transform($baseData); } /** diff --git a/src/API/Kitsu/Transformer/MangaTransformer.php b/src/API/Kitsu/Transformer/MangaTransformer.php index fd235df5..e645226e 100644 --- a/src/API/Kitsu/Transformer/MangaTransformer.php +++ b/src/API/Kitsu/Transformer/MangaTransformer.php @@ -16,6 +16,7 @@ namespace Aviat\AnimeClient\API\Kitsu\Transformer; +use Aviat\AnimeClient\API\JsonAPI; use Aviat\AnimeClient\Types\MangaPage; use Aviat\Ion\Transformer\AbstractTransformer; @@ -35,31 +36,33 @@ final class MangaTransformer extends AbstractTransformer { { $genres = []; - foreach($item['included'] as $included) - { - if ($included['type'] === 'categories') - { - $genres[] = $included['attributes']['title']; - } - } + $item['included'] = JsonAPI::organizeIncluded($item['included']); - sort($genres); + if (array_key_exists('categories', $item['included'])) + { + foreach ($item['included']['categories'] as $cat) + { + $genres[] = $cat['attributes']['title']; + } + sort($genres); + } $title = $item['canonicalTitle']; $rawTitles = array_values($item['titles']); $titles = array_unique(array_diff($rawTitles, [$title])); return new MangaPage([ + 'chapter_count' => $this->count($item['chapterCount']), + 'cover_image' => $item['posterImage']['small'], + 'genres' => $genres, 'id' => $item['id'], + 'included' => $item['included'], + 'manga_type' => $item['mangaType'], + 'synopsis' => $item['synopsis'], 'title' => $title, 'titles' => $titles, - 'cover_image' => $item['posterImage']['small'], - 'manga_type' => $item['mangaType'], - 'chapter_count' => $this->count($item['chapterCount']), - 'volume_count' => $this->count($item['volumeCount']), - 'synopsis' => $item['synopsis'], 'url' => "https://kitsu.io/manga/{$item['slug']}", - 'genres' => $genres, + 'volume_count' => $this->count($item['volumeCount']), ]); } diff --git a/tests/API/Kitsu/Transformer/__snapshots__/MangaTransformerTest__testTransform__1.php b/tests/API/Kitsu/Transformer/__snapshots__/MangaTransformerTest__testTransform__1.php index f29c6889..983cbbc7 100644 --- a/tests/API/Kitsu/Transformer/__snapshots__/MangaTransformerTest__testTransform__1.php +++ b/tests/API/Kitsu/Transformer/__snapshots__/MangaTransformerTest__testTransform__1.php @@ -5,6 +5,68 @@ array ( ), 'id' => '20286', + 'included' => + array ( + 'genres' => + array ( + 3 => + array ( + 'attributes' => + array ( + 'name' => 'Comedy', + 'slug' => 'comedy', + 'description' => NULL, + ), + ), + 24 => + array ( + 'attributes' => + array ( + 'name' => 'School', + 'slug' => 'school', + 'description' => NULL, + ), + ), + 16 => + array ( + 'attributes' => + array ( + 'name' => 'Slice of Life', + 'slug' => 'slice-of-life', + 'description' => '', + ), + ), + 14 => + array ( + 'attributes' => + array ( + 'name' => 'Romance', + 'slug' => 'romance', + 'description' => '', + ), + ), + 18 => + array ( + 'attributes' => + array ( + 'name' => 'Thriller', + 'slug' => 'thriller', + 'description' => NULL, + ), + ), + ), + 'mappings' => + array ( + 48014 => + array ( + 'attributes' => + array ( + 'externalSite' => 'myanimelist/manga', + 'externalId' => '26769', + ), + ), + ), + ), 'manga_type' => 'manga', 'synopsis' => 'Usa, a high-school student aspiring to begin a bachelor lifestyle, moves into a new apartment only to discover that he not only shares a room with a perverted roommate that has an obsession for underaged girls, but also that another girl, Ritsu, a love-at-first-sight, is living in the same building as well! (Source: Kirei Cake)',