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

78 lines
2.1 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
{
$rewatching = (array_key_exists('reconsuming', $item['data']) && $item['data']['reconsuming']);
$map = [
'id' => $item['mal_id'],
'data' => [
'episode' => $item['data']['progress'],
'status' => self::statusMap[$item['data']['status']],
'score' => (array_key_exists('rating', $item['data']))
? $item['data']['rating'] * 2
: "",
// 'enable_rewatching' => $rewatching,
// 'times_rewatched' => $item['data']['reconsumeCount'],
// 'comments' => $item['data']['notes'],
]
];
return $map;
}
2017-01-12 15:41:20 -05:00
}