2017-01-05 13:41:32 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-05 13:41:32 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-01-05 13:41:32 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2017-01-05 13:41:32 -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-05 13:41:32 -05:00
|
|
|
|
2020-08-26 15:22:14 -04:00
|
|
|
use Aviat\AnimeClient\Kitsu;
|
2018-11-08 11:36:42 -05:00
|
|
|
use Aviat\AnimeClient\Types\AnimePage;
|
2017-01-05 13:41:32 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime description page
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class AnimeTransformer extends AbstractTransformer {
|
2017-01-05 13:41:32 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw api response to a more
|
|
|
|
* logical and workable structure
|
|
|
|
*
|
|
|
|
* @param array $item API library item
|
2018-11-08 11:36:42 -05:00
|
|
|
* @return AnimePage
|
2017-01-05 13:41:32 -05:00
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
public function transform($item): AnimePage
|
2017-01-05 13:41:32 -05:00
|
|
|
{
|
2020-12-02 12:42:47 -05:00
|
|
|
$base = $item['data']['findAnimeBySlug'] ?? $item['data']['findAnimeById'] ?? $item['data']['randomMedia'];
|
2020-07-28 17:46:18 -04:00
|
|
|
$characters = [];
|
2020-08-24 15:20:07 -04:00
|
|
|
$links = [];
|
2020-07-28 17:46:18 -04:00
|
|
|
$staff = [];
|
|
|
|
$genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']);
|
|
|
|
|
|
|
|
sort($genres);
|
|
|
|
|
|
|
|
$title = $base['titles']['canonical'];
|
2020-07-29 16:25:57 -04:00
|
|
|
$titles = Kitsu::getTitles($base['titles']);
|
|
|
|
$titles_more = Kitsu::filterLocalizedTitles($base['titles']);
|
2020-07-28 17:46:18 -04:00
|
|
|
|
2020-07-29 11:00:54 -04:00
|
|
|
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] = [];
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:00:54 -04:00
|
|
|
$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-07-29 20:06:59 -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']);
|
|
|
|
}
|
2020-07-29 20:06:59 -04:00
|
|
|
}
|
2020-08-24 13:07:47 -04:00
|
|
|
|
|
|
|
krsort($characters);
|
2020-07-29 11:00:54 -04:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:04:03 -04:00
|
|
|
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'],
|
|
|
|
'name' => $name,
|
|
|
|
'image' => [
|
|
|
|
'original' => $person['image']['original']['url'],
|
|
|
|
],
|
2020-08-05 20:57:01 -04:00
|
|
|
'slug' => $person['slug'],
|
2020-07-29 14:04:03 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
usort($staff[$role], fn ($a, $b) => $a['name'] <=> $b['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($staff);
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:20:07 -04:00
|
|
|
if (count($base['mappings']['nodes']) > 0)
|
|
|
|
{
|
|
|
|
$links = Kitsu::mappingsToUrls($base['mappings']['nodes'], "https://kitsu.io/anime/{$base['slug']}");
|
|
|
|
}
|
|
|
|
|
2020-07-29 20:06:59 -04:00
|
|
|
return AnimePage::from([
|
2020-07-28 17:46:18 -04:00
|
|
|
'age_rating' => $base['ageRating'],
|
|
|
|
'age_rating_guide' => $base['ageRatingGuide'],
|
|
|
|
'characters' => $characters,
|
|
|
|
'cover_image' => $base['posterImage']['views'][1]['url'],
|
|
|
|
'episode_count' => $base['episodeCount'],
|
2020-07-29 15:49:16 -04:00
|
|
|
'episode_length' => $base['episodeLength'],
|
2020-07-28 17:46:18 -04:00
|
|
|
'genres' => $genres,
|
2020-08-24 15:20:07 -04:00
|
|
|
'links' => $links,
|
2020-07-28 17:46:18 -04:00
|
|
|
'id' => $base['id'],
|
|
|
|
'slug' => $base['slug'],
|
|
|
|
'staff' => $staff,
|
2020-08-03 14:36:14 -04:00
|
|
|
'show_type' => $base['subtype'],
|
2020-07-28 17:46:18 -04:00
|
|
|
'status' => Kitsu::getAiringStatus($base['startDate'], $base['endDate']),
|
2020-08-24 15:20:07 -04:00
|
|
|
'streaming_links' => Kitsu::parseStreamingLinks($base['streamingLinks']['nodes'] ?? []),
|
2020-08-17 10:45:17 -04:00
|
|
|
'synopsis' => $base['description']['en'],
|
2020-07-28 17:46:18 -04:00
|
|
|
'title' => $title,
|
2020-07-29 16:25:57 -04:00
|
|
|
'titles' => $titles,
|
|
|
|
'titles_more' => $titles_more,
|
2020-07-29 15:49:16 -04:00
|
|
|
'total_length' => $base['totalLength'],
|
2020-07-28 17:46:18 -04:00
|
|
|
'trailer_id' => $base['youtubeTrailerVideoId'],
|
|
|
|
'url' => "https://kitsu.io/anime/{$base['slug']}",
|
2020-07-29 20:06:59 -04:00
|
|
|
]);
|
2020-07-28 17:46:18 -04:00
|
|
|
}
|
2016-12-22 21:36:23 -05:00
|
|
|
}
|