2016-12-20 12:55:43 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2016-12-20 12:55:43 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2016-12-20 12:55:43 -05:00
|
|
|
*
|
2018-10-01 11:35:51 -04:00
|
|
|
* PHP version 7.1
|
2016-12-20 12:55:43 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2016-12-20 12:55:43 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2018-01-15 14:43:15 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2016-12-20 12:55:43 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2018-10-01 11:35:51 -04:00
|
|
|
* @version 4.1
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2017-01-05 22:24:45 -05:00
|
|
|
use Aviat\AnimeClient\API\Kitsu;
|
2018-08-08 10:12:45 -04:00
|
|
|
use Aviat\AnimeClient\Types\{
|
2018-09-27 16:45:12 -04:00
|
|
|
FormItem,
|
2018-08-08 10:12:45 -04:00
|
|
|
AnimeListItem
|
|
|
|
};
|
2016-12-20 12:55:43 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime list
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class AnimeListTransformer extends AbstractTransformer {
|
2016-12-20 12:55:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw api response to a more
|
|
|
|
* logical and workable structure
|
|
|
|
*
|
|
|
|
* @param array $item API library item
|
2018-08-08 10:12:45 -04:00
|
|
|
* @return AnimeListItem
|
2016-12-20 12:55:43 -05:00
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
public function transform($item): AnimeListItem
|
2016-12-20 12:55:43 -05:00
|
|
|
{
|
2017-01-13 16:53:56 -05:00
|
|
|
$included = $item['included'];
|
2017-01-12 15:41:20 -05:00
|
|
|
$animeId = $item['relationships']['media']['data']['id'];
|
2017-01-13 16:53:56 -05:00
|
|
|
$anime = $included['anime'][$animeId];
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2018-10-16 14:22:47 -04:00
|
|
|
$genres = [];
|
|
|
|
|
|
|
|
foreach($anime['relationships']['categories'] as $genre)
|
|
|
|
{
|
|
|
|
$genres[] = $genre['title'];
|
|
|
|
}
|
|
|
|
|
2017-01-13 16:53:56 -05:00
|
|
|
sort($genres);
|
2017-03-29 16:09:22 -04:00
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
$rating = (int) $item['attributes']['ratingTwenty'] !== 0
|
|
|
|
? $item['attributes']['ratingTwenty'] / 2
|
2017-04-07 13:57:14 -04:00
|
|
|
: '-';
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2017-01-10 21:13:44 -05:00
|
|
|
$total_episodes = array_key_exists('episodeCount', $anime) && (int) $anime['episodeCount'] !== 0
|
2017-01-05 22:24:45 -05:00
|
|
|
? (int) $anime['episodeCount']
|
2016-12-20 12:55:43 -05:00
|
|
|
: '-';
|
2017-03-29 16:09:22 -04:00
|
|
|
|
2017-01-12 15:41:20 -05:00
|
|
|
$MALid = NULL;
|
2017-01-13 16:53:56 -05:00
|
|
|
|
|
|
|
if (array_key_exists('mappings', $anime['relationships']))
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
2017-01-13 16:53:56 -05:00
|
|
|
foreach ($anime['relationships']['mappings'] as $mapping)
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
|
|
|
if ($mapping['externalSite'] === 'myanimelist/anime')
|
|
|
|
{
|
|
|
|
$MALid = $mapping['externalId'];
|
2017-01-13 16:53:56 -05:00
|
|
|
break;
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-29 16:09:22 -04:00
|
|
|
|
2018-12-21 15:52:34 -05:00
|
|
|
$streamingLinks = array_key_exists('streamingLinks', $anime['relationships'])
|
2017-01-26 13:03:38 -05:00
|
|
|
? Kitsu::parseListItemStreamingLinks($included, $animeId)
|
|
|
|
: [];
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2018-08-08 10:12:45 -04:00
|
|
|
$titles = Kitsu::filterTitles($anime);
|
|
|
|
$title = array_shift($titles);
|
|
|
|
|
|
|
|
return new AnimeListItem([
|
2016-12-20 12:55:43 -05:00
|
|
|
'id' => $item['id'],
|
2017-01-12 15:41:20 -05:00
|
|
|
'mal_id' => $MALid,
|
2016-12-20 12:55:43 -05:00
|
|
|
'episodes' => [
|
2017-04-07 13:57:14 -04:00
|
|
|
'watched' => (int) $item['attributes']['progress'] !== 0
|
2017-01-13 16:53:56 -05:00
|
|
|
? (int) $item['attributes']['progress']
|
|
|
|
: '-',
|
2016-12-20 12:55:43 -05:00
|
|
|
'total' => $total_episodes,
|
2017-01-05 22:24:45 -05:00
|
|
|
'length' => $anime['episodeLength'],
|
2016-12-20 12:55:43 -05:00
|
|
|
],
|
|
|
|
'airing' => [
|
2017-01-06 21:39:01 -05:00
|
|
|
'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']),
|
2017-01-05 22:24:45 -05:00
|
|
|
'started' => $anime['startDate'],
|
|
|
|
'ended' => $anime['endDate']
|
2016-12-20 12:55:43 -05:00
|
|
|
],
|
2018-11-08 11:36:42 -05:00
|
|
|
'anime' => [
|
2017-04-13 11:26:28 -04:00
|
|
|
'id' => $animeId,
|
2017-01-05 22:24:45 -05:00
|
|
|
'age_rating' => $anime['ageRating'],
|
2018-08-08 10:12:45 -04:00
|
|
|
'title' => $title,
|
|
|
|
'titles' => $titles,
|
2017-01-05 22:24:45 -05:00
|
|
|
'slug' => $anime['slug'],
|
2018-09-27 16:45:12 -04:00
|
|
|
'show_type' => $this->string($anime['subtype'])->upperCaseFirst()->__toString(),
|
2018-08-08 10:12:45 -04:00
|
|
|
'cover_image' => $anime['posterImage']['small'],
|
2016-12-22 21:36:23 -05:00
|
|
|
'genres' => $genres,
|
2017-01-25 13:37:39 -05:00
|
|
|
'streaming_links' => $streamingLinks,
|
2018-11-08 11:36:42 -05:00
|
|
|
],
|
2016-12-21 12:46:20 -05:00
|
|
|
'watching_status' => $item['attributes']['status'],
|
|
|
|
'notes' => $item['attributes']['notes'],
|
|
|
|
'rewatching' => (bool) $item['attributes']['reconsuming'],
|
|
|
|
'rewatched' => (int) $item['attributes']['reconsumeCount'],
|
2017-04-07 13:57:14 -04:00
|
|
|
'user_rating' => $rating,
|
2018-01-18 16:21:45 -05:00
|
|
|
'private' => $item['attributes']['private'] ?? FALSE,
|
2018-08-08 10:12:45 -04:00
|
|
|
]);
|
2016-12-20 12:55:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert transformed data to
|
|
|
|
* api response format
|
|
|
|
*
|
|
|
|
* @param array $item Transformed library item
|
2018-09-27 16:45:12 -04:00
|
|
|
* @return FormItem API library item
|
2016-12-20 12:55:43 -05:00
|
|
|
*/
|
2018-09-27 16:45:12 -04:00
|
|
|
public function untransform($item): FormItem
|
2016-12-20 12:55:43 -05:00
|
|
|
{
|
2017-01-12 15:41:20 -05:00
|
|
|
$privacy = (array_key_exists('private', $item) && $item['private']);
|
|
|
|
$rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']);
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2018-09-27 16:45:12 -04:00
|
|
|
$untransformed = new FormItem([
|
2016-12-20 12:55:43 -05:00
|
|
|
'id' => $item['id'],
|
2018-09-20 10:41:28 -04:00
|
|
|
'anilist_item_id' => $item['anilist_item_id'] ?? NULL,
|
2017-02-17 11:37:22 -05:00
|
|
|
'mal_id' => $item['mal_id'] ?? NULL,
|
2018-08-09 11:34:02 -04:00
|
|
|
'data' => [
|
2017-01-06 21:39:01 -05:00
|
|
|
'status' => $item['watching_status'],
|
|
|
|
'reconsuming' => $rewatching,
|
|
|
|
'reconsumeCount' => $item['rewatched'],
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
'private' => $privacy
|
2018-08-09 11:34:02 -04:00
|
|
|
]
|
2018-08-08 10:12:45 -04:00
|
|
|
]);
|
2017-03-29 16:09:22 -04:00
|
|
|
|
2017-04-10 15:31:35 -04:00
|
|
|
if (is_numeric($item['episodes_watched']) && $item['episodes_watched'] > 0)
|
|
|
|
{
|
|
|
|
$untransformed['data']['progress'] = (int) $item['episodes_watched'];
|
|
|
|
}
|
|
|
|
|
2017-04-06 14:53:38 -04:00
|
|
|
if (is_numeric($item['user_rating']) && $item['user_rating'] > 0)
|
2017-01-06 21:39:01 -05:00
|
|
|
{
|
2018-09-26 22:31:04 -04:00
|
|
|
$untransformed['data']['ratingTwenty'] = $item['user_rating'] * 2;
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $untransformed;
|
2016-12-20 12:55:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of AnimeListTransformer.php
|