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

150 lines
4.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
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
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
2020-08-26 15:22:14 -04:00
use Aviat\AnimeClient\Kitsu;
use Aviat\AnimeClient\Types\AnimePage;
use Aviat\Ion\Transformer\AbstractTransformer;
/**
* Transformer for anime description page
*/
final class AnimeTransformer extends AbstractTransformer {
/**
* 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
* @return AnimePage
*/
2021-02-12 13:09:57 -05:00
public function transform(array|object $item): AnimePage
{
2021-02-12 13:09:57 -05:00
$item = (array)$item;
$base = $item['data']['findAnimeBySlug'] ?? $item['data']['findAnimeById'] ?? $item['data']['randomMedia'];
$characters = [];
$links = [];
$staff = [];
$genres = array_map(fn ($genre) => $genre['title']['en'], $base['categories']['nodes']);
sort($genres);
2021-02-16 14:43:51 -05:00
$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-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 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 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 14:04:03 -04:00
if ( ! array_key_exists($role, $staff))
{
$staff[$role] = [];
}
$staff[$role][$person['id']] = [
'id' => $person['id'],
'name' => $name,
'image' => [
'original' => $person['image']['original']['url'] ?? '',
2020-07-29 14:04:03 -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);
}
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([
'airDate' => Kitsu::formatAirDates($base['startDate'], $base['endDate']),
'age_rating' => $base['ageRating'],
'age_rating_guide' => $base['ageRatingGuide'],
'characters' => $characters,
2022-01-10 17:19:28 -05:00
'cover_image' => $base['posterImage']['views'][1]['url']
?? $base['posterImage']['original']['url']
?? '/public/images/placeholder.png',
'episode_count' => $base['episodeCount'],
2020-07-29 15:49:16 -04:00
'episode_length' => $base['episodeLength'],
'genres' => $genres,
'links' => $links,
'id' => $base['id'],
'slug' => $base['slug'],
'staff' => $staff,
'show_type' => $base['subtype'],
'status' => Kitsu::getAiringStatus($base['startDate'], $base['endDate']),
'streaming_links' => Kitsu::parseStreamingLinks($base['streamingLinks']['nodes'] ?? []),
'synopsis' => $base['description']['en'] ?? '',
'title' => $title,
'titles' => $titles,
'titles_more' => $titles_more,
2020-07-29 15:49:16 -04:00
'total_length' => $base['totalLength'],
'trailer_id' => $base['youtubeTrailerVideoId'],
'url' => "https://kitsu.io/anime/{$base['slug']}",
2020-07-29 20:06:59 -04:00
]);
}
}