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
|
|
|
*
|
2018-10-01 11:35:51 -04:00
|
|
|
* PHP version 7.1
|
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>
|
2018-01-15 14:43:15 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2017-01-06 23:34:56 -05: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-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']))
|
|
|
|
{
|
|
|
|
uasort($characters['main'], function ($a, $b) {
|
|
|
|
return $a['name'] <=> $b['name'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($characters['supporting']))
|
|
|
|
{
|
|
|
|
uasort($characters['supporting'], function ($a, $b) {
|
|
|
|
return $a['name'] <=> $b['name'];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($characters);
|
|
|
|
ksort($staff);
|
|
|
|
|
2018-08-08 13:05:38 -04:00
|
|
|
return new MangaPage([
|
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
|
|
|
}
|
|
|
|
|
2017-02-17 11:37:22 -05:00
|
|
|
private function count(int $value = NULL)
|
2017-01-04 13:16:58 -05:00
|
|
|
{
|
|
|
|
return ((int)$value === 0)
|
|
|
|
? '-'
|
|
|
|
: $value;
|
|
|
|
}
|
2017-01-03 21:06:49 -05:00
|
|
|
}
|