From a38c9712e6a4f5a840e0598de9be42b8d184af99 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 7 Dec 2018 10:22:16 -0500 Subject: [PATCH] Don't show media tabs with no media on character page, make stats on user page more resiliant --- app/views/character/details.php | 6 +-- src/API/Kitsu/Transformer/UserTransformer.php | 43 +++++++++++++------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/app/views/character/details.php b/app/views/character/details.php index 4b44a4d4..eb9a9c0c 100644 --- a/app/views/character/details.php +++ b/app/views/character/details.php @@ -27,10 +27,10 @@ use Aviat\AnimeClient\API\Kitsu; - +

Media

- + @@ -58,7 +58,7 @@ use Aviat\AnimeClient\API\Kitsu; - + diff --git a/src/API/Kitsu/Transformer/UserTransformer.php b/src/API/Kitsu/Transformer/UserTransformer.php index 35e4e83e..c5b05464 100644 --- a/src/API/Kitsu/Transformer/UserTransformer.php +++ b/src/API/Kitsu/Transformer/UserTransformer.php @@ -131,19 +131,38 @@ final class UserTransformer extends AbstractTransformer { return $output; } - private function organizeStats($stats, $data): array + private function organizeStats($stats, $data = []): array { - // $timeOnAnime = $this->formatAnimeTime($orgData['attributes']['lifeSpentOnAnime']); + $animeStats = []; + $mangaStats = []; + $otherStats = []; - return [ - 'Time spent watching anime:' => $this->formatAnimeTime($stats['anime-amount-consumed']['time']), - 'Anime series watched:' => number_format($stats['anime-amount-consumed']['media']), - 'Anime episodes watched:' => number_format($stats['anime-amount-consumed']['units']), - 'Manga series read:' => number_format($stats['manga-amount-consumed']['media']), - 'Manga chapters read:' => number_format($stats['manga-amount-consumed']['units']), - 'Posts:' => number_format($data['postsCount']), - 'Comments:' => number_format($data['commentsCount']), - 'Media Rated:' => number_format($data['ratingsCount']), - ]; + if (array_key_exists('anime-amount-consumed', $stats)) + { + $animeStats = [ + 'Time spent watching anime:' => $this->formatAnimeTime($stats['anime-amount-consumed']['time']), + 'Anime series watched:' => number_format($stats['anime-amount-consumed']['media']), + 'Anime episodes watched:' => number_format($stats['anime-amount-consumed']['units']), + ]; + } + + if (array_key_exists('manga-amount-consumed', $stats)) + { + $mangaStats = [ + 'Manga series read:' => number_format($stats['manga-amount-consumed']['media']), + 'Manga chapters read:' => number_format($stats['manga-amount-consumed']['units']), + ]; + } + + if ( ! empty($data)) + { + $otherStats = [ + 'Posts:' => number_format($data['postsCount']), + 'Comments:' => number_format($data['commentsCount']), + 'Media Rated:' => number_format($data['ratingsCount']), + ]; + } + + return array_merge($animeStats, $mangaStats, $otherStats); } } \ No newline at end of file