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
|
|
|
|
2017-02-15 11:57:29 -05:00
|
|
|
const STATUS_MAP = [
|
2017-02-04 15:18:34 -05:00
|
|
|
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
|
|
|
|
2017-02-14 15:29:13 -05:00
|
|
|
/**
|
|
|
|
* Transform MAL episode data to Kitsu episode data
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
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-15 11:57:29 -05:00
|
|
|
'status' => self::STATUS_MAP[$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
|
|
|
|
*
|
2017-02-15 11:57:29 -05:00
|
|
|
* @param array $item
|
2017-02-04 15:18:34 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function untransform(array $item): array
|
2017-02-13 12:42:05 -05:00
|
|
|
{
|
2017-02-04 15:18:34 -05:00
|
|
|
$map = [
|
|
|
|
'id' => $item['mal_id'],
|
|
|
|
'data' => [
|
2017-02-08 00:44:57 -05:00
|
|
|
'episode' => $item['data']['progress']
|
2017-02-04 15:18:34 -05:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2017-02-13 12:42:05 -05:00
|
|
|
$data =& $item['data'];
|
|
|
|
|
|
|
|
foreach($item['data'] as $key => $value)
|
2017-02-07 09:12:44 -05:00
|
|
|
{
|
2017-02-13 12:42:05 -05:00
|
|
|
switch($key)
|
|
|
|
{
|
|
|
|
case 'notes':
|
|
|
|
$map['data']['comments'] = $value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rating':
|
|
|
|
$map['data']['score'] = $value * 2;
|
|
|
|
break;
|
|
|
|
|
2017-02-13 13:33:01 -05:00
|
|
|
case 'reconsuming':
|
2017-02-13 12:42:05 -05:00
|
|
|
$map['data']['enable_rewatching'] = (bool) $value;
|
2017-02-08 00:44:57 -05:00
|
|
|
break;
|
2017-02-13 12:42:05 -05:00
|
|
|
|
|
|
|
case 'reconsumeCount':
|
|
|
|
$map['data']['times_rewatched'] = $value;
|
2017-02-13 13:33:01 -05:00
|
|
|
break;
|
2017-02-13 12:42:05 -05:00
|
|
|
|
|
|
|
case 'status':
|
2017-02-15 11:57:29 -05:00
|
|
|
$map['data']['status'] = self::STATUS_MAP[$value];
|
2017-02-13 12:42:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-02-07 09:12:44 -05:00
|
|
|
}
|
2017-02-13 12:42:05 -05:00
|
|
|
|
2017-02-04 15:18:34 -05:00
|
|
|
return $map;
|
|
|
|
}
|
2017-01-12 15:41:20 -05:00
|
|
|
}
|