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
|
2017-01-06 23:34:56 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2017-01-11 10:30:53 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2017-01-06 23:34:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/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;
|
2017-01-03 21:06:49 -05:00
|
|
|
use Aviat\Ion\StringWrapper;
|
2016-12-20 12:55:43 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data transformation class for zippered Hummingbird manga
|
|
|
|
*/
|
|
|
|
class MangaListTransformer extends AbstractTransformer {
|
|
|
|
|
2017-01-03 21:06:49 -05:00
|
|
|
use StringWrapper;
|
2016-12-20 12:55:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remap zipped anime data to a more logical form
|
|
|
|
*
|
|
|
|
* @param array $item manga entry item
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform($item)
|
|
|
|
{
|
2017-03-29 13:29:03 -04:00
|
|
|
$included = $item['included'];
|
|
|
|
$mangaId = $item['relationships']['media']['data']['id'];
|
|
|
|
$manga = $included['manga'][$mangaId];
|
|
|
|
|
|
|
|
$genres = array_column($manga['relationships']['genres'], 'name') ?? [];
|
|
|
|
sort($genres);
|
2016-12-20 12:55:43 -05:00
|
|
|
|
2017-04-07 13:57:14 -04:00
|
|
|
$rating = (int) $item['attributes']['rating'] !== 0
|
|
|
|
? (int) 2 * $item['attributes']['rating']
|
2016-12-20 12:55:43 -05:00
|
|
|
: '-';
|
|
|
|
|
2017-04-07 13:57:14 -04:00
|
|
|
$totalChapters = ((int) $manga['chapterCount'] !== 0)
|
2017-03-29 13:29:03 -04:00
|
|
|
? $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)
|
2017-03-29 13:29:03 -04:00
|
|
|
? $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']
|
|
|
|
: '-';
|
|
|
|
|
2017-03-29 13:29:03 -04:00
|
|
|
$MALid = NULL;
|
|
|
|
|
|
|
|
if (array_key_exists('mappings', $manga['relationships']))
|
|
|
|
{
|
|
|
|
foreach ($manga['relationships']['mappings'] as $mapping)
|
|
|
|
{
|
|
|
|
if ($mapping['externalSite'] === 'myanimelist/manga')
|
|
|
|
{
|
|
|
|
$MALid = $mapping['externalId'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 12:55:43 -05:00
|
|
|
$map = [
|
|
|
|
'id' => $item['id'],
|
2017-03-29 13:29:03 -04:00
|
|
|
'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
|
|
|
],
|
|
|
|
'manga' => [
|
2017-04-13 11:44:03 -04:00
|
|
|
'id' => $mangaId,
|
2017-03-29 13:29:03 -04:00
|
|
|
'titles' => Kitsu::filterTitles($manga),
|
2016-12-20 12:55:43 -05:00
|
|
|
'alternate_title' => NULL,
|
2017-03-29 13:29:03 -04:00
|
|
|
'slug' => $manga['slug'],
|
|
|
|
'url' => 'https://kitsu.io/manga/' . $manga['slug'],
|
|
|
|
'type' => $manga['mangaType'],
|
|
|
|
'image' => $manga['posterImage']['small'],
|
|
|
|
'genres' => $genres,
|
2016-12-20 12:55:43 -05:00
|
|
|
],
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Untransform data to update the api
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function untransform($item)
|
|
|
|
{
|
|
|
|
$rereading = (array_key_exists('rereading', $item)) && (bool)$item['rereading'];
|
|
|
|
|
|
|
|
$map = [
|
|
|
|
'id' => $item['id'],
|
2017-03-29 13:29:03 -04:00
|
|
|
'mal_id' => $item['mal_id'],
|
2017-01-09 20:36:48 -05:00
|
|
|
'data' => [
|
|
|
|
'status' => $item['status'],
|
|
|
|
'reconsuming' => $rereading,
|
|
|
|
'reconsumeCount' => (int)$item['reread_count'],
|
|
|
|
'notes' => $item['notes'],
|
|
|
|
],
|
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)
|
2017-03-29 16:09:22 -04:00
|
|
|
{
|
|
|
|
$map['data']['rating'] = $item['new_rating'] / 2;
|
|
|
|
}
|
|
|
|
|
2016-12-20 12:55:43 -05:00
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of MangaListTransformer.php
|