HummingBirdAnimeClient/src/API/Anilist/ListItem.php

121 lines
2.9 KiB
PHP
Raw Normal View History

<?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-10-01 11:35:51 -04:00
* PHP version 7.1
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2018-10-01 11:35:51 -04:00
* @version 4.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Anilist;
use Amp\Artax\Request;
use Aviat\AnimeClient\API\ListItemInterface;
2018-09-26 22:31:04 -04:00
use Aviat\AnimeClient\API\Enum\AnimeWatchingStatus\Anilist as AnilistStatus;
use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
use Aviat\AnimeClient\Types\FormItemData;
/**
* CRUD operations for MAL list items
*/
final class ListItem implements ListItemInterface{
use AnilistTrait;
/**
2018-09-26 22:31:04 -04:00
* Create a minimal list item
*
* @param array $data
* @return Request
*/
public function create(array $data): Request
{
return $this->mutateRequest('CreateMediaListEntry', $data);
}
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
{
return $this->mutateRequest('CreateFullMediaListEntry', $data);
}
/**
* Delete a list item
*
* @param string $id
* @param string $type
* @return Request
*/
public function delete(string $id, string $type = 'anime'): Request
{
return $this->mutateRequest('DeleteMediaListEntry', ['id' => $id]);
}
/**
* Get the data for a list item
*
* @param string $id
* @return array
*/
public function get(string $id): array
{
return $this->runQuery('MediaListItem', ['id' => $id]);
}
/**
* Increase the progress on the medium by 1
*
* @param string $id
* @param FormItemData $data
* @return Request
*/
public function increment(string $id, FormItemData $data): Request
{
return $this->mutateRequest('IncrementMediaListEntry', [
'id' => $id,
'progress' => $data['progress'],
]);
}
/**
* Update a list item
*
* @param string $id
* @param FormItemData $data
* @return Request
*/
public function update(string $id, FormItemData $data): Request
2018-09-26 22:31:04 -04:00
{
$array = $data->toArray();
$notes = $data['notes'] ?? '';
$progress = array_key_exists('progress', $array) ? $data['progress'] : 0;
2018-09-26 22:31:04 -04:00
$private = array_key_exists('private', $array) ? (bool)$data['private'] : false;
$rating = array_key_exists('rating', $array) ? $data['rating'] : NULL;
2018-09-26 22:31:04 -04:00
$status = ($data['reconsuming'] === true) ? AnilistStatus::REPEATING : AnimeWatchingStatus::KITSU_TO_ANILIST[$data['status']];
$updateData = [
'id' => (int)$id,
'status' => $status,
'score' => $rating * 10,
'progress' => $progress,
'repeat' => (int)$data['reconsumeCount'],
2018-09-26 22:31:04 -04:00
'private' => $private,
'notes' => $notes,
2018-09-26 22:31:04 -04:00
];
return $this->mutateRequest('UpdateMediaListEntry', $updateData);
}
}