From f1893d97083af44f99802260097f54c602f0d779 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 4 Jan 2017 13:16:58 -0500 Subject: [PATCH] Manga lists and detail pages --- app/views/manga/cover.php | 6 +- app/views/manga/details.php | 8 +- app/views/manga/list.php | 6 +- src/API/Kitsu/Enum/MangaReadingStatus.php | 6 +- src/API/Kitsu/KitsuModel.php | 70 +++++++------- .../Transformer/MangaListTransformer.php | 51 ++++------ .../Kitsu/Transformer/MangaTransformer.php | 92 +++++++++++-------- src/Controller/Manga.php | 4 +- src/Model/Manga.php | 40 ++++---- 9 files changed, 146 insertions(+), 137 deletions(-) diff --git a/app/views/manga/cover.php b/app/views/manga/cover.php index 69b7fb63..5bcacab6 100644 --- a/app/views/manga/cover.php +++ b/app/views/manga/cover.php @@ -14,7 +14,6 @@ is_authenticated()): ?> @@ -43,8 +42,7 @@
- Volumes: / - + Volumes:
@@ -57,4 +55,4 @@ is_authenticated()): ?> - + \ No newline at end of file diff --git a/app/views/manga/details.php b/app/views/manga/details.php index 986fca43..2b204b7b 100644 --- a/app/views/manga/details.php +++ b/app/views/manga/details.php @@ -1,7 +1,7 @@
- <?= $data['title'] ?> cover image + <?= $data['title'] ?> cover image

@@ -26,9 +26,9 @@
-

- -

+

+ +


diff --git a/app/views/manga/list.php b/app/views/manga/list.php index 481d85e7..dbf00076 100644 --- a/app/views/manga/list.php +++ b/app/views/manga/list.php @@ -15,8 +15,8 @@ Title Rating - Chapters - Volumes + Completed Chapters + # of Volumes Type @@ -36,7 +36,7 @@ / 10 / - / + diff --git a/src/API/Kitsu/Enum/MangaReadingStatus.php b/src/API/Kitsu/Enum/MangaReadingStatus.php index 9d7f43e6..20d00712 100644 --- a/src/API/Kitsu/Enum/MangaReadingStatus.php +++ b/src/API/Kitsu/Enum/MangaReadingStatus.php @@ -24,8 +24,8 @@ use Aviat\Ion\Enum as BaseEnum; class MangaReadingStatus extends BaseEnum { const READING = 1; const PLAN_TO_READ = 2; - const DROPPED = 3; + const DROPPED = 5; const ON_HOLD = 4; - const COMPLETED = 5; + const COMPLETED = 3; } -// End of MangaReadingStatus.php +// End of MangaReadingStatus.php \ No newline at end of file diff --git a/src/API/Kitsu/KitsuModel.php b/src/API/Kitsu/KitsuModel.php index 86c769a5..845c6488 100644 --- a/src/API/Kitsu/KitsuModel.php +++ b/src/API/Kitsu/KitsuModel.php @@ -18,7 +18,7 @@ namespace Aviat\AnimeClient\API\Kitsu; use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\API\Kitsu\Transformer\{ - AnimeTransformer, AnimeListTransformer, MangaListTransformer + AnimeTransformer, AnimeListTransformer, MangaTransformer, MangaListTransformer }; use Aviat\Ion\Json; use GuzzleHttp\Exception\ClientException; @@ -47,9 +47,9 @@ class KitsuModel { */ protected $animeTransformer; - /** - * @var MangaListTransformer - */ + /** + * @var MangaListTransformer + */ protected $mangaListTransformer; /** @@ -62,6 +62,7 @@ class KitsuModel { $this->animeTransformer = new AnimeTransformer(); $this->animeListTransformer = new AnimeListTransformer(); + $this->mangaTransformer = new MangaTransformer(); $this->mangaListTransformer = new MangaListTransformer(); } @@ -98,16 +99,17 @@ class KitsuModel { return $this->animeTransformer->transform($baseData); } + public function getManga(string $mangaId): array + { + $baseData = $this->getRawMediaData('manga', $mangaId); + return $this->mangaTransformer->transform($baseData); + } + public function getRawAnimeData($animeId): array { return $this->getRawMediaData('anime', $animeId); } - public function getAnimeMedia($entryId): array - { - return $this->getRequest("library-entries/{$entryId}/media"); - } - public function getAnimeList($status): array { $options = [ @@ -139,34 +141,34 @@ class KitsuModel { } public function getMangaList($status): array - { - $options = [ - 'query' => [ - 'filter' => [ - 'user_id' => 2644, - 'media_type' => 'Manga', - 'status' => $status, - ], - 'include' => 'media', - 'page' => [ - 'offset' => 0, - 'limit' => 200 - ], - 'sort' => '-updated_at' - ] - ]; + { + $options = [ + 'query' => [ + 'filter' => [ + 'user_id' => 2644, + 'media_type' => 'Manga', + 'status' => $status, + ], + 'include' => 'media', + 'page' => [ + 'offset' => 0, + 'limit' => 200 + ], + 'sort' => '-updated_at' + ] + ]; - $data = $this->getRequest('library-entries', $options); - - foreach($data['data'] as $i => &$item) - { - $item['manga'] = $data['included'][$i]; - } + $data = $this->getRequest('library-entries', $options); - $transformed = $this->mangaListTransformer->transformCollection($data['data']); + foreach($data['data'] as $i => &$item) + { + $item['manga'] = $data['included'][$i]; + } - return $transformed; - } + $transformed = $this->mangaListTransformer->transformCollection($data['data']); + + return $transformed; + } private function getGenres(string $type, string $id): array { diff --git a/src/API/Kitsu/Transformer/MangaListTransformer.php b/src/API/Kitsu/Transformer/MangaListTransformer.php index 2a43be63..251f745c 100644 --- a/src/API/Kitsu/Transformer/MangaListTransformer.php +++ b/src/API/Kitsu/Transformer/MangaListTransformer.php @@ -6,12 +6,12 @@ * * PHP version 7 * - * @package AnimeListClient - * @author Timothy J. Warren + * @package AnimeListClient + * @author Timothy J. Warren * @copyright 2015 - 2016 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @version 4.0 - * @link https://github.com/timw4mail/HummingBirdAnimeClient + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient */ namespace Aviat\AnimeClient\API\Kitsu\Transformer; @@ -34,17 +34,15 @@ class MangaListTransformer extends AbstractTransformer { */ public function transform($item) { -?>
0) ? $manga['attributes']['chapterCount'] - : '-'; + : '-'; $total_volumes = ($manga['attributes']['volumeCount'] > 0) ? $manga['attributes']['volumeCount'] @@ -53,40 +51,29 @@ die(); $map = [ 'id' => $item['id'], 'chapters' => [ - 'read' => $item['chapters_read'], + 'read' => $item['attributes']['progress'], 'total' => $total_chapters ], 'volumes' => [ - 'read' => $item['volumes_read'], + 'read' => '-', //$item['attributes']['volumes_read'], 'total' => $total_volumes ], 'manga' => [ - 'title' => $manga['romaji_title'], + 'title' => $manga['attributes']['canonicalTitle'], 'alternate_title' => NULL, 'slug' => $manga['id'], - 'url' => 'https://hummingbird.me/manga/' . $manga['id'], - 'type' => $manga['manga_type'], - 'image' => $manga['poster_image_thumb'], - 'genres' => $manga['genres'], + 'url' => 'https://kitsu.io/manga/' . $manga['id'], + 'type' => $manga['attributes']['mangaType'], + 'image' => $manga['attributes']['posterImage']['small'], + 'genres' => [], //$manga['genres'], ], - 'reading_status' => $item['status'], - 'notes' => $item['notes'], - 'rereading' => (bool)$item['rereading'], - 'reread' => $item['reread_count'], + 'reading_status' => $item['attributes']['status'], + 'notes' => $item['attributes']['notes'], + 'rereading' => (bool)$item['attributes']['reconsuming'], + 'reread' => $item['attributes']['reconsumeCount'], 'user_rating' => $rating, ]; - if (array_key_exists('english_title', $manga)) - { - $diff = levenshtein($manga['romaji_title'], $manga['english_title']); - - // If the titles are REALLY similar, don't bother showing both - if ($diff >= 5) - { - $map['manga']['alternate_title'] = $manga['english_title']; - } - } - return $map; } diff --git a/src/API/Kitsu/Transformer/MangaTransformer.php b/src/API/Kitsu/Transformer/MangaTransformer.php index 9c7ac517..fa5013c8 100644 --- a/src/API/Kitsu/Transformer/MangaTransformer.php +++ b/src/API/Kitsu/Transformer/MangaTransformer.php @@ -1,39 +1,55 @@ - - * @copyright 2015 - 2016 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @version 4.0 - * @link https://github.com/timw4mail/HummingBirdAnimeClient - */ - -namespace Aviat\AnimeClient\API\Kitsu\Transformer; - -use Aviat\Ion\Transformer\AbstractTransformer; - -/** - * Transformer for anime description page - */ -class MangaTransformer extends AbstractTransformer { - - /** - * Convert raw api response to a more - * logical and workable structure - * - * @param array $item API library item - * @return array - */ - public function transform($item) - { - return [ - - ]; - } + + * @copyright 2015 - 2016 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\API\Kitsu\Transformer; + +use Aviat\Ion\Transformer\AbstractTransformer; + +/** + * Transformer for anime description page + */ +class MangaTransformer extends AbstractTransformer { + + /** + * Convert raw api response to a more + * logical and workable structure + * + * @param array $item API library item + * @return array + */ + public function transform($item) + { + return [ + 'title' => $item['canonicalTitle'], + 'en_title' => $item['titles']['en'], + 'jp_title' => $item['titles']['en_jp'], + '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' => $item['genres'], + ]; + } + + private function count(int $value = null) + { + return ((int)$value === 0) + ? '-' + : $value; + } } \ No newline at end of file diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index e88152c7..0626b85c 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -264,8 +264,8 @@ class Manga extends Controller { $data = $this->model->get_manga($manga_id); $this->outputHTML('manga/details', [ - 'title' => 'Manga · ' . $data['manga']['romaji_title'], - 'data' => $data['manga'], + 'title' => 'Manga · ' . $data['title'], + 'data' => $data, ]); } } diff --git a/src/Model/Manga.php b/src/Model/Manga.php index 6b9f7f60..0b7f7af4 100644 --- a/src/Model/Manga.php +++ b/src/Model/Manga.php @@ -6,12 +6,12 @@ * * PHP version 7 * - * @package AnimeListClient - * @author Timothy J. Warren + * @package AnimeListClient + * @author Timothy J. Warren * @copyright 2015 - 2016 Timothy J. Warren - * @license http://www.opensource.org/licenses/mit-license.html MIT License - * @version 4.0 - * @link https://github.com/timw4mail/HummingBirdAnimeClient + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://github.com/timw4mail/HummingBirdAnimeClient */ namespace Aviat\AnimeClient\Model; @@ -46,12 +46,20 @@ class Manga extends API { MangaReadingStatus::COMPLETED => self::COMPLETED ]; - public function __construct(ContainerInterface $container) - { - parent::__construct($container); + protected $status_map = [ + 'current' => self::READING, + 'planned' => self::PLAN_TO_READ, + 'completed' => self::COMPLETED, + 'on_hold' => self::ON_HOLD, + 'dropped' => self::DROPPED + ]; - $this->kitsuModel = $container->get('kitsu-model'); - } + public function __construct(ContainerInterface $container) + { + parent::__construct($container); + + $this->kitsuModel = $container->get('kitsu-model'); + } /** * Make an authenticated manga API call @@ -102,10 +110,9 @@ class Manga extends API { */ public function get_list($status) { - $data = $this->kitsuModel->getMangaList($status); - return $this->map_by_status($data)[$status]; - /*$data = $this->cache->get($this, '_get_list_from_api'); - return ($status !== 'All') ? $data[$status] : $data;*/ + $APIstatus = array_flip($this->const_map)[$status]; + $data = $this->kitsuModel->getMangaList($APIstatus); + return $this->map_by_status($data)[$status]; } @@ -117,8 +124,7 @@ class Manga extends API { */ public function get_manga($manga_id) { - $raw = $this->_manga_api_call('get', "manga/{$manga_id}.json"); - return Json::decode($raw['body'], TRUE); + return $this->kitsuModel->getManga($manga_id); } /** @@ -146,7 +152,7 @@ class Manga extends API { $entry['manga']['slug'], 'manga' );*/ - $key = $this->const_map[$entry['reading_status']]; + $key = $this->status_map[$entry['reading_status']]; $output[$key][] = $entry; }