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
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
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>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2016-12-20 12:55:43 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
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
|
|
|
|
2020-08-26 15:22:14 -04:00
|
|
|
use Aviat\AnimeClient\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;
|
2020-04-10 16:35:01 -04:00
|
|
|
use Aviat\Ion\Type\StringType;
|
2016-12-20 12:55:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2021-02-12 13:09:57 -05:00
|
|
|
* @param array|object $item API library item
|
2018-08-08 10:12:45 -04:00
|
|
|
* @return AnimeListItem
|
2016-12-20 12:55:43 -05:00
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): AnimeListItem
|
2016-12-20 12:55:43 -05:00
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
$item = (array)$item;
|
2020-10-09 16:18:45 -04:00
|
|
|
$animeId = $item['media']['id'];
|
|
|
|
$anime = $item['media'];
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2018-10-16 14:22:47 -04:00
|
|
|
$genres = [];
|
|
|
|
|
2020-10-09 16:18:45 -04:00
|
|
|
$rating = (int) $item['rating'] !== 0
|
|
|
|
? (int)$item['rating'] / 2
|
2017-04-07 13:57:14 -04:00
|
|
|
: '-';
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2020-10-09 16:18:45 -04:00
|
|
|
$total_episodes = (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
|
|
|
|
2020-10-09 16:18:45 -04:00
|
|
|
$mappings = $anime['mappings']['nodes'] ?? [];
|
|
|
|
if ( ! empty($mappings))
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
2020-10-09 16:18:45 -04:00
|
|
|
foreach ($mappings as $mapping)
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
2020-10-09 16:18:45 -04:00
|
|
|
if ($mapping['externalSite'] === 'MYANIMELIST_ANIME')
|
2017-01-12 15:41:20 -05:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2020-10-09 16:18:45 -04:00
|
|
|
$streamingLinks = array_key_exists('nodes', $anime['streamingLinks'])
|
|
|
|
? Kitsu::parseStreamingLinks($anime['streamingLinks']['nodes'])
|
2017-01-26 13:03:38 -05:00
|
|
|
: [];
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2020-10-09 16:18:45 -04:00
|
|
|
$titles = Kitsu::getFilteredTitles($anime['titles']);
|
|
|
|
$title = $anime['titles']['canonical'];
|
2018-08-08 10:12:45 -04:00
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
return AnimeListItem::from([
|
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' => [
|
2020-10-09 16:18:45 -04:00
|
|
|
'watched' => (int) $item['progress'] !== 0
|
|
|
|
? (int) $item['progress']
|
2017-01-13 16:53:56 -05:00
|
|
|
: '-',
|
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'],
|
2020-04-10 16:35:01 -04:00
|
|
|
'show_type' => (string)StringType::from($anime['subtype'])->upperCaseFirst(),
|
2020-10-09 16:18:45 -04:00
|
|
|
'cover_image' => $anime['posterImage']['views'][1]['url'],
|
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
|
|
|
],
|
2020-10-09 16:18:45 -04:00
|
|
|
'watching_status' => $item['status'],
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
'rewatching' => (bool) $item['reconsuming'],
|
|
|
|
'rewatched' => (int) $item['reconsumeCount'],
|
2017-04-07 13:57:14 -04:00
|
|
|
'user_rating' => $rating,
|
2020-10-09 16:18:45 -04:00
|
|
|
'private' => $item['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
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function untransform(array $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
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
$untransformed = FormItem::from([
|
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
|
|
|
{
|
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
|