2017-01-04 13:16:58 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-04 13:16:58 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-01-04 13:16:58 -05:00
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2017-01-04 13:16:58 -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
|
2020-04-10 15:39:39 -04:00
|
|
|
* @version 5
|
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;
|
2017-01-04 13:16:58 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
use Aviat\AnimeClient\API\Kitsu;
|
2018-08-08 13:05:38 -04:00
|
|
|
use Aviat\AnimeClient\Types\MangaPage;
|
2017-01-04 13:16:58 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime description page
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class MangaTransformer extends AbstractTransformer {
|
2017-01-04 13:16:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw api response to a more
|
|
|
|
* logical and workable structure
|
|
|
|
*
|
|
|
|
* @param array $item API library item
|
2018-08-08 13:05:38 -04:00
|
|
|
* @return MangaPage
|
2017-01-04 13:16:58 -05:00
|
|
|
*/
|
2018-08-08 13:05:38 -04:00
|
|
|
public function transform($item): MangaPage
|
2017-01-04 13:16:58 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
$base = array_key_exists('findMangaBySlug', $item['data'])
|
|
|
|
? $item['data']['findMangaBySlug']
|
|
|
|
: $item['data']['findMangaById'];
|
2018-08-20 13:01:16 -04:00
|
|
|
|
2018-11-08 12:15:30 -05:00
|
|
|
$characters = [];
|
|
|
|
$staff = [];
|
2020-07-29 17:51:58 -04:00
|
|
|
$genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']);
|
|
|
|
sort($genres);
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
$title = $base['titles']['canonical'];
|
2020-07-30 15:16:24 -04:00
|
|
|
$titles = Kitsu::getTitles($base['titles']);
|
|
|
|
$titles_more = Kitsu::filterLocalizedTitles($base['titles']);
|
2020-07-29 17:51:58 -04:00
|
|
|
|
|
|
|
if (count($base['characters']['nodes']) > 0)
|
2018-11-08 12:15:30 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
$characters['main'] = [];
|
|
|
|
$characters['supporting'] = [];
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
foreach ($base['characters']['nodes'] as $rawCharacter)
|
2018-11-08 12:15:30 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
$type = $rawCharacter['role'] === 'MAIN' ? 'main' : 'supporting';
|
|
|
|
$details = $rawCharacter['character'];
|
|
|
|
$characters[$type][$details['id']] = [
|
|
|
|
'image' => $details['image'],
|
|
|
|
'name' => $details['names']['canonical'],
|
|
|
|
'slug' => $details['slug'],
|
|
|
|
];
|
|
|
|
}
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
uasort($characters['main'], fn($a, $b) => $a['name'] <=> $b['name']);
|
|
|
|
uasort($characters['supporting'], fn($a, $b) => $a['name'] <=> $b['name']);
|
|
|
|
|
|
|
|
if (empty($characters['supporting']))
|
|
|
|
{
|
|
|
|
unset($characters['supporting']);
|
2018-11-08 12:15:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
if (count($base['staff']['nodes']) > 0)
|
2018-11-08 12:15:30 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
foreach ($base['staff']['nodes'] as $staffing)
|
2018-11-08 12:15:30 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
$person = $staffing['person'];
|
|
|
|
$role = $staffing['role'];
|
|
|
|
$name = $person['names']['localized'][$person['names']['canonical']];
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
if ( ! array_key_exists($role, $staff))
|
2018-11-08 12:15:30 -05:00
|
|
|
{
|
2020-07-29 17:51:58 -04:00
|
|
|
$staff[$role] = [];
|
2018-11-08 12:15:30 -05:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
$staff[$role][$person['id']] = [
|
|
|
|
'id' => $person['id'],
|
|
|
|
'name' => $name,
|
|
|
|
'image' => [
|
|
|
|
'original' => $person['image']['original']['url'],
|
|
|
|
],
|
|
|
|
];
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
usort($staff[$role], fn ($a, $b) => $a['name'] <=> $b['name']);
|
|
|
|
}
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
ksort($staff);
|
|
|
|
}
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
$data = [
|
|
|
|
'age_rating' => $base['ageRating'],
|
|
|
|
'age_rating_guide' => $base['ageRatingGuide'],
|
2018-11-08 12:15:30 -05:00
|
|
|
'characters' => $characters,
|
2020-08-03 14:36:14 -04:00
|
|
|
'chapter_count' => $base['chapterCount'],
|
|
|
|
'volume_count' => $base['volumeCount'],
|
2020-07-29 17:51:58 -04:00
|
|
|
'cover_image' => $base['posterImage']['views'][1]['url'],
|
2018-10-29 15:48:54 -04:00
|
|
|
'genres' => $genres,
|
2020-08-03 14:36:14 -04:00
|
|
|
'manga_type' => $base['subtype'],
|
2020-07-29 17:51:58 -04:00
|
|
|
'id' => $base['id'],
|
2018-11-08 12:15:30 -05:00
|
|
|
'staff' => $staff,
|
2020-07-29 17:51:58 -04:00
|
|
|
'status' => Kitsu::getPublishingStatus($base['status'], $base['startDate'], $base['endDate']),
|
|
|
|
'synopsis' => $base['synopsis']['en'],
|
2018-10-29 15:48:54 -04:00
|
|
|
'title' => $title,
|
|
|
|
'titles' => $titles,
|
2020-07-30 15:16:24 -04:00
|
|
|
'titles_more' => $titles_more,
|
2020-07-29 17:51:58 -04:00
|
|
|
'url' => "https://kitsu.io/manga/{$base['slug']}",
|
|
|
|
];
|
2017-01-04 13:16:58 -05:00
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
return MangaPage::from($data);
|
2017-01-04 13:16:58 -05:00
|
|
|
}
|
2017-01-03 21:06:49 -05:00
|
|
|
}
|