2018-08-10 20:10:19 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2018-08-10 20:10:19 -04:00
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
2018-08-10 20:10:19 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2018-08-10 20:10:19 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-08-04 09:30:21 -04:00
|
|
|
* @version 5.1
|
2018-08-10 20:10:19 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Anilist;
|
|
|
|
|
2020-03-11 16:26:17 -04:00
|
|
|
use Amp\Http\Client\Request;
|
2018-09-20 10:41:28 -04:00
|
|
|
|
2020-01-15 12:35:37 -05:00
|
|
|
use Aviat\AnimeClient\API\AbstractListItem;
|
2018-09-26 22:31:04 -04:00
|
|
|
use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Anilist as AnilistStatus;
|
2018-09-20 10:41:28 -04:00
|
|
|
use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
|
|
|
|
use Aviat\AnimeClient\Types\FormItemData;
|
2018-08-10 20:10:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CRUD operations for MAL list items
|
|
|
|
*/
|
2020-01-15 12:35:37 -05:00
|
|
|
final class ListItem extends AbstractListItem {
|
2018-08-10 20:10:19 -04:00
|
|
|
use AnilistTrait;
|
|
|
|
|
|
|
|
/**
|
2018-09-26 22:31:04 -04:00
|
|
|
* Create a minimal list item
|
2018-08-10 20:10:19 -04:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Request
|
|
|
|
*/
|
2018-09-20 10:41:28 -04:00
|
|
|
public function create(array $data): Request
|
2018-08-10 20:10:19 -04:00
|
|
|
{
|
2020-04-22 07:53:52 -04:00
|
|
|
$checkedData = Types\MediaListEntry::check($data);
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->mutateRequest('CreateMediaListEntry', $checkedData);
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 22:31:04 -04:00
|
|
|
/**
|
|
|
|
* Create a fleshed-out list item
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Request
|
|
|
|
*/
|
|
|
|
public function createFull(array $data): Request
|
|
|
|
{
|
2020-04-22 07:53:52 -04:00
|
|
|
$checkedData = Types\MediaListEntry::check($data);
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->mutateRequest('CreateFullMediaListEntry', $checkedData);
|
2018-09-26 22:31:04 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 20:10:19 -04:00
|
|
|
/**
|
|
|
|
* Delete a list item
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param string $type
|
|
|
|
* @return Request
|
|
|
|
*/
|
|
|
|
public function delete(string $id, string $type = 'anime'): Request
|
|
|
|
{
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->mutateRequest('DeleteMediaListEntry', ['id' => $id]);
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:41:28 -04:00
|
|
|
/**
|
|
|
|
* Get the data for a list item
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-08-10 20:10:19 -04:00
|
|
|
public function get(string $id): array
|
|
|
|
{
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->runQuery('MediaListItem', ['id' => $id]);
|
2018-09-20 10:41:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase the progress on the medium by 1
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param FormItemData $data
|
|
|
|
* @return Request
|
|
|
|
*/
|
|
|
|
public function increment(string $id, FormItemData $data): Request
|
|
|
|
{
|
2020-04-22 07:53:52 -04:00
|
|
|
$checkedData = Types\MediaListEntry::check([
|
2018-09-20 10:41:28 -04:00
|
|
|
'id' => $id,
|
2020-01-15 12:35:37 -05:00
|
|
|
'progress' => $data->progress,
|
2020-04-22 07:53:52 -04:00
|
|
|
]);
|
2020-01-15 12:35:37 -05:00
|
|
|
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->mutateRequest('IncrementMediaListEntry', $checkedData);
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a list item
|
|
|
|
*
|
|
|
|
* @param string $id
|
2018-09-20 10:41:28 -04:00
|
|
|
* @param FormItemData $data
|
2018-08-10 20:10:19 -04:00
|
|
|
* @return Request
|
|
|
|
*/
|
2018-09-20 10:41:28 -04:00
|
|
|
public function update(string $id, FormItemData $data): Request
|
2018-10-01 13:03:48 -04:00
|
|
|
{
|
2020-01-15 12:35:37 -05:00
|
|
|
$notes = $data->notes ?? '';
|
|
|
|
$progress = (int)$data->progress;
|
|
|
|
$private = (bool)$data->private;
|
|
|
|
$rating = $data->ratingTwenty;
|
|
|
|
$status = ($data->reconsuming === TRUE)
|
|
|
|
? AnilistStatus::REPEATING
|
|
|
|
: AnimeWatchingStatus::KITSU_TO_ANILIST[$data->status];
|
2018-10-01 13:03:48 -04:00
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
$updateData = Types\MediaListEntry::check([
|
2018-09-26 22:31:04 -04:00
|
|
|
'id' => (int)$id,
|
|
|
|
'status' => $status,
|
2018-10-01 13:03:48 -04:00
|
|
|
'score' => $rating * 5,
|
2018-09-20 16:08:46 -04:00
|
|
|
'progress' => $progress,
|
2018-09-20 10:41:28 -04:00
|
|
|
'repeat' => (int)$data['reconsumeCount'],
|
2018-09-26 22:31:04 -04:00
|
|
|
'private' => $private,
|
2018-09-20 16:08:46 -04:00
|
|
|
'notes' => $notes,
|
2020-04-22 07:53:52 -04:00
|
|
|
]);
|
2018-09-26 22:31:04 -04:00
|
|
|
|
2020-08-05 20:57:01 -04:00
|
|
|
return $this->requestBuilder->mutateRequest('UpdateMediaListEntry', $updateData);
|
2018-08-10 20:10:19 -04:00
|
|
|
}
|
|
|
|
}
|