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
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
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
|
|
|
|
* @version 4.0
|
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\{
|
|
|
|
Anime,
|
|
|
|
AnimeFormItem,
|
|
|
|
AnimeFormItemData,
|
|
|
|
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
|
|
|
|
2017-01-13 16:53:56 -05:00
|
|
|
$genres = array_column($anime['relationships']['genres'], 'name') ?? [];
|
|
|
|
sort($genres);
|
2017-03-29 16:09:22 -04:00
|
|
|
|
2017-04-07 13:57:14 -04:00
|
|
|
$rating = (int) $item['attributes']['rating'] !== 0
|
2018-01-18 16:21:45 -05:00
|
|
|
? 2 * $item['attributes']['rating']
|
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
|
|
|
|
2017-01-26 13:03:38 -05:00
|
|
|
$streamingLinks = (array_key_exists('streamingLinks', $anime['relationships']))
|
|
|
|
? 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-08-08 10:12:45 -04:00
|
|
|
'anime' => new 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-08-08 10:12:45 -04:00
|
|
|
'show_type' => $this->string($anime['showType'])->upperCaseFirst()->__toString(),
|
|
|
|
'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-08-08 10:12:45 -04: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-08-08 10:12:45 -04:00
|
|
|
* @return AnimeFormItem API library item
|
2016-12-20 12:55:43 -05:00
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
public function untransform($item): AnimeFormItem
|
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-08-08 10:12:45 -04:00
|
|
|
$untransformed = new AnimeFormItem([
|
2016-12-20 12:55:43 -05:00
|
|
|
'id' => $item['id'],
|
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
|
|
|
{
|
2017-02-08 00:44:57 -05:00
|
|
|
$untransformed['data']['rating'] = $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
|