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
|
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2018-11-08 11:36:42 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-06 09:16:35 -05:00
|
|
|
* @copyright 2015 - 2019 Timothy J. Warren
|
2018-11-08 11:36:42 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2018-11-08 11:36:42 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\API\JsonAPI;
|
|
|
|
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 14:41:04 -05:00
|
|
|
public function transform(array $personData): Person
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
|
|
|
$data = JsonAPI::organizeData($personData);
|
|
|
|
$included = JsonAPI::organizeIncludes($personData['included']);
|
|
|
|
|
|
|
|
$orgData = $this->organizeData($included);
|
|
|
|
|
|
|
|
return new Person([
|
|
|
|
'id' => $data['id'],
|
|
|
|
'name' => $data['attributes']['name'],
|
|
|
|
'characters' => $orgData['characters'],
|
|
|
|
'staff' => $orgData['staff'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function organizeData(array $data): array
|
|
|
|
{
|
|
|
|
$output = [
|
|
|
|
'characters' => [
|
|
|
|
'main' => [],
|
|
|
|
'supporting' => [],
|
|
|
|
],
|
|
|
|
'staff' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (array_key_exists('characterVoices', $data))
|
|
|
|
{
|
|
|
|
foreach ($data['characterVoices'] as $cv)
|
|
|
|
{
|
|
|
|
$mcId = $cv['relationships']['mediaCharacter']['data']['id'];
|
|
|
|
|
|
|
|
if ( ! array_key_exists($mcId, $data['mediaCharacters']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mc = $data['mediaCharacters'][$mcId];
|
|
|
|
|
|
|
|
$role = $mc['role'];
|
|
|
|
|
|
|
|
$charId = $mc['relationships']['character']['data']['id'];
|
|
|
|
$mediaId = $mc['relationships']['media']['data']['id'];
|
|
|
|
|
|
|
|
$existingMedia = array_key_exists($charId, $output['characters'][$role])
|
|
|
|
? $output['characters'][$role][$charId]['media']
|
|
|
|
: [];
|
|
|
|
|
|
|
|
$relatedMedia = [
|
|
|
|
$mediaId => $data['anime'][$mediaId],
|
|
|
|
];
|
|
|
|
|
|
|
|
$includedMedia = array_replace_recursive($existingMedia, $relatedMedia);
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($includedMedia, static function ($a, $b) {
|
2018-11-08 11:36:42 -05:00
|
|
|
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
|
|
|
|
});
|
|
|
|
|
|
|
|
$character = $data['characters'][$charId];
|
|
|
|
|
|
|
|
$output['characters'][$role][$charId] = [
|
|
|
|
'character' => $character,
|
|
|
|
'media' => $includedMedia,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('mediaStaff', $data))
|
|
|
|
{
|
|
|
|
foreach ($data['mediaStaff'] as $rid => $role)
|
|
|
|
{
|
|
|
|
$roleName = $role['role'];
|
|
|
|
$mediaType = $role['relationships']['media']['data']['type'];
|
|
|
|
$mediaId = $role['relationships']['media']['data']['id'];
|
|
|
|
$media = $data[$mediaType][$mediaId];
|
|
|
|
$output['staff'][$roleName][$mediaType][$mediaId] = $media;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($output['characters']['main'], static function ($a, $b) {
|
2018-11-08 11:36:42 -05:00
|
|
|
return $a['character']['canonicalName'] <=> $b['character']['canonicalName'];
|
|
|
|
});
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($output['characters']['supporting'], static function ($a, $b) {
|
2018-11-08 11:36:42 -05:00
|
|
|
return $a['character']['canonicalName'] <=> $b['character']['canonicalName'];
|
|
|
|
});
|
|
|
|
ksort($output['staff']);
|
|
|
|
foreach ($output['staff'] as $role => &$media)
|
|
|
|
{
|
|
|
|
if (array_key_exists('anime', $media))
|
|
|
|
{
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($media['anime'], static function ($a, $b) {
|
2018-11-08 11:36:42 -05:00
|
|
|
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('manga', $media))
|
|
|
|
{
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($media['manga'], static function ($a, $b) {
|
2018-11-08 11:36:42 -05:00
|
|
|
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|