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
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
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>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2017-01-06 23:34:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
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-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;
|
|
|
|
|
|
|
|
/**
|
2021-12-02 16:06:34 -05:00
|
|
|
* Transformer for manga description page
|
2017-01-04 13:16:58 -05:00
|
|
|
*/
|
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
|
|
|
|
*
|
2021-02-12 13:09:57 -05:00
|
|
|
* @param array|object $item API library item
|
2017-01-04 13:16:58 -05:00
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): MangaPage
|
2017-01-04 13:16:58 -05:00
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
$item = (array)$item;
|
2020-12-02 12:42:47 -05:00
|
|
|
$base = $item['data']['findMangaBySlug'] ?? $item['data']['findMangaById'] ?? $item['data']['randomMedia'];
|
2018-11-08 12:15:30 -05:00
|
|
|
$characters = [];
|
2020-08-24 15:20:07 -04:00
|
|
|
$links = [];
|
2018-11-08 12:15:30 -05:00
|
|
|
$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
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
if ((is_countable($base['characters']['nodes']) ? count($base['characters']['nodes']) : 0) > 0)
|
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-08-24 13:07:47 -04:00
|
|
|
$type = mb_strtolower($rawCharacter['role']);
|
|
|
|
if ( ! isset($characters[$type]))
|
|
|
|
{
|
|
|
|
$characters[$type] = [];
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:51:58 -04:00
|
|
|
$details = $rawCharacter['character'];
|
|
|
|
$characters[$type][$details['id']] = [
|
2022-01-17 09:59:27 -05:00
|
|
|
'image' => $details['image']['original']['url'],
|
2020-07-29 17:51:58 -04:00
|
|
|
'name' => $details['names']['canonical'],
|
|
|
|
'slug' => $details['slug'],
|
|
|
|
];
|
|
|
|
}
|
2018-11-08 12:15:30 -05:00
|
|
|
|
2020-08-24 13:07:47 -04:00
|
|
|
foreach (array_keys($characters) as $type)
|
2020-07-29 17:51:58 -04:00
|
|
|
{
|
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']);
|
|
|
|
}
|
2018-11-08 12:15:30 -05:00
|
|
|
}
|
2020-08-24 13:07:47 -04:00
|
|
|
|
|
|
|
krsort($characters);
|
2018-11-08 12:15:30 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
if ((is_countable($base['staff']['nodes']) ? count($base['staff']['nodes']) : 0) > 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
|
|
|
|
2021-12-02 16:06:34 -05:00
|
|
|
// If this person object is so broken as to not have a proper image object,
|
|
|
|
// just skip it. No point in showing a role with nothing in it.
|
|
|
|
if ($person === null || $person['id'] === null || $person['image'] === null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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'],
|
2020-08-05 20:57:01 -04:00
|
|
|
'slug' => $person['slug'],
|
2020-07-29 17:51:58 -04:00
|
|
|
'name' => $name,
|
2022-01-17 09:59:27 -05:00
|
|
|
'image' => $person['image']['original']['url'],
|
2020-07-29 17:51:58 -04:00
|
|
|
];
|
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
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
if ((is_countable($base['mappings']['nodes']) ? count($base['mappings']['nodes']) : 0) > 0)
|
2020-08-24 15:20:07 -04:00
|
|
|
{
|
|
|
|
$links = Kitsu::mappingsToUrls($base['mappings']['nodes'], "https://kitsu.io/manga/{$base['slug']}");
|
|
|
|
}
|
|
|
|
|
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'],
|
2022-01-12 18:23:40 -05:00
|
|
|
'cover_image' => Kitsu::getPosterImage($base),
|
2018-10-29 15:48:54 -04:00
|
|
|
'genres' => $genres,
|
2020-08-24 15:20:07 -04:00
|
|
|
'links' => $links,
|
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']),
|
2020-08-17 11:36:01 -04:00
|
|
|
'synopsis' => $base['description']['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
|
|
|
}
|