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

136 lines
3.5 KiB
PHP
Raw Normal View History

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
*
* 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-08-04 09:30:21 -04:00
* @version 5.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
2017-01-04 13:16:58 -05:00
2020-08-26 15:22:14 -04:00
use Aviat\AnimeClient\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
*/
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
{
$base = $item['data']['findMangaBySlug'] ?? $item['data']['findMangaById'] ?? $item['data']['randomMedia'];
$characters = [];
$links = [];
$staff = [];
$genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']);
sort($genres);
$title = $base['titles']['canonical'];
$titles = Kitsu::getTitles($base['titles']);
$titles_more = Kitsu::filterLocalizedTitles($base['titles']);
if (count($base['characters']['nodes']) > 0)
{
foreach ($base['characters']['nodes'] as $rawCharacter)
{
2020-08-24 13:07:47 -04:00
$type = mb_strtolower($rawCharacter['role']);
if ( ! isset($characters[$type]))
{
$characters[$type] = [];
}
$details = $rawCharacter['character'];
$characters[$type][$details['id']] = [
'image' => $details['image'],
'name' => $details['names']['canonical'],
'slug' => $details['slug'],
];
}
2020-08-24 13:07:47 -04:00
foreach (array_keys($characters) as $type)
{
2020-08-24 13:07:47 -04:00
if (empty($characters[$type]))
{
unset($characters[$type]);
}
else
{
uasort($characters[$type], fn($a, $b) => $a['name'] <=> $b['name']);
}
}
2020-08-24 13:07:47 -04:00
krsort($characters);
}
if (count($base['staff']['nodes']) > 0)
{
foreach ($base['staff']['nodes'] as $staffing)
{
$person = $staffing['person'];
$role = $staffing['role'];
$name = $person['names']['localized'][$person['names']['canonical']];
if ( ! array_key_exists($role, $staff))
{
$staff[$role] = [];
}
$staff[$role][$person['id']] = [
'id' => $person['id'],
'slug' => $person['slug'],
'name' => $name,
'image' => [
'original' => $person['image']['original']['url'],
],
];
usort($staff[$role], fn ($a, $b) => $a['name'] <=> $b['name']);
}
ksort($staff);
}
if (count($base['mappings']['nodes']) > 0)
{
$links = Kitsu::mappingsToUrls($base['mappings']['nodes'], "https://kitsu.io/manga/{$base['slug']}");
}
$data = [
'age_rating' => $base['ageRating'],
'age_rating_guide' => $base['ageRatingGuide'],
'characters' => $characters,
'chapter_count' => $base['chapterCount'],
'volume_count' => $base['volumeCount'],
'cover_image' => $base['posterImage']['views'][1]['url'],
2018-10-29 15:48:54 -04:00
'genres' => $genres,
'links' => $links,
'manga_type' => $base['subtype'],
'id' => $base['id'],
'staff' => $staff,
'status' => Kitsu::getPublishingStatus($base['status'], $base['startDate'], $base['endDate']),
2020-08-17 11:36:01 -04:00
'synopsis' => $base['description']['en'],
2018-10-29 15:48:54 -04:00
'title' => $title,
'titles' => $titles,
'titles_more' => $titles_more,
'url' => "https://kitsu.io/manga/{$base['slug']}",
];
2017-01-04 13:16:58 -05:00
return MangaPage::from($data);
2017-01-04 13:16:58 -05:00
}
2017-01-03 21:06:49 -05:00
}