From 1f86d4460cf4ba37b89e87d0668e50918316107e Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 1 Mar 2017 20:51:40 -0500 Subject: [PATCH] Add new mapping class for Anime watching statuses --- src/API/Mapping/AnimeWatchingStatus.php | 67 +++++++++++++++++++++++++ src/Controller/Anime.php | 61 +++++----------------- 2 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 src/API/Mapping/AnimeWatchingStatus.php diff --git a/src/API/Mapping/AnimeWatchingStatus.php b/src/API/Mapping/AnimeWatchingStatus.php new file mode 100644 index 00000000..edc53236 --- /dev/null +++ b/src/API/Mapping/AnimeWatchingStatus.php @@ -0,0 +1,67 @@ + + * @copyright 2015 - 2017 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\Mapping; + +use Aviat\AnimeClient\API\{ + Kitsu\Enum\AnimeWatchingStatus as Kitsu, + MAL\Enum\AnimeWatchingStatus as MAL +}; +use Aviat\Ion\Enum; + +class AnimeWatchingStatus extends Enum { + const MAL_TO_KITSU = [ + Kitsu::WATCHING => MAL::WATCHING, + Kitsu::PLAN_TO_WATCH => MAL::PLAN_TO_WATCH, + Kitsu::COMPLETED => MAL::COMPLETED, + Kitsu::ON_HOLD => MAL::ON_HOLD, + Kitsu::DROPPED => MAL::DROPPED + ]; + + const KITSU_TO_MAL = [ + MAL::WATCHING => Kitsu::WATCHING, + MAL::PLAN_TO_WATCH => Kitsu::PLAN_TO_WATCH, + MAL::COMPLETED => Kitsu::COMPLETED, + MAL::ON_HOLD => Kitsu::ON_HOLD, + MAL::DROPPED => Kitsu::DROPPED + ]; + + const ROUTE_TO_KITSU = [ + 'watching' => Kitsu::WATCHING, + 'plan_to_watch' => Kitsu::PLAN_TO_WATCH, + 'on_hold' => Kitsu::ON_HOLD, + 'all' => 'all', + 'dropped' => Kitsu::DROPPED, + 'completed' => Kitsu::COMPLETED + ]; + + const ROUTE_TO_TITLE = [ + 'all' => 'All', + 'watching' => 'Currently Watching', + 'plan_to_watch' => 'Plan to Watch', + 'on_hold' => 'On Hold', + 'dropped' => 'Dropped', + 'completed' => 'Completed' + ]; + + const KITSU_TO_TITLE = [ + Kitsu::WATCHING => 'Currently Watching', + Kitsu::PLAN_TO_WATCH => 'Plan to Watch', + Kitsu::ON_HOLD => 'On Hold', + Kitsu::DROPPED => 'Dropped', + Kitsu::COMPLETED => 'Completed' + ]; +} \ No newline at end of file diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index c13cdb7d..fae30a6c 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -18,8 +18,11 @@ namespace Aviat\AnimeClient\Controller; use Aviat\AnimeClient\Controller as BaseController; use Aviat\AnimeClient\API\Kitsu; -use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus; -use Aviat\AnimeClient\API\Kitsu\Transformer\AnimeListTransformer; +use Aviat\AnimeClient\API\Kitsu\{ + Enum\AnimeWatchingStatus as KitsuWatchingStatus, + Transformer\AnimeListTransformer +}; +use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus; use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Json; use Aviat\Ion\StringWrapper; @@ -77,29 +80,11 @@ class Anime extends BaseController { * @param string $view - List or cover view * @return void */ - public function index($type = AnimeWatchingStatus::WATCHING, string $view = NULL) + public function index($type = KitsuWatchingStatus::WATCHING, string $view = NULL) { - $typeTitleMap = [ - 'all' => 'All', - 'watching' => 'Currently Watching', - 'plan_to_watch' => 'Plan to Watch', - 'on_hold' => 'On Hold', - 'dropped' => 'Dropped', - 'completed' => 'Completed' - ]; - - $modelMap = [ - 'watching' => AnimeWatchingStatus::WATCHING, - 'plan_to_watch' => AnimeWatchingStatus::PLAN_TO_WATCH, - 'on_hold' => AnimeWatchingStatus::ON_HOLD, - 'all' => 'all', - 'dropped' => AnimeWatchingStatus::DROPPED, - 'completed' => AnimeWatchingStatus::COMPLETED - ]; - - $title = (array_key_exists($type, $typeTitleMap)) + $title = (array_key_exists($type, AnimeWatchingStatus::ROUTE_TO_TITLE)) ? $this->config->get('whose_list') . - "'s Anime List · {$typeTitleMap[$type]}" + "'s Anime List · " . AnimeWatchingStatus::ROUTE_TO_TITLE[$type] : ''; $viewMap = [ @@ -108,7 +93,7 @@ class Anime extends BaseController { ]; $data = ($type !== 'all') - ? $this->model->getList($modelMap[$type]) + ? $this->model->getList(AnimeWatchingStatus::ROUTE_TO_KITSU[$type]) : $this->model->get_all_lists(); $this->outputHTML('anime/' . $viewMap[$view], [ @@ -124,20 +109,12 @@ class Anime extends BaseController { */ public function addForm() { - $statuses = [ - AnimeWatchingStatus::WATCHING => 'Currently Watching', - AnimeWatchingStatus::PLAN_TO_WATCH => 'Plan to Watch', - AnimeWatchingStatus::ON_HOLD => 'On Hold', - AnimeWatchingStatus::DROPPED => 'Dropped', - AnimeWatchingStatus::COMPLETED => 'Completed' - ]; - $this->setSessionRedirect(); $this->outputHTML('anime/add', [ 'title' => $this->config->get('whose_list') . "'s Anime List · Add", 'action_url' => $this->urlGenerator->url('anime/add'), - 'status_list' => $statuses + 'status_list' => AnimeWatchingStatus::KITSU_TO_TITLE ]); } @@ -179,25 +156,13 @@ class Anime extends BaseController { public function edit($id, $status = "all") { $item = $this->model->getLibraryItem($id, $status); - $rawStatusList = AnimeWatchingStatus::getConstList(); - - $statuses = []; - - foreach ($rawStatusList as $statusItem) - { - $statuses[$statusItem] = (string) $this->string($statusItem) - ->underscored() - ->humanize() - ->titleize(); - } - $this->setSessionRedirect(); $this->outputHTML('anime/edit', [ 'title' => $this->config->get('whose_list') . "'s Anime List · Edit", 'item' => $item, - 'statuses' => Kitsu::getStatusToSelectMap(), + 'statuses' => AnimeWatchingStatus::KITSU_TO_TITLE, 'action' => $this->container->get('url-generator') ->url('/anime/update_form'), ]); @@ -252,7 +217,7 @@ class Anime extends BaseController { { if ($this->request->getHeader('content-type')[0] === 'application/json') { - $data = JSON::decode((string)$this->request->getBody()); + $data = Json::decode((string)$this->request->getBody()); } else { @@ -294,7 +259,7 @@ class Anime extends BaseController { * @param string $animeId * @return void */ - public function details($animeId) + public function details(string $animeId) { $data = $this->model->getAnime($animeId);