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

124 lines
3.0 KiB
PHP
Raw Normal View History

2016-12-20 12:55:43 -05:00
<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
2017-01-03 21:06:49 -05:00
use Aviat\Ion\StringWrapper;
2016-12-20 12:55:43 -05:00
use Aviat\Ion\Transformer\AbstractTransformer;
/**
* Data transformation class for zippered Hummingbird manga
*/
class MangaListTransformer extends AbstractTransformer {
2017-01-03 21:06:49 -05:00
use StringWrapper;
2016-12-20 12:55:43 -05:00
/**
* Remap zipped anime data to a more logical form
*
* @param array $item manga entry item
* @return array
*/
public function transform($item)
{
2017-01-03 21:06:49 -05:00
?><pre><?= print_r($item, TRUE) ?></pre><?php
die();
2016-12-20 12:55:43 -05:00
$manga =& $item['manga'];
$rating = (is_numeric($item['rating']))
? intval(2 * $item['rating'])
: '-';
2017-01-03 21:06:49 -05:00
$total_chapters = ($manga['attributes']['chapterCount'] > 0)
? $manga['attributes']['chapterCount']
: '-';
2016-12-20 12:55:43 -05:00
2017-01-03 21:06:49 -05:00
$total_volumes = ($manga['attributes']['volumeCount'] > 0)
? $manga['attributes']['volumeCount']
2016-12-20 12:55:43 -05:00
: '-';
$map = [
'id' => $item['id'],
'chapters' => [
'read' => $item['chapters_read'],
'total' => $total_chapters
],
'volumes' => [
'read' => $item['volumes_read'],
'total' => $total_volumes
],
'manga' => [
'title' => $manga['romaji_title'],
'alternate_title' => NULL,
'slug' => $manga['id'],
'url' => 'https://hummingbird.me/manga/' . $manga['id'],
'type' => $manga['manga_type'],
'image' => $manga['poster_image_thumb'],
'genres' => $manga['genres'],
],
'reading_status' => $item['status'],
'notes' => $item['notes'],
'rereading' => (bool)$item['rereading'],
'reread' => $item['reread_count'],
'user_rating' => $rating,
];
if (array_key_exists('english_title', $manga))
{
$diff = levenshtein($manga['romaji_title'], $manga['english_title']);
// If the titles are REALLY similar, don't bother showing both
if ($diff >= 5)
{
$map['manga']['alternate_title'] = $manga['english_title'];
}
}
return $map;
}
/**
* Untransform data to update the api
*
* @param array $item
* @return array
*/
public function untransform($item)
{
$rereading = (array_key_exists('rereading', $item)) && (bool)$item['rereading'];
$map = [
'id' => $item['id'],
'manga_id' => $item['manga_id'],
'status' => $item['status'],
'chapters_read' => (int)$item['chapters_read'],
'volumes_read' => (int)$item['volumes_read'],
'rereading' => $rereading,
'reread_count' => (int)$item['reread_count'],
'notes' => $item['notes'],
];
if ($item['new_rating'] !== $item['old_rating'] && $item['new_rating'] !== "")
{
$map['rating'] = ($item['new_rating'] > 0)
? $item['new_rating'] / 2
: $item['old_rating'] / 2;
}
return $map;
}
}
// End of MangaListTransformer.php