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

100 lines
2.2 KiB
PHP
Raw Normal View History

2017-01-12 15:41:20 -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 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
2017-02-04 15:18:34 -05:00
namespace Aviat\AnimeClient\API\MAL\Transformer;
2017-01-12 15:41:20 -05:00
2017-02-04 15:18:34 -05:00
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
2017-01-12 15:41:20 -05:00
use Aviat\Ion\Transformer\AbstractTransformer;
/**
* Transformer for updating MAL List
*/
class AnimeListTransformer extends AbstractTransformer {
2017-02-04 15:18:34 -05:00
const statusMap = [
AnimeWatchingStatus::WATCHING => '1',
AnimeWatchingStatus::COMPLETED => '2',
AnimeWatchingStatus::ON_HOLD => '3',
AnimeWatchingStatus::DROPPED => '4',
AnimeWatchingStatus::PLAN_TO_WATCH => '6'
];
2017-01-12 15:41:20 -05:00
public function transform($item)
{
2017-02-04 15:18:34 -05:00
$rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']);
2017-01-12 15:41:20 -05:00
return [
2017-02-04 15:18:34 -05:00
'id' => $item['mal_id'],
2017-01-12 15:41:20 -05:00
'data' => [
2017-02-04 15:18:34 -05:00
'status' => self::statusMap[$item['watching_status']],
2017-01-12 15:41:20 -05:00
'rating' => $item['user_rating'],
'rewatch_value' => (int) $rewatching,
'times_rewatched' => $item['rewatched'],
'comments' => $item['notes'],
'episode' => $item['episodes_watched']
]
];
}
2017-02-04 15:18:34 -05:00
/**
* Transform Kitsu episode data to MAL episode data
*
* @param array $item
* @return array
*/
public function untransform(array $item): array
{
2017-02-04 15:18:34 -05:00
$map = [
'id' => $item['mal_id'],
'data' => [
'episode' => $item['data']['progress']
2017-02-04 15:18:34 -05:00
]
];
$data =& $item['data'];
foreach($item['data'] as $key => $value)
{
switch($key)
{
case 'notes':
$map['data']['comments'] = $value;
break;
case 'rating':
$map['data']['score'] = $value * 2;
break;
/* case 'reconsuming':
$map['data']['enable_rewatching'] = (bool) $value;
break;
case 'reconsumeCount':
$map['data']['times_rewatched'] = $value;
break; */
case 'status':
$map['data']['status'] = self::statusMap[$value];
break;
default:
break;
}
}
2017-02-04 15:18:34 -05:00
return $map;
}
2017-01-12 15:41:20 -05:00
}