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-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
|
|
|
*/
|
|
|
|
final class PersonTransformer extends AbstractTransformer {
|
|
|
|
|
2019-12-09 16:17:25 -05:00
|
|
|
/**
|
2021-02-12 13:09:57 -05:00
|
|
|
* @param array|object $item
|
2019-12-09 16:17:25 -05:00
|
|
|
* @return Person
|
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function transform(array|object $item): Person
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2021-02-12 13:09:57 -05:00
|
|
|
$item = (array)$item;
|
|
|
|
$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'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
if (count($data['mediaStaff']['nodes']) > 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'));
|
|
|
|
foreach ($roles as $role)
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-27 15:01:00 -04:00
|
|
|
$staff[$role] = [];
|
|
|
|
}
|
|
|
|
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
|
|
|
];
|
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
uasort($staff[$role][$type], 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
|
|
|
}
|
|
|
|
|
2020-08-27 15:01:00 -04:00
|
|
|
if (count($data['voices']['nodes']) > 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'],
|
|
|
|
'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 08:37:00 -05:00
|
|
|
'image' => Kitsu::getPosterImage($character),
|
2020-08-27 15:01:00 -04:00
|
|
|
'canonicalName' => $character['names']['canonical'],
|
|
|
|
],
|
|
|
|
'media' => [
|
|
|
|
$media['id'] => $media
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
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],
|
|
|
|
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'],
|
|
|
|
fn ($a, $b) => $a['titles'][0] <=> $b['titles'][0]
|
|
|
|
);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|