HummingBirdAnimeClient/src/Controller/People.php

161 lines
3.8 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
* PHP version 7.1
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Controller;
use Aviat\AnimeClient\Controller as BaseController;
use Aviat\AnimeClient\API\JsonAPI;
/**
* Controller for People pages
*/
final class People extends BaseController {
/**
* Show information about a person
*
* @param string $id
* @return void
*/
public function index(string $id): void
{
$model = $this->container->get('kitsu-model');
$rawData = $model->getPerson($id);
if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
{
$this->notFound(
$this->formatTitle(
'People',
'Person not found'
),
'Person Not Found'
);
return;
}
$data = JsonAPI::organizeData($rawData);
2018-11-01 22:12:41 -04:00
$included = JsonAPI::organizeIncludes($rawData['included']);
$orgData = $this->organizeData($included);
$viewData = [
2018-11-01 22:12:41 -04:00
'included' => $included,
'title' => $this->formatTitle(
'People',
$data['attributes']['name']
),
'data' => $data,
'castCount' => 0,
2018-11-01 22:12:41 -04:00
'castings' => [],
'characters' => $orgData['characters'],
'staff' => $orgData['staff'],
];
2018-11-01 22:12:41 -04:00
$this->outputHTML('person/details', $viewData);
}
2018-11-01 22:12:41 -04:00
protected function organizeData(array $data): array
{
2018-11-01 22:12:41 -04:00
$output = [
'characters' => [
'main' => [],
'supporting' => [],
],
'staff' => [],
];
2018-11-01 22:12:41 -04:00
if (array_key_exists('characterVoices', $data))
{
2018-11-01 22:12:41 -04:00
foreach ($data['characterVoices'] as $cv)
{
2018-11-01 22:12:41 -04:00
$mcId = $cv['relationships']['mediaCharacter']['data']['id'];
2018-11-01 22:12:41 -04:00
if ( ! array_key_exists($mcId, $data['mediaCharacters']))
{
continue;
}
2018-11-01 22:12:41 -04:00
$mc = $data['mediaCharacters'][$mcId];
2018-11-01 22:12:41 -04:00
$role = $mc['role'];
2018-11-01 22:12:41 -04:00
$charId = $mc['relationships']['character']['data']['id'];
$mediaId = $mc['relationships']['media']['data']['id'];
2018-11-01 22:12:41 -04:00
$existingMedia = array_key_exists($charId, $output['characters'][$role])
? $output['characters'][$role][$charId]['media']
: [];
$relatedMedia = [
$mediaId => $data['anime'][$mediaId],
];
$includedMedia = array_replace_recursive($existingMedia, $relatedMedia);
2018-11-01 22:12:41 -04:00
uasort($includedMedia, function ($a, $b) {
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
});
2018-11-01 22:12:41 -04:00
$character = $data['characters'][$charId];
$output['characters'][$role][$charId] = [
'character' => $character,
'media' => $includedMedia,
];
}
2018-11-01 22:12:41 -04:00
}
2018-11-01 22:12:41 -04:00
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;
}
}
uasort($output['characters']['main'], function ($a, $b) {
return $a['character']['canonicalName'] <=> $b['character']['canonicalName'];
});
uasort($output['characters']['supporting'], function ($a, $b) {
return $a['character']['canonicalName'] <=> $b['character']['canonicalName'];
});
ksort($output['staff']);
foreach($output['staff'] as $role => &$media)
{
if (array_key_exists('anime', $media))
{
2018-11-01 22:12:41 -04:00
uasort($media['anime'], function ($a, $b) {
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
});
}
2018-11-01 22:12:41 -04:00
if (array_key_exists('manga', $media))
{
2018-11-01 22:12:41 -04:00
uasort($media['manga'], function ($a, $b) {
return $a['canonicalTitle'] <=> $b['canonicalTitle'];
});
}
}
return $output;
}
}