2020-08-24 13:07:47 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2020-08-24 13:07:47 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2020-08-24 13:07:47 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2020-08-24 13:07:47 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
2020-08-26 15:22:14 -04:00
|
|
|
use Aviat\AnimeClient\Kitsu;
|
2020-08-24 13:07:47 -04:00
|
|
|
use Aviat\AnimeClient\Types\{FormItem, AnimeListItem, MangaListItem, MangaListItemDetail};
|
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
use Aviat\Ion\Type\StringType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime list
|
|
|
|
*/
|
|
|
|
final class LibraryEntryTransformer extends AbstractTransformer
|
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): AnimeListItem|MangaListItem
|
2020-08-24 13:07:47 -04:00
|
|
|
{
|
2021-02-12 11:14:45 -05:00
|
|
|
$item = (array)$item;
|
2020-08-24 13:07:47 -04:00
|
|
|
$type = $item['media']['type'] ?? '';
|
|
|
|
|
|
|
|
$genres = [];
|
|
|
|
if ($type !== '')
|
|
|
|
{
|
|
|
|
$genres = array_column($item['media']['categories']['nodes'], 'title');
|
|
|
|
sort($genres);
|
|
|
|
}
|
|
|
|
|
2021-02-12 13:09:57 -05:00
|
|
|
return match (strtolower($type))
|
2020-08-24 13:07:47 -04:00
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
'anime' => $this->animeTransform($item, $genres),
|
|
|
|
'manga' => $this->mangaTransform($item, $genres),
|
|
|
|
default => AnimeListItem::from([]),
|
|
|
|
};
|
2020-08-24 13:07:47 -04:00
|
|
|
}
|
|
|
|
|
2021-02-12 11:14:45 -05:00
|
|
|
private function animeTransform(array $item, array $genres): AnimeListItem
|
2020-08-24 13:07:47 -04:00
|
|
|
{
|
|
|
|
$animeId = $item['media']['id'];
|
|
|
|
$anime = $item['media'];
|
|
|
|
|
|
|
|
$rating = (int) $item['rating'] !== 0
|
|
|
|
? $item['rating'] / 2
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$total_episodes = array_key_exists('episodeCount', $anime) && (int) $anime['episodeCount'] !== 0
|
|
|
|
? (int) $anime['episodeCount']
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$MALid = NULL;
|
|
|
|
|
|
|
|
if (isset($anime['mappings']['nodes']))
|
|
|
|
{
|
|
|
|
foreach ($anime['mappings']['nodes'] as $mapping)
|
|
|
|
{
|
|
|
|
if ($mapping['externalSite'] === 'MYANIMELIST_ANIME')
|
|
|
|
{
|
|
|
|
$MALid = $mapping['externalId'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$streamingLinks = array_key_exists('nodes', $anime['streamingLinks'])
|
|
|
|
? Kitsu::parseStreamingLinks($anime['streamingLinks']['nodes'])
|
|
|
|
: [];
|
|
|
|
|
|
|
|
$titles = Kitsu::getFilteredTitles($anime['titles']);
|
|
|
|
$title = $anime['titles']['canonical'];
|
|
|
|
|
|
|
|
return AnimeListItem::from([
|
|
|
|
'id' => $item['id'],
|
|
|
|
'mal_id' => $MALid,
|
|
|
|
'episodes' => [
|
|
|
|
'watched' => (int) $item['progress'] !== 0
|
|
|
|
? (int) $item['progress']
|
|
|
|
: '-',
|
|
|
|
'total' => $total_episodes,
|
|
|
|
'length' => $anime['episodeLength'],
|
|
|
|
],
|
|
|
|
'airing' => [
|
|
|
|
'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']),
|
|
|
|
'started' => $anime['startDate'],
|
|
|
|
'ended' => $anime['endDate']
|
|
|
|
],
|
|
|
|
'anime' => [
|
|
|
|
'id' => $animeId,
|
|
|
|
'age_rating' => $anime['ageRating'],
|
|
|
|
'title' => $title,
|
|
|
|
'titles' => $titles,
|
|
|
|
'slug' => $anime['slug'],
|
|
|
|
'show_type' => (string)StringType::from($anime['subtype'])->upperCaseFirst(),
|
2022-01-12 18:00:00 -05:00
|
|
|
'cover_image' => $anime['posterImage']['views'][1]['url']
|
|
|
|
?? $anime['posterImage']['original']['url']
|
|
|
|
?? '/public/images/placeholder.png',
|
2020-08-24 13:07:47 -04:00
|
|
|
'genres' => $genres,
|
|
|
|
'streaming_links' => $streamingLinks,
|
|
|
|
],
|
|
|
|
'watching_status' => $item['status'],
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
'rewatching' => (bool) $item['reconsuming'],
|
|
|
|
'rewatched' => (int) $item['reconsumeCount'],
|
|
|
|
'user_rating' => $rating,
|
|
|
|
'private' => $item['private'] ?? FALSE,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:14:45 -05:00
|
|
|
private function mangaTransform(array $item, array $genres): MangaListItem
|
2020-08-24 13:07:47 -04:00
|
|
|
{
|
|
|
|
$mangaId = $item['media']['id'];
|
|
|
|
$manga = $item['media'];
|
|
|
|
|
|
|
|
$rating = (int) $item['rating'] !== 0
|
|
|
|
? $item['rating'] / 2
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$totalChapters = ((int) $manga['chapterCount'] !== 0)
|
|
|
|
? $manga['chapterCount']
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$totalVolumes = ((int) $manga['volumeCount'] !== 0)
|
|
|
|
? $manga['volumeCount']
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$readChapters = ((int) $item['progress'] !== 0)
|
|
|
|
? $item['progress']
|
|
|
|
: '-';
|
|
|
|
|
|
|
|
$MALid = NULL;
|
|
|
|
|
|
|
|
if (isset($manga['mappings']['nodes']))
|
|
|
|
{
|
|
|
|
foreach ($manga['mappings']['nodes'] as $mapping)
|
|
|
|
{
|
|
|
|
if ($mapping['externalSite'] === 'MYANIMELIST_MANGA')
|
|
|
|
{
|
|
|
|
$MALid = $mapping['externalId'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$titles = Kitsu::getFilteredTitles($manga['titles']);
|
|
|
|
$title = $manga['titles']['canonical'];
|
|
|
|
|
|
|
|
return MangaListItem::from([
|
|
|
|
'id' => $item['id'],
|
|
|
|
'mal_id' => $MALid,
|
|
|
|
'chapters' => [
|
|
|
|
'read' => $readChapters,
|
|
|
|
'total' => $totalChapters
|
|
|
|
],
|
|
|
|
'volumes' => [
|
|
|
|
'read' => '-', //$item['attributes']['volumes_read'],
|
|
|
|
'total' => $totalVolumes
|
|
|
|
],
|
|
|
|
'manga' => MangaListItemDetail::from([
|
|
|
|
'genres' => $genres,
|
|
|
|
'id' => $mangaId,
|
|
|
|
'image' => $manga['posterImage']['views'][1]['url'],
|
|
|
|
'slug' => $manga['slug'],
|
|
|
|
'title' => $title,
|
|
|
|
'titles' => $titles,
|
|
|
|
'type' => (string)StringType::from($manga['subtype'])->upperCaseFirst(),
|
|
|
|
'url' => 'https://kitsu.io/manga/' . $manga['slug'],
|
|
|
|
]),
|
|
|
|
'reading_status' => strtolower($item['status']),
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
'rereading' => (bool)$item['reconsuming'],
|
|
|
|
'reread' => $item['reconsumeCount'],
|
|
|
|
'user_rating' => $rating,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|