HummingBirdAnimeClient/src/API/Kitsu/Transformer/AnimeListTransformer.php

155 lines
4.2 KiB
PHP
Raw Normal View History

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
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
2016-12-20 12:55:43 -05:00
use Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\Types\{
2018-09-27 16:45:12 -04:00
FormItem,
AnimeListItem
};
2016-12-20 12:55:43 -05:00
use Aviat\Ion\Transformer\AbstractTransformer;
/**
* Transformer for anime list
*/
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
* @return AnimeListItem
2016-12-20 12:55:43 -05:00
*/
public function transform($item): AnimeListItem
2016-12-20 12:55:43 -05:00
{
$included = $item['included'];
2017-01-12 15:41:20 -05:00
$animeId = $item['relationships']['media']['data']['id'];
$anime = $included['anime'][$animeId];
2016-12-20 12:55:43 -05:00
$genres = [];
foreach($anime['relationships']['categories'] as $genre)
{
$genres[] = $genre['title'];
}
sort($genres);
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
? (int) $anime['episodeCount']
2016-12-20 12:55:43 -05:00
: '-';
2017-01-12 15:41:20 -05:00
$MALid = NULL;
if (array_key_exists('mappings', $anime['relationships']))
2017-01-12 15:41:20 -05:00
{
foreach ($anime['relationships']['mappings'] as $mapping)
2017-01-12 15:41:20 -05:00
{
if ($mapping['externalSite'] === 'myanimelist/anime')
{
$MALid = $mapping['externalId'];
break;
2017-01-12 15:41:20 -05:00
}
}
}
$streamingLinks = (array_key_exists('streamingLinks', $anime['relationships']))
? Kitsu::parseListItemStreamingLinks($included, $animeId)
: [];
2016-12-20 12:55:43 -05: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
? (int) $item['attributes']['progress']
: '-',
2016-12-20 12:55:43 -05:00
'total' => $total_episodes,
'length' => $anime['episodeLength'],
2016-12-20 12:55:43 -05:00
],
'airing' => [
'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']),
'started' => $anime['startDate'],
'ended' => $anime['endDate']
2016-12-20 12:55:43 -05:00
],
'anime' => [
'id' => $animeId,
'age_rating' => $anime['ageRating'],
'title' => $title,
'titles' => $titles,
'slug' => $anime['slug'],
2018-09-27 16:45:12 -04:00
'show_type' => $this->string($anime['subtype'])->upperCaseFirst()->__toString(),
'cover_image' => $anime['posterImage']['small'],
'genres' => $genres,
'streaming_links' => $streamingLinks,
],
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,
'private' => $item['attributes']['private'] ?? FALSE,
]);
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'],
'anilist_item_id' => $item['anilist_item_id'] ?? NULL,
2017-02-17 11:37:22 -05:00
'mal_id' => $item['mal_id'] ?? NULL,
'data' => [
'status' => $item['watching_status'],
'reconsuming' => $rewatching,
'reconsumeCount' => $item['rewatched'],
'notes' => $item['notes'],
'private' => $privacy
]
]);
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'];
}
if (is_numeric($item['user_rating']) && $item['user_rating'] > 0)
{
2018-09-26 22:31:04 -04:00
$untransformed['data']['ratingTwenty'] = $item['user_rating'] * 2;
}
return $untransformed;
2016-12-20 12:55:43 -05:00
}
}
// End of AnimeListTransformer.php