2018-11-08 11:36:42 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2018-11-08 11:36:42 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2018-11-08 11:36:42 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2018-11-08 11:36:42 -05:00
|
|
|
* @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;
|
2018-11-08 11:36:42 -05:00
|
|
|
use Aviat\AnimeClient\Types\Character;
|
|
|
|
|
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
2020-08-17 16:36:55 -04:00
|
|
|
use Locale;
|
2018-11-08 11:36:42 -05:00
|
|
|
|
|
|
|
/**
|
2018-11-08 12:15:30 -05:00
|
|
|
* Data transformation class for character pages
|
2018-11-08 11:36:42 -05:00
|
|
|
*/
|
|
|
|
final class CharacterTransformer extends AbstractTransformer {
|
|
|
|
|
2019-12-09 14:41:04 -05:00
|
|
|
/**
|
2021-02-12 13:09:57 -05:00
|
|
|
* @param array|object $item
|
2019-12-09 16:17:25 -05:00
|
|
|
* @return Character
|
2019-12-09 14:41:04 -05:00
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): Character
|
2020-08-17 14:01:55 -04:00
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
$item = (array)$item;
|
|
|
|
$data = $item['data']['findCharacterBySlug'] ?? [];
|
2020-08-17 14:01:55 -04:00
|
|
|
$castings = [];
|
|
|
|
$media = [
|
|
|
|
'anime' => [],
|
|
|
|
'manga' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$names = array_unique(
|
|
|
|
array_merge(
|
|
|
|
[$data['names']['canonical']],
|
|
|
|
array_values($data['names']['localized'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$name = array_shift($names);
|
|
|
|
|
|
|
|
if (isset($data['media']['nodes']))
|
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
[$media, $castings] = $this->organizeMediaAndVoices($data['media']['nodes'] ?? []);
|
2020-08-17 14:01:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Character::from([
|
|
|
|
'castings' => $castings,
|
|
|
|
'description' => $data['description']['en'],
|
|
|
|
'id' => $data['id'],
|
2022-01-17 08:37:00 -05:00
|
|
|
'image' => $data['image']['original']['url'] ?? 'images/placeholder.png',
|
2020-08-17 14:01:55 -04:00
|
|
|
'media' => $media,
|
|
|
|
'name' => $name,
|
|
|
|
'names' => $names,
|
|
|
|
'otherNames' => $data['names']['alternatives'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function organizeMediaAndVoices (array $data): array
|
|
|
|
{
|
|
|
|
if (empty($data))
|
|
|
|
{
|
|
|
|
return [[], []];
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:36:55 -04:00
|
|
|
$titleSort = fn ($a, $b) => $a['title'] <=> $b['title'];
|
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
// First, let's deal with related media
|
2020-08-17 14:01:55 -04:00
|
|
|
$rawMedia = array_column($data, 'media');
|
|
|
|
$rawAnime = array_filter($rawMedia, fn ($item) => $item['type'] === 'Anime');
|
|
|
|
$rawManga = array_filter($rawMedia, fn ($item) => $item['type'] === 'Manga');
|
|
|
|
|
2020-08-17 16:36:55 -04:00
|
|
|
$anime = array_map(static function ($item) {
|
|
|
|
$output = $item;
|
|
|
|
unset($output['titles']);
|
|
|
|
$output['title'] = $item['titles']['canonical'];
|
|
|
|
$output['titles'] = Kitsu::getFilteredTitles($item['titles']);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}, $rawAnime);
|
|
|
|
$manga = array_map(static function ($item) {
|
|
|
|
$output = $item;
|
|
|
|
unset($output['titles']);
|
|
|
|
$output['title'] = $item['titles']['canonical'];
|
|
|
|
$output['titles'] = Kitsu::getFilteredTitles($item['titles']);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}, $rawManga);
|
|
|
|
|
|
|
|
uasort($anime, $titleSort);
|
|
|
|
uasort($manga, $titleSort);
|
2020-08-17 14:01:55 -04:00
|
|
|
|
|
|
|
$media = [
|
2020-08-17 16:36:55 -04:00
|
|
|
'anime' => $anime,
|
|
|
|
'manga' => $manga,
|
2020-08-17 14:01:55 -04:00
|
|
|
];
|
|
|
|
|
2020-08-18 16:59:08 -04:00
|
|
|
// And now, reorganize voice actor relationships
|
2021-12-02 16:06:34 -05:00
|
|
|
$rawVoices = array_filter($data, fn($item) => (! empty($item['voices'])) && count((array)$item['voices']['nodes']) > 0);
|
2020-08-17 16:36:55 -04:00
|
|
|
|
|
|
|
if (empty($rawVoices))
|
|
|
|
{
|
|
|
|
return [$media, []];
|
|
|
|
}
|
|
|
|
|
|
|
|
$castings = [
|
|
|
|
'Voice Actor' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($rawVoices as $voiceMap)
|
|
|
|
{
|
|
|
|
foreach ($voiceMap['voices']['nodes'] as $voice)
|
|
|
|
{
|
|
|
|
$lang = Locale::getDisplayLanguage($voice['locale'], 'en');
|
|
|
|
$id = $voice['person']['name'];
|
|
|
|
$seriesId = $voiceMap['media']['id'];
|
|
|
|
|
|
|
|
if ( ! array_key_exists($lang, $castings['Voice Actor']))
|
|
|
|
{
|
|
|
|
$castings['Voice Actor'][$lang] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! array_key_exists($id, $castings['Voice Actor'][$lang]))
|
|
|
|
{
|
|
|
|
$castings['Voice Actor'][$lang][$id] = [
|
|
|
|
'person' => [
|
|
|
|
'id' => $voice['person']['id'],
|
|
|
|
'slug' => $voice['person']['slug'],
|
|
|
|
'image' => $voice['person']['image']['original']['url'],
|
|
|
|
'name' => $voice['person']['name'],
|
|
|
|
],
|
|
|
|
'series' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$castings['Voice Actor'][$lang][$id]['series'][$seriesId] = [
|
|
|
|
'id' => $seriesId,
|
|
|
|
'slug' => $voiceMap['media']['slug'],
|
|
|
|
'title' => $voiceMap['media']['titles']['canonical'],
|
|
|
|
'titles' => Kitsu::getFilteredTitles($voiceMap['media']['titles']),
|
2022-01-17 09:59:27 -05:00
|
|
|
'posterImage' => Kitsu::getPosterImage($voiceMap['media']),
|
2020-08-17 16:36:55 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
uasort($castings['Voice Actor'][$lang][$id]['series'], $titleSort);
|
|
|
|
ksort($castings['Voice Actor'][$lang]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$media, $castings];
|
2020-08-17 14:01:55 -04:00
|
|
|
}
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|