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

150 lines
4.1 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
*
* 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
* @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\{
Anime,
AnimeFormItem,
AnimeFormItemData,
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 = array_column($anime['relationships']['genres'], 'name') ?? [];
sort($genres);
2017-04-07 13:57:14 -04:00
$rating = (int) $item['attributes']['rating'] !== 0
? 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
? (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' => new Anime([
'id' => $animeId,
'age_rating' => $anime['ageRating'],
'title' => $title,
'titles' => $titles,
'slug' => $anime['slug'],
'show_type' => $this->string($anime['showType'])->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
* @return AnimeFormItem API library item
2016-12-20 12:55:43 -05: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
$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,
'data' => new AnimeFormItemData([
'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)
{
$untransformed['data']['rating'] = $item['user_rating'] / 2;
}
return $untransformed;
2016-12-20 12:55:43 -05:00
}
}
// End of AnimeListTransformer.php