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
|
2022-03-04 12:32:17 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshome.page>
|
|
|
|
* @copyright 2015 - 2022 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-27 15:01:00 -04:00
|
|
|
use Aviat\AnimeClient\Kitsu;
|
2018-11-08 11:36:42 -05:00
|
|
|
use Aviat\AnimeClient\Types\Person;
|
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
2018-11-08 12:15:30 -05:00
|
|
|
* Data transformation class for people pages
|
2018-11-08 11:36:42 -05:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
final class PersonTransformer extends AbstractTransformer
|
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): Person
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$item = (array) $item;
|
2021-02-12 13:09:57 -05:00
|
|
|
$data = $item['data']['findPersonBySlug'] ?? [];
|
2020-08-27 15:01:00 -04:00
|
|
|
$canonicalName = $data['names']['localized'][$data['names']['canonical']]
|
|
|
|
?? array_shift($data['names']['localized']);
|
2018-11-08 11:36:42 -05:00
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
$orgData = $this->organizeData($data);
|
2018-11-08 11:36:42 -05:00
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
return Person::from([
|
2018-11-08 11:36:42 -05:00
|
|
|
'id' => $data['id'],
|
2020-08-27 15:01:00 -04:00
|
|
|
'name' => $canonicalName,
|
2022-01-17 08:37:00 -05:00
|
|
|
'image' => $data['image']['original']['url'],
|
2020-08-27 15:01:00 -04:00
|
|
|
'names' => array_diff($data['names']['localized'], [$canonicalName]),
|
|
|
|
'description' => $data['description']['en'] ?? '',
|
2018-11-08 11:36:42 -05:00
|
|
|
'characters' => $orgData['characters'],
|
|
|
|
'staff' => $orgData['staff'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
/**
|
|
|
|
* @return array<string, array<int|string, array<int|string, array<int|string, array<int|string, mixed>>>>>
|
|
|
|
*/
|
2018-11-08 11:36:42 -05:00
|
|
|
protected function organizeData(array $data): array
|
|
|
|
{
|
|
|
|
$output = [
|
2020-08-27 15:01:00 -04:00
|
|
|
'characters' => [],
|
2018-11-08 11:36:42 -05:00
|
|
|
'staff' => [],
|
|
|
|
];
|
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
$characters = [];
|
|
|
|
$staff = [];
|
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
if ((is_countable($data['mediaStaff']['nodes']) ? count($data['mediaStaff']['nodes']) : 0) > 0)
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
$roles = array_unique(array_column($data['mediaStaff']['nodes'], 'role'));
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
foreach ($roles as $role)
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
$staff[$role] = [];
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
ksort($staff);
|
2018-11-08 11:36:42 -05:00
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
foreach ($data['mediaStaff']['nodes'] as $staffing)
|
|
|
|
{
|
2021-12-02 16:06:34 -05:00
|
|
|
if (empty($staffing['media']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
$media = $staffing['media'];
|
|
|
|
$role = $staffing['role'];
|
|
|
|
$title = $media['titles']['canonical'];
|
|
|
|
$type = strtolower($media['type']);
|
|
|
|
|
|
|
|
$staff[$role][$type][$media['id']] = [
|
|
|
|
'id' => $media['id'],
|
|
|
|
'title' => $title,
|
|
|
|
'titles' => array_merge([$title], Kitsu::getFilteredTitles($media['titles'])),
|
2022-01-17 08:37:00 -05:00
|
|
|
'image' => Kitsu::getPosterImage($media),
|
2020-08-27 15:01:00 -04:00
|
|
|
'slug' => $media['slug'],
|
2018-11-08 11:36:42 -05:00
|
|
|
];
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
uasort($staff[$role][$type], static fn ($a, $b) => $a['title'] <=> $b['title']);
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
$output['staff'] = $staff;
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
|
|
|
|
2022-03-03 13:25:10 -05:00
|
|
|
if ((is_countable($data['voices']['nodes']) ? count($data['voices']['nodes']) : 0) > 0)
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
foreach ($data['voices']['nodes'] as $voicing)
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
$character = $voicing['mediaCharacter']['character'];
|
|
|
|
$charId = $character['id'];
|
|
|
|
$rawMedia = $voicing['mediaCharacter']['media'];
|
|
|
|
$role = strtolower($voicing['mediaCharacter']['role']);
|
|
|
|
|
|
|
|
$media = [
|
|
|
|
'id' => $rawMedia['id'],
|
|
|
|
'slug' => $rawMedia['slug'],
|
2022-01-17 09:59:27 -05:00
|
|
|
'image' => Kitsu::getPosterImage($rawMedia),
|
2020-08-27 15:01:00 -04:00
|
|
|
'titles' => array_merge(
|
|
|
|
[$rawMedia['titles']['canonical']],
|
|
|
|
Kitsu::getFilteredTitles($rawMedia['titles']),
|
|
|
|
),
|
|
|
|
];
|
2018-11-08 11:36:42 -05:00
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
if ( ! isset($characters[$role][$charId]))
|
|
|
|
{
|
|
|
|
if ( ! array_key_exists($role, $characters))
|
|
|
|
{
|
|
|
|
$characters[$role] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$characters[$role][$charId] = [
|
|
|
|
'character' => [
|
|
|
|
'id' => $character['id'],
|
|
|
|
'slug' => $character['slug'],
|
2022-01-17 09:59:27 -05:00
|
|
|
'image' => $character['image']['original']['url'],
|
2020-08-27 15:01:00 -04:00
|
|
|
'canonicalName' => $character['names']['canonical'],
|
|
|
|
],
|
|
|
|
'media' => [
|
2022-03-03 17:26:09 -05:00
|
|
|
$media['id'] => $media,
|
2020-08-27 15:01:00 -04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$characters[$role][$charId]['media'][$media['id']] = $media;
|
|
|
|
}
|
2020-08-27 15:39:23 -04:00
|
|
|
}
|
2020-08-27 15:01:00 -04:00
|
|
|
|
2020-08-27 15:39:23 -04:00
|
|
|
foreach ($characters as $role => $_)
|
|
|
|
{
|
|
|
|
// Sort the characters by name
|
2020-08-27 15:01:00 -04:00
|
|
|
uasort(
|
2020-08-27 15:39:23 -04:00
|
|
|
$characters[$role],
|
2022-03-03 17:26:09 -05:00
|
|
|
static fn ($a, $b) => $a['character']['canonicalName'] <=> $b['character']['canonicalName']
|
2020-08-27 15:01:00 -04:00
|
|
|
);
|
2020-08-27 15:39:23 -04:00
|
|
|
|
|
|
|
// Sort the media for the character
|
|
|
|
foreach ($characters[$role] as $charId => $__)
|
|
|
|
{
|
|
|
|
uasort(
|
|
|
|
$characters[$role][$charId]['media'],
|
2022-03-03 17:26:09 -05:00
|
|
|
static fn ($a, $b) => $a['titles'][0] <=> $b['titles'][0]
|
2020-08-27 15:39:23 -04:00
|
|
|
);
|
|
|
|
}
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
2020-08-27 15:01:00 -04:00
|
|
|
|
|
|
|
krsort($characters);
|
|
|
|
|
|
|
|
$output['characters'] = $characters;
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2022-03-03 17:26:09 -05:00
|
|
|
}
|