API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

People.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu to manage anime and manga watch lists
  6. *
  7. * PHP version 7.1
  8. *
  9. * @package HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.1
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\Controller;
  17. use Aviat\AnimeClient\Controller as BaseController;
  18. use Aviat\AnimeClient\API\JsonAPI;
  19. /**
  20. * Controller for People pages
  21. */
  22. final class People extends BaseController {
  23. /**
  24. * Show information about a person
  25. *
  26. * @param string $id
  27. * @return void
  28. */
  29. public function index(string $id): void
  30. {
  31. $model = $this->container->get('kitsu-model');
  32. $rawData = $model->getPerson($id);
  33. if (( ! array_key_exists('data', $rawData)) || empty($rawData['data']))
  34. {
  35. $this->notFound(
  36. $this->formatTitle(
  37. 'People',
  38. 'Person not found'
  39. ),
  40. 'Person Not Found'
  41. );
  42. return;
  43. }
  44. $data = JsonAPI::organizeData($rawData);
  45. $viewData = [
  46. 'title' => $this->formatTitle(
  47. 'People',
  48. $data['attributes']['name']
  49. ),
  50. 'data' => $data,
  51. 'castCount' => 0,
  52. 'castings' => []
  53. ];
  54. if (array_key_exists('included', $data) && array_key_exists('castings', $data['included']))
  55. {
  56. $viewData['castings'] = $this->organizeCast($data['included']['castings']);
  57. $viewData['castCount'] = count($viewData['castings']);
  58. }
  59. $this->outputHTML('person', $viewData);
  60. }
  61. protected function organizeCast(array $cast): array
  62. {
  63. $output = [];
  64. foreach ($cast as $id => $role)
  65. {
  66. if (empty($role['attributes']['role']))
  67. {
  68. continue;
  69. }
  70. $roleName = $role['attributes']['role'];
  71. $media = $role['relationships']['media'];
  72. if (array_key_exists('anime', $media))
  73. {
  74. foreach($media['anime'] as $sid => $series)
  75. {
  76. $output[$roleName]['anime'][$sid] = $series;
  77. }
  78. uasort($output[$roleName]['anime'], function ($a, $b) {
  79. return $a['attributes']['canonicalTitle'] <=> $b['attributes']['canonicalTitle'];
  80. });
  81. }
  82. else if (array_key_exists('manga', $media))
  83. {
  84. foreach ($media['manga'] as $sid => $series)
  85. {
  86. $output[$roleName]['manga'][$sid] = $series;
  87. }
  88. uasort($output[$roleName]['anime'], function ($a, $b) {
  89. return $a['attributes']['canonicalTitle'] <=> $b['attributes']['canonicalTitle'];
  90. });
  91. }
  92. }
  93. return $output;
  94. }
  95. }