HummingBirdAnimeClient/src/API/Kitsu/Transformer/MangaTransformer.php

69 lines
1.7 KiB
PHP
Raw Normal View History

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
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
2017-01-04 13:16:58 -05:00
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
*/
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 = [];
foreach($included['categories'] as $cat)
2017-01-31 12:52:43 -05:00
{
$genres[] = $cat['title'];
2017-01-31 12:52:43 -05:00
}
2017-01-31 12:52:43 -05:00
sort($genres);
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-08-08 13:05:38 -04:00
return new MangaPage([
'id' => $item['id'],
2018-08-20 13:01:16 -04:00
'title' => $title,
'titles' => $titles,
2017-01-04 13:16:58 -05:00
'cover_image' => $item['posterImage']['small'],
'manga_type' => $item['mangaType'],
'chapter_count' => $this->count($item['chapterCount']),
'volume_count' => $this->count($item['volumeCount']),
'synopsis' => $item['synopsis'],
'url' => "https://kitsu.io/manga/{$item['slug']}",
2017-01-31 12:52:43 -05:00
'genres' => $genres,
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
}