HummingBirdAnimeClient/src/Controller/Character.php

221 lines
4.8 KiB
PHP
Raw Normal View History

2017-03-30 16:49:48 -04:00
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
2017-03-30 16:49:48 -04:00
*
2018-10-01 11:35:51 -04:00
* PHP version 7.1
2017-03-30 16:49:48 -04:00
*
* @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
2018-10-01 11:35:51 -04:00
* @version 4.1
2017-03-30 16:49:48 -04:00
* @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 {
2017-04-05 13:01:51 -04:00
use ArrayWrapper;
2018-02-02 09:50:58 -05:00
/**
* Show information about a character
*
* @param string $slug
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function index(string $slug): void
2017-04-05 13:01:51 -04:00
{
$model = $this->container->get('kitsu-model');
$rawData = $model->getCharacter($slug);
if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
{
2018-01-16 14:58:07 -05:00
$this->notFound(
2017-04-05 13:01:51 -04:00
$this->formatTitle(
'Characters',
'Character not found'
),
'Character Not Found'
);
2018-01-16 14:58:07 -05:00
return;
2017-04-05 13:01:51 -04:00
}
$data = JsonAPI::organizeData($rawData);
$data['names'] = array_unique(
array_merge(
[ $data[0]['attributes']['canonicalName'] ],
$data[0]['attributes']['names']
)
);
$data['name'] = array_shift($data['names']);
if (array_key_exists('included', $data))
{
if (array_key_exists('anime', $data['included']))
{
uasort($data['included']['anime'], function ($a, $b) {
return $a['attributes']['canonicalTitle'] <=> $b['attributes']['canonicalTitle'];
});
}
if (array_key_exists('manga', $data['included']))
{
uasort($data['included']['manga'], function ($a, $b) {
return $a['attributes']['canonicalTitle'] <=> $b['attributes']['canonicalTitle'];
});
}
}
2017-04-05 13:01:51 -04:00
$viewData = [
'title' => $this->formatTitle(
'Characters',
$data[0]['attributes']['name']
),
'data' => $data,
'castCount' => 0,
2017-04-05 13:01:51 -04:00
'castings' => []
];
if (array_key_exists('included', $data))
2017-04-05 13:01:51 -04:00
{
if (array_key_exists('castings', $data['included']))
{
$viewData['castings'] = $this->organizeCast($data['included']['castings']);
$viewData['castCount'] = $this->getCastCount($viewData['castings']);
}
2017-04-05 13:01:51 -04:00
}
$this->outputHTML('character', $viewData);
}
/**
* 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'];
2018-02-02 09:50:58 -05:00
$hasName = array_key_exists($person['name'], $people);
2017-04-05 13:01:51 -04:00
2018-02-02 09:50:58 -05:00
if ( ! $hasName)
2017-04-05 13:01:51 -04:00
{
$people[$person['name']] = $i;
$role['relationships']['media']['anime'] = [current($role['relationships']['media']['anime'])];
$output[$i] = $role;
$i++;
continue;
}
2018-02-02 09:50:58 -05:00
if (array_key_exists('anime', $role['relationships']['media']))
2017-04-05 13:01:51 -04:00
{
2018-02-02 09:50:58 -05:00
$key = $people[$person['name']];
$output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']);
2017-04-05 13:01:51 -04:00
}
2018-02-02 09:50:58 -05:00
continue;
2017-04-05 13:01:51 -04:00
}
return $output;
}
2018-01-16 14:58:07 -05:00
protected function getCastCount(array $cast): int
{
$count = 0;
2018-01-16 14:58:07 -05:00
foreach($cast as $role)
{
$count++;
/* if (
2018-01-16 14:58:07 -05:00
array_key_exists('attributes', $role) &&
array_key_exists('role', $role['attributes']) &&
2018-02-02 09:50:58 -05:00
$role['attributes']['role'] !== NULL
) {
$count++;
} */
}
2018-01-16 14:58:07 -05:00
return $count;
}
2017-04-05 13:01:51 -04:00
protected function organizeCast(array $cast): array
2017-04-05 13:01:51 -04:00
{
$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)
{
foreach($role['relationships']['person']['people'] as $pid => $peoples)
{
$p = $peoples;
}
$person = $p['attributes'];
$person['id'] = $pid;
$person['image'] = $person['image']['original'];
uasort($role['relationships']['media']['anime'], function ($a, $b) {
return $a['attributes']['canonicalTitle'] <=> $b['attributes']['canonicalTitle'];
});
2017-04-05 13:01:51 -04:00
$item = [
'person' => $person,
'series' => $role['relationships']['media']['anime']
];
$output[$roleName][$language][] = $item;
}
else
{
foreach($role['relationships']['person']['people'] as $pid => $person)
{
$person['id'] = $pid;
$output[$roleName][$pid] = $person;
}
2017-04-05 13:01:51 -04:00
}
}
return $output;
}
2017-03-08 12:55:49 -05:00
}