2016-12-20 12:55:43 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package AnimeListClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-01-11 10:30:53 -05:00
|
|
|
* @copyright 2015 - 2017 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://github.com/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;
|
2016-12-20 12:55:43 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime list
|
|
|
|
*/
|
|
|
|
class AnimeListTransformer extends AbstractTransformer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw api response to a more
|
|
|
|
* logical and workable structure
|
|
|
|
*
|
|
|
|
* @param array $item API library item
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform($item)
|
|
|
|
{
|
2017-01-13 16:53:56 -05:00
|
|
|
/* ?><pre><?= json_encode($item, \JSON_PRETTY_PRINT) ?></pre><?php */
|
|
|
|
$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);
|
|
|
|
|
2016-12-21 12:46:20 -05:00
|
|
|
$rating = (int) 2 * $item['attributes']['rating'];
|
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-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-01-25 13:37:39 -05: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
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $item['id'],
|
2017-01-12 15:41:20 -05:00
|
|
|
'mal_id' => $MALid,
|
2016-12-20 12:55:43 -05:00
|
|
|
'episodes' => [
|
2017-01-13 16:53:56 -05:00
|
|
|
'watched' => (int) $item['attributes']['progress'] !== '0'
|
|
|
|
? (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
|
|
|
],
|
|
|
|
'anime' => [
|
2017-01-05 22:24:45 -05:00
|
|
|
'age_rating' => $anime['ageRating'],
|
2017-01-25 13:37:39 -05:00
|
|
|
'title' => $anime['canonicalTitle'],
|
2017-01-05 22:24:45 -05:00
|
|
|
'titles' => Kitsu::filterTitles($anime),
|
|
|
|
'slug' => $anime['slug'],
|
|
|
|
'type' => $this->string($anime['showType'])->upperCaseFirst()->__toString(),
|
|
|
|
'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,
|
2016-12-20 12:55:43 -05: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-01-12 15:41:20 -05:00
|
|
|
'user_rating' => ($rating === 0) ? '-' : (int) $rating,
|
2016-12-21 12:46:20 -05:00
|
|
|
'private' => (bool) $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 array API library item
|
|
|
|
*/
|
|
|
|
public function untransform($item)
|
|
|
|
{
|
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
|
|
|
|
2017-01-06 21:39:01 -05:00
|
|
|
$untransformed = [
|
2016-12-20 12:55:43 -05:00
|
|
|
'id' => $item['id'],
|
2017-02-04 15:18:34 -05:00
|
|
|
'mal_id' => $item['mal_id'] ?? null,
|
2017-01-06 21:39:01 -05:00
|
|
|
'data' => [
|
|
|
|
'status' => $item['watching_status'],
|
|
|
|
'rating' => $item['user_rating'] / 2,
|
|
|
|
'reconsuming' => $rewatching,
|
|
|
|
'reconsumeCount' => $item['rewatched'],
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
'progress' => $item['episodes_watched'],
|
|
|
|
'private' => $privacy
|
|
|
|
]
|
2016-12-20 12:55:43 -05:00
|
|
|
];
|
2017-01-06 21:39:01 -05:00
|
|
|
|
|
|
|
if ((int) $untransformed['data']['rating'] === 0)
|
|
|
|
{
|
|
|
|
unset($untransformed['data']['rating']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $untransformed;
|
2016-12-20 12:55:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of AnimeListTransformer.php
|