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

145 lines
3.7 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;
use Aviat\Ion\Transformer\AbstractTransformer;
/**
* Transformer for anime list
*/
class AnimeListTransformer extends AbstractTransformer {
/**
* Convert raw api response to a more
* logical and workable structure
*
* @param array $item API library item
* @return array
*/
public function transform($item)
{
2016-12-21 12:46:20 -05:00
/* ?><pre><?= print_r($item, TRUE) ?></pre><?php
die(); */
// $anime = [];
$genres = [];
2016-12-20 12:55:43 -05:00
$anime =& $item['anime'];
2016-12-21 12:46:20 -05:00
// $genres = $this->linearizeGenres($item['anime']['genres']); */
2016-12-20 12:55:43 -05:00
2016-12-21 12:46:20 -05:00
$rating = (int) 2 * $item['attributes']['rating'];
2016-12-20 12:55:43 -05:00
2016-12-21 12:46:20 -05:00
$total_episodes = is_numeric($anime['episodeCount'])
? $anime['episodeCount']
2016-12-20 12:55:43 -05:00
: '-';
$alternate_title = NULL;
2016-12-21 12:46:20 -05:00
if (array_key_exists('en_jp', $anime['titles']))
2016-12-20 12:55:43 -05:00
{
// If the alternate title is very similar, or
// a subset of the main title, don't list the
// alternate title
2016-12-21 12:46:20 -05:00
$not_subset = stripos($anime['canonicalTitle'], $anime['titles']['en_jp']) === FALSE;
$diff = levenshtein($anime['canonicalTitle'], $anime['titles']['en_jp'] ?? '');
2016-12-20 12:55:43 -05:00
if ($not_subset && $diff >= 5)
{
2016-12-21 12:46:20 -05:00
$alternate_title = $anime['titles']['en_jp'];
2016-12-20 12:55:43 -05:00
}
}
return [
'id' => $item['id'],
'episodes' => [
2016-12-21 12:46:20 -05:00
'watched' => $item['attributes']['progress'],
2016-12-20 12:55:43 -05:00
'total' => $total_episodes,
2016-12-21 12:46:20 -05:00
'length' => $anime['episodeLength'],
2016-12-20 12:55:43 -05:00
],
'airing' => [
2016-12-21 12:46:20 -05:00
'status' => $anime['status'] ?? '',
'started' => $anime['startDate'],
'ended' => $anime['endDate']
2016-12-20 12:55:43 -05:00
],
'anime' => [
2016-12-21 12:46:20 -05:00
'age_rating' => $anime['ageRating'],
'title' => $anime['canonicalTitle'],
2016-12-20 12:55:43 -05:00
'alternate_title' => $alternate_title,
'slug' => $anime['slug'],
2016-12-21 12:46:20 -05:00
'url' => $anime['url'] ?? '',
'type' => $anime['showType'],
'image' => $anime['posterImage']['small'],
'genres' => [],//$genres,
2016-12-20 12:55:43 -05:00
],
2016-12-21 12:46:20 -05:00
'watching_status' => $item['attributes']['status'],
'notes' => $item['attributes']['notes'],
'rewatching' => (bool) $item['attributes']['reconsuming'],
'rewatched' => (int) $item['attributes']['reconsumeCount'],
2016-12-20 12:55:43 -05:00
'user_rating' => $rating,
2016-12-21 12:46:20 -05:00
'private' => (bool) $item['attributes']['private'] ?? false,
2016-12-20 12:55:43 -05:00
];
}
/**
* Convert transformed data to
* api response format
*
* @param array $item Transformed library item
* @return array API library item
*/
public function untransform($item)
{
// Messy mapping of boolean values to their API string equivalents
$privacy = 'public';
if (array_key_exists('private', $item) && $item['private'])
{
$privacy = 'private';
}
$rewatching = 'false';
if (array_key_exists('rewatching', $item) && $item['rewatching'])
{
$rewatching = 'true';
}
return [
'id' => $item['id'],
'status' => $item['watching_status'],
'sane_rating_update' => $item['user_rating'] / 2,
'rewatching' => $rewatching,
'rewatched_times' => $item['rewatched'],
'notes' => $item['notes'],
'episodes_watched' => $item['episodes_watched'],
'privacy' => $privacy
];
}
/**
* Simplify structure of genre list
*
* @param array $rawGenres
* @return array
*/
protected function linearizeGenres(array $rawGenres): array
{
$genres = [];
foreach ($rawGenres as $genre)
{
$genres[] = $genre['name'];
}
return $genres;
}
}
// End of AnimeListTransformer.php