HummingBirdAnimeClient/src/Aviat/AnimeClient/Transformer/Hummingbird/AnimeListTransformer.php

98 lines
2.3 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
namespace Aviat\AnimeClient\Transformer\Hummingbird;
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 [
'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,
],
'id' => $item['id'],
'watching_status' => $item['status'],
'notes' => $item['notes'],
'rewatching' => (bool) $item['rewatching'],
'rewatched' => $item['rewatched_times'],
'user_rating' => $rating,
];
}
/**
* Simplify structure of genre list
*
* @param array $raw_genres
* @return array
*/
protected function linearize_genres(array $raw_genres)
{
$genres = [];
foreach($raw_genres as $genre)
{
$genres[] = $genre['name'];
}
return $genres;
2015-09-17 23:11:18 -04:00
}
}
// End of AnimeListTransformer.php