2017-01-04 13:16:58 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-04 13:16:58 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-01-04 13:16:58 -05:00
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2017-01-04 13:16:58 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-01-06 23:34:56 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2017-01-06 23:34:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-04-10 15:39:39 -04:00
|
|
|
* @version 5
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-11 10:34:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
2017-01-04 13:16:58 -05:00
|
|
|
|
2018-10-29 15:48:54 -04:00
|
|
|
use Aviat\AnimeClient\API\JsonAPI;
|
2018-08-08 13:05:38 -04:00
|
|
|
use Aviat\AnimeClient\Types\MangaPage;
|
2017-01-04 13:16:58 -05:00
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformer for anime description page
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class MangaTransformer extends AbstractTransformer {
|
2017-01-04 13:16:58 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw api response to a more
|
|
|
|
* logical and workable structure
|
|
|
|
*
|
|
|
|
* @param array $item API library item
|
2018-08-08 13:05:38 -04:00
|
|
|
* @return MangaPage
|
2017-01-04 13:16:58 -05:00
|
|
|
*/
|
2018-08-08 13:05:38 -04:00
|
|
|
public function transform($item): MangaPage
|
2017-01-04 13:16:58 -05:00
|
|
|
{
|
2017-01-31 12:52:43 -05:00
|
|
|
$genres = [];
|
2017-04-13 11:44:03 -04:00
|
|
|
|
2018-10-29 15:48:54 -04:00
|
|
|
$item['included'] = JsonAPI::organizeIncluded($item['included']);
|
|
|
|
|
|
|
|
if (array_key_exists('categories', $item['included']))
|
2017-01-31 12:52:43 -05:00
|
|
|
{
|
2018-10-29 15:48:54 -04:00
|
|
|
foreach ($item['included']['categories'] as $cat)
|
2018-10-16 14:32:52 -04:00
|
|
|
{
|
2018-10-29 15:48:54 -04:00
|
|
|
$genres[] = $cat['attributes']['title'];
|
2018-10-16 14:32:52 -04:00
|
|
|
}
|
2018-10-29 15:48:54 -04:00
|
|
|
sort($genres);
|
2017-01-31 12:52:43 -05:00
|
|
|
}
|
2017-04-13 11:44:03 -04:00
|
|
|
|
2018-08-20 13:01:16 -04:00
|
|
|
$title = $item['canonicalTitle'];
|
2018-08-20 13:41:25 -04:00
|
|
|
$rawTitles = array_values($item['titles']);
|
|
|
|
$titles = array_unique(array_diff($rawTitles, [$title]));
|
2018-08-20 13:01:16 -04:00
|
|
|
|
2018-11-08 12:15:30 -05:00
|
|
|
$characters = [];
|
|
|
|
$staff = [];
|
|
|
|
|
|
|
|
if (array_key_exists('mediaCharacters', $item['included']))
|
|
|
|
{
|
|
|
|
$mediaCharacters = $item['included']['mediaCharacters'];
|
|
|
|
|
|
|
|
foreach ($mediaCharacters as $rel)
|
|
|
|
{
|
|
|
|
// dd($rel);
|
|
|
|
// $charId = $rel['relationships']['character']['data']['id'];
|
|
|
|
$role = $rel['attributes']['role'];
|
|
|
|
|
|
|
|
foreach ($rel['relationships']['character']['characters'] as $charId => $char)
|
|
|
|
{
|
|
|
|
if (array_key_exists($charId, $item['included']['characters']))
|
|
|
|
{
|
|
|
|
$characters[$role][$charId] = $char['attributes'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('mediaStaff', $item['included']))
|
|
|
|
{
|
|
|
|
foreach ($item['included']['mediaStaff'] as $id => $staffing)
|
|
|
|
{
|
|
|
|
$role = $staffing['attributes']['role'];
|
|
|
|
|
|
|
|
foreach ($staffing['relationships']['person']['people'] as $personId => $personDetails)
|
|
|
|
{
|
|
|
|
if ( ! array_key_exists($role, $staff))
|
|
|
|
{
|
|
|
|
$staff[$role] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$staff[$role][$personId] = [
|
|
|
|
'id' => $personId,
|
|
|
|
'name' => $personDetails['attributes']['name'] ?? '??',
|
|
|
|
'image' => $personDetails['attributes']['image'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($characters['main']))
|
|
|
|
{
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($characters['main'], static function ($a, $b) {
|
2018-11-08 12:15:30 -05:00
|
|
|
return $a['name'] <=> $b['name'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($characters['supporting']))
|
|
|
|
{
|
2019-12-09 14:34:23 -05:00
|
|
|
uasort($characters['supporting'], static function ($a, $b) {
|
2018-11-08 12:15:30 -05:00
|
|
|
return $a['name'] <=> $b['name'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($characters);
|
|
|
|
ksort($staff);
|
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
return MangaPage::from([
|
2018-11-08 12:15:30 -05:00
|
|
|
'characters' => $characters,
|
2018-10-29 15:48:54 -04:00
|
|
|
'chapter_count' => $this->count($item['chapterCount']),
|
2017-01-04 13:16:58 -05:00
|
|
|
'cover_image' => $item['posterImage']['small'],
|
2018-10-29 15:48:54 -04:00
|
|
|
'genres' => $genres,
|
|
|
|
'id' => $item['id'],
|
|
|
|
'included' => $item['included'],
|
2017-01-04 13:16:58 -05:00
|
|
|
'manga_type' => $item['mangaType'],
|
2018-11-08 12:15:30 -05:00
|
|
|
'staff' => $staff,
|
2017-01-04 13:16:58 -05:00
|
|
|
'synopsis' => $item['synopsis'],
|
2018-10-29 15:48:54 -04:00
|
|
|
'title' => $title,
|
|
|
|
'titles' => $titles,
|
2017-01-04 13:16:58 -05:00
|
|
|
'url' => "https://kitsu.io/manga/{$item['slug']}",
|
2018-10-29 15:48:54 -04:00
|
|
|
'volume_count' => $this->count($item['volumeCount']),
|
2018-08-08 13:05:38 -04:00
|
|
|
]);
|
2017-01-04 13:16:58 -05:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:46:56 -05:00
|
|
|
/**
|
2019-12-09 14:34:23 -05:00
|
|
|
* @param int|null $value
|
|
|
|
* @return string
|
2019-12-06 15:46:56 -05:00
|
|
|
*/
|
2019-12-09 14:34:23 -05:00
|
|
|
private function count(int $value = NULL): string
|
2017-01-04 13:16:58 -05:00
|
|
|
{
|
|
|
|
return ((int)$value === 0)
|
|
|
|
? '-'
|
2019-12-09 14:34:23 -05:00
|
|
|
: (string)$value;
|
2017-01-04 13:16:58 -05:00
|
|
|
}
|
2017-01-03 21:06:49 -05:00
|
|
|
}
|