2017-03-30 16:49:48 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2018-01-15 14:43:15 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2017-03-30 16:49:48 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 4.0
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
2017-04-05 13:01:51 -04:00
|
|
|
namespace Aviat\AnimeClient\Controller;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\Controller as BaseController;
|
|
|
|
use Aviat\AnimeClient\API\JsonAPI;
|
|
|
|
use Aviat\Ion\ArrayWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for character description pages
|
|
|
|
*/
|
|
|
|
class Character extends BaseController {
|
|
|
|
|
|
|
|
use ArrayWrapper;
|
|
|
|
|
|
|
|
public function index(string $slug)
|
|
|
|
{
|
|
|
|
$model = $this->container->get('kitsu-model');
|
|
|
|
|
|
|
|
$rawData = $model->getCharacter($slug);
|
|
|
|
|
|
|
|
if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
|
|
|
|
{
|
|
|
|
return $this->notFound(
|
|
|
|
$this->formatTitle(
|
|
|
|
'Characters',
|
|
|
|
'Character not found'
|
|
|
|
),
|
|
|
|
'Character Not Found'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = JsonAPI::organizeData($rawData);
|
|
|
|
|
|
|
|
$viewData = [
|
|
|
|
'title' => $this->formatTitle(
|
|
|
|
'Characters',
|
|
|
|
$data[0]['attributes']['name']
|
|
|
|
),
|
|
|
|
'data' => $data,
|
2017-04-06 21:27:03 -04:00
|
|
|
'castCount' => 0,
|
2017-04-05 13:01:51 -04:00
|
|
|
'castings' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
if (array_key_exists('included', $data) && array_key_exists('castings', $data['included']))
|
|
|
|
{
|
|
|
|
$viewData['castings'] = $this->organizeCast($data['included']['castings']);
|
2017-04-06 21:27:03 -04:00
|
|
|
$viewData['castCount'] = $this->getCastCount($viewData['castings']);
|
2017-04-05 13:01:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputHTML('character', $viewData);
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:27:03 -04:00
|
|
|
/**
|
|
|
|
* Organize VA => anime relationships
|
|
|
|
*
|
|
|
|
* @param array $cast
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-04-05 13:01:51 -04:00
|
|
|
private function dedupeCast(array $cast): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
$people = [];
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach ($cast as &$role)
|
|
|
|
{
|
|
|
|
if (empty($role['attributes']['role']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$person = current($role['relationships']['person']['people'])['attributes'];
|
|
|
|
|
|
|
|
if ( ! array_key_exists($person['name'], $people))
|
|
|
|
{
|
|
|
|
$people[$person['name']] = $i;
|
|
|
|
$role['relationships']['media']['anime'] = [current($role['relationships']['media']['anime'])];
|
|
|
|
$output[$i] = $role;
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(array_key_exists($person['name'], $people))
|
|
|
|
{
|
|
|
|
if (array_key_exists('anime', $role['relationships']['media']))
|
|
|
|
{
|
|
|
|
$key = $people[$person['name']];
|
|
|
|
$output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2017-04-06 21:27:03 -04:00
|
|
|
|
|
|
|
private function getCastCount(array $cast): int
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
foreach($cast as $role)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
array_key_exists('attributes', $role) &&
|
|
|
|
array_key_exists('role', $role['attributes']) &&
|
|
|
|
( ! is_null($role['attributes']['role']))
|
|
|
|
) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
2017-04-05 13:01:51 -04:00
|
|
|
|
|
|
|
private function organizeCast(array $cast): array
|
|
|
|
{
|
|
|
|
$cast = $this->dedupeCast($cast);
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach($cast as $id => $role)
|
|
|
|
{
|
|
|
|
if (empty($role['attributes']['role']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$language = $role['attributes']['language'];
|
|
|
|
$roleName = $role['attributes']['role'];
|
|
|
|
$isVA = $role['attributes']['voiceActor'];
|
|
|
|
|
|
|
|
if ($isVA)
|
|
|
|
{
|
|
|
|
$person = current($role['relationships']['person']['people'])['attributes'];
|
|
|
|
$name = $person['name'];
|
|
|
|
$item = [
|
|
|
|
'person' => $person,
|
|
|
|
'series' => $role['relationships']['media']['anime']
|
|
|
|
];
|
|
|
|
|
|
|
|
$output[$roleName][$language][] = $item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$output[$roleName][] = $role['relationships']['person']['people'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2017-03-08 12:55:49 -05:00
|
|
|
}
|