HummingBirdAnimeClient/src/AnimeClient/API/Kitsu/Transformer/MangaListTransformer.php

148 lines
3.7 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
*
* PHP version 7.4
2016-12-20 12:55:43 -05:00
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2017-01-06 23:34:56 -05:00
* @author Timothy J. Warren <tim@timshomepage.net>
2020-01-08 15:39:49 -05:00
* @copyright 2015 - 2020 Timothy J. Warren
2017-01-06 23:34:56 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5
* @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;
2018-08-08 13:05:38 -04:00
use Aviat\AnimeClient\Types\{
2018-09-27 16:45:12 -04:00
FormItem, FormItemData,
2018-08-08 13:05:38 -04:00
MangaListItem, MangaListItemDetail
};
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
/**
* Data transformation class for zippered Hummingbird manga
*/
final class MangaListTransformer extends AbstractTransformer {
2016-12-20 12:55:43 -05:00
/**
* Remap zipped anime data to a more logical form
*
* @param array $item manga entry item
2018-08-08 13:05:38 -04:00
* @return MangaListItem
2016-12-20 12:55:43 -05:00
*/
2018-08-08 13:05:38 -04:00
public function transform($item): MangaListItem
2016-12-20 12:55:43 -05:00
{
$included = $item['included'];
$mangaId = $item['relationships']['media']['data']['id'];
$manga = $included['manga'][$mangaId];
$genres = [];
foreach ($manga['relationships']['categories'] as $genre)
{
$genres[] = $genre['title'];
}
sort($genres);
2016-12-20 12:55:43 -05:00
2018-09-26 22:31:04 -04:00
$rating = (int) $item['attributes']['ratingTwenty'] !== 0
? $item['attributes']['ratingTwenty'] / 2
2016-12-20 12:55:43 -05:00
: '-';
2017-04-07 13:57:14 -04:00
$totalChapters = ((int) $manga['chapterCount'] !== 0)
? $manga['chapterCount']
2017-01-04 13:16:58 -05:00
: '-';
2016-12-20 12:55:43 -05:00
2017-04-07 13:57:14 -04:00
$totalVolumes = ((int) $manga['volumeCount'] !== 0)
? $manga['volumeCount']
2016-12-20 12:55:43 -05:00
: '-';
2017-04-07 13:57:14 -04:00
$readChapters = ((int) $item['attributes']['progress'] !== 0)
? $item['attributes']['progress']
: '-';
$MALid = NULL;
if (array_key_exists('mappings', $manga['relationships']))
{
foreach ($manga['relationships']['mappings'] as $mapping)
{
if ($mapping['externalSite'] === 'myanimelist/manga')
{
$MALid = $mapping['externalId'];
break;
}
}
}
2018-08-08 13:05:38 -04:00
$titles = Kitsu::filterTitles($manga);
$title = array_shift($titles);
2019-12-09 14:34:23 -05:00
return new MangaListItem([
2016-12-20 12:55:43 -05:00
'id' => $item['id'],
'mal_id' => $MALid,
2016-12-20 12:55:43 -05:00
'chapters' => [
2017-04-07 13:57:14 -04:00
'read' => $readChapters,
2017-02-15 13:07:36 -05:00
'total' => $totalChapters
2016-12-20 12:55:43 -05:00
],
'volumes' => [
2017-01-04 13:16:58 -05:00
'read' => '-', //$item['attributes']['volumes_read'],
2017-02-15 13:07:36 -05:00
'total' => $totalVolumes
2016-12-20 12:55:43 -05:00
],
2018-08-08 13:05:38 -04:00
'manga' => new MangaListItemDetail([
'genres' => $genres,
'id' => $mangaId,
2018-08-08 13:05:38 -04:00
'image' => $manga['posterImage']['small'],
'slug' => $manga['slug'],
2018-08-08 13:05:38 -04:00
'title' => $title,
'titles' => $titles,
2020-04-10 16:35:01 -04:00
'type' => (string)StringType::from($manga['subtype'])->upperCaseFirst(),
2018-08-08 13:05:38 -04:00
'url' => 'https://kitsu.io/manga/' . $manga['slug'],
]),
2017-01-04 13:16:58 -05:00
'reading_status' => $item['attributes']['status'],
'notes' => $item['attributes']['notes'],
'rereading' => (bool)$item['attributes']['reconsuming'],
'reread' => $item['attributes']['reconsumeCount'],
2016-12-20 12:55:43 -05:00
'user_rating' => $rating,
2018-08-08 13:05:38 -04:00
]);
2016-12-20 12:55:43 -05:00
}
/**
* Untransform data to update the api
*
* @param array $item
2018-09-27 16:45:12 -04:00
* @return FormItem
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
{
$rereading = array_key_exists('rereading', $item) && (bool)$item['rereading'];
2016-12-20 12:55:43 -05:00
2018-09-27 16:45:12 -04:00
$map = new FormItem([
2016-12-20 12:55:43 -05:00
'id' => $item['id'],
'mal_id' => $item['mal_id'],
2018-09-27 16:45:12 -04:00
'data' => new FormItemData([
'status' => $item['status'],
'reconsuming' => $rereading,
'reconsumeCount' => (int)$item['reread_count'],
'notes' => $item['notes'],
2018-08-08 13:05:38 -04:00
]),
]);
2016-12-20 12:55:43 -05:00
2017-04-10 15:31:35 -04:00
if (is_numeric($item['chapters_read']) && $item['chapters_read'] > 0)
{
$map['data']['progress'] = (int)$item['chapters_read'];
}
if (is_numeric($item['new_rating']) && $item['new_rating'] > 0)
{
2018-09-26 22:31:04 -04:00
$map['data']['ratingTwenty'] = $item['new_rating'] * 2;
}
2016-12-20 12:55:43 -05:00
return $map;
}
}
// End of MangaListTransformer.php