HummingBirdAnimeClient/src/Hummingbird/Transformer/AnimeListTransformer.php

147 lines
3.6 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
2015-11-16 11:40:01 -05:00
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
2016-08-30 10:01:18 -04:00
* PHP version 5.6
*
2015-11-16 11:40:01 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @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 3.1
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
2015-09-17 23:11:18 -04:00
namespace Aviat\AnimeClient\Hummingbird\Transformer;
2015-09-17 23:11:18 -04:00
2015-09-25 13:41:12 -04:00
use Aviat\Ion\Transformer\AbstractTransformer;
2015-09-17 23:11:18 -04:00
2015-09-25 13:41:12 -04:00
/**
* Transformer for anime list
*/
class AnimeListTransformer extends AbstractTransformer {
2015-09-17 23:11:18 -04:00
2015-09-25 13:41:12 -04:00
/**
* Convert raw api response to a more
* logical and workable structure
*
* @param array $item API library item
* @return array
*/
2015-09-17 23:11:18 -04:00
public function transform($item)
{
2015-09-25 13:41:12 -04:00
$anime =& $item['anime'];
$genres = $this->linearize_genres($item['anime']['genres']);
2015-09-17 23:11:18 -04:00
2015-09-25 13:41:12 -04:00
$rating = NULL;
if ($item['rating']['type'] === 'advanced')
{
$rating = (is_numeric($item['rating']['value']))
2015-09-28 15:11:45 -04:00
? intval(2 * $item['rating']['value'])
2015-09-25 13:41:12 -04:00
: '-';
}
2015-10-01 16:02:51 -04:00
$total_episodes = (is_numeric($anime['episode_count']))
? $anime['episode_count']
: '-';
2015-09-25 13:41:12 -04:00
$alternate_title = NULL;
if (array_key_exists('alternate_title', $anime))
{
// If the alternate title is very similar, or
// a subset of the main title, don't list the
// alternate title
$not_subset = stripos($anime['title'], $anime['alternate_title']) === FALSE;
$diff = levenshtein($anime['title'], $anime['alternate_title']);
if ($not_subset && $diff >= 5)
{
$alternate_title = $anime['alternate_title'];
}
}
return [
'id' => $item['id'],
2015-09-25 13:41:12 -04:00
'episodes' => [
'watched' => $item['episodes_watched'],
2015-10-01 16:02:51 -04:00
'total' => $total_episodes,
2015-09-25 13:41:12 -04:00
'length' => $anime['episode_length'],
],
'airing' => [
'status' => $anime['status'],
'started' => $anime['started_airing'],
'ended' => $anime['finished_airing']
],
'anime' => [
2015-09-28 15:11:45 -04:00
'age_rating' => $anime['age_rating'],
2015-09-25 13:41:12 -04:00
'title' => $anime['title'],
'alternate_title' => $alternate_title,
'slug' => $anime['slug'],
'url' => $anime['url'],
'type' => $anime['show_type'],
'image' => $anime['cover_image'],
'genres' => $genres,
],
'watching_status' => $item['status'],
'notes' => $item['notes'],
2016-01-04 10:53:03 -05:00
'rewatching' => (bool) $item['rewatching'],
2015-09-25 13:41:12 -04:00
'rewatched' => $item['rewatched_times'],
'user_rating' => $rating,
2016-01-04 10:53:03 -05:00
'private' => (bool) $item['private'],
];
}
/**
* 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';
2016-01-06 11:08:56 -05:00
if (array_key_exists('private', $item) && $item['private'])
2016-01-04 10:53:03 -05:00
{
$privacy = 'private';
}
$rewatching = 'false';
2016-01-06 11:08:56 -05:00
if (array_key_exists('rewatching', $item) && $item['rewatching'])
2016-01-04 10:53:03 -05:00
{
$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
2015-09-25 13:41:12 -04:00
];
}
/**
* Simplify structure of genre list
*
* @param array $raw_genres
* @return array
*/
protected function linearize_genres(array $raw_genres)
{
$genres = [];
foreach ($raw_genres as $genre)
2015-09-25 13:41:12 -04:00
{
$genres[] = $genre['name'];
}
return $genres;
2015-09-17 23:11:18 -04:00
}
}
// End of AnimeListTransformer.php