HummingBirdAnimeClient/src/API/MAL/Model.php

182 lines
3.8 KiB
PHP
Raw Normal View History

2017-02-01 09:53:02 -05:00
<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
2017-02-01 09:53:02 -05:00
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2017-02-01 09:53:02 -05:00
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 Timothy J. Warren
2017-02-01 09:53:02 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
2017-02-01 09:53:02 -05:00
*/
namespace Aviat\AnimeClient\API\MAL;
use Amp\Artax\Request;
use Aviat\AnimeClient\API\MAL\{
ListItem,
Transformer\AnimeListTransformer,
Transformer\MangaListTransformer
};
2017-02-01 09:53:02 -05:00
use Aviat\AnimeClient\API\XML;
use Aviat\AnimeClient\API\Mapping\{AnimeWatchingStatus, MangaReadingStatus};
use Aviat\AnimeClient\Types\{Anime, AnimeFormItem};
2017-02-01 09:53:02 -05:00
use Aviat\Ion\Di\ContainerAware;
/**
* MyAnimeList API Model
*/
final class Model {
2017-02-01 09:53:02 -05:00
use ContainerAware;
use MALTrait;
/**
* @var AnimeListTransformer
*/
protected $animeListTransformer;
2018-01-16 14:58:07 -05:00
/**
* @var MangaListTransformer
*/
protected $mangaListTransformer;
2017-02-22 14:46:35 -05:00
/**
* @var ListItem
*/
protected $listItem;
2017-02-01 09:53:02 -05:00
/**
* MAL Model constructor.
2017-02-17 10:55:17 -05:00
*
* @param ListItem $listItem
2017-02-01 09:53:02 -05:00
*/
public function __construct(ListItem $listItem)
{
2017-02-04 15:18:34 -05:00
$this->animeListTransformer = new AnimeListTransformer();
$this->mangaListTransformer = new MangaListTransformer();
2017-02-01 09:53:02 -05:00
$this->listItem = $listItem;
}
/**
* Create a list item on MAL
*
* @param array $data
* @param string $type "anime" or "manga"
* @return Request
*/
public function createFullListItem(array $data, string $type = 'anime'): Request
{
return $this->listItem->create($data, $type);
}
2017-02-01 09:53:02 -05:00
2017-06-19 13:49:28 -04:00
/**
* Create a list item on MAL from a Kitsu list item
*
* @param array $data
* @param string $type "anime" or "manga"
* @return Request
*/
public function createListItem(array $data, string $type = 'anime'): Request
2017-02-01 09:53:02 -05:00
{
2018-01-16 14:58:07 -05:00
$createData = [];
if ($type === 'anime')
{
$createData = [
'id' => $data['id'],
'data' => [
'status' => AnimeWatchingStatus::KITSU_TO_MAL[$data['status']]
]
];
}
elseif ($type === 'manga')
{
$createData = [
'id' => $data['id'],
'data' => [
'status' => MangaReadingStatus::KITSU_TO_MAL[$data['status']]
]
];
}
return $this->listItem->create($createData, $type);
2017-02-04 15:18:34 -05:00
}
2017-06-19 13:49:28 -04:00
/**
* Get list info
*
* @param string $type "anime" or "manga"
* @return array
*/
public function getList(string $type = "anime"): array
2017-02-04 15:18:34 -05:00
{
2017-06-19 13:49:28 -04:00
$config = $this->container->get('config');
$userName = $config->get(['mal', 'username']);
$list = $this->getRequest('https://myanimelist.net/malappinfo.php', [
'headers' => [
'Accept' => 'text/xml'
],
'query' => [
'u' => $userName,
'status' => 'all',
'type' => $type
]
]);
2017-02-04 15:18:34 -05:00
2018-01-16 14:58:07 -05:00
return array_key_exists($type, $list['myanimelist'])
? $list['myanimelist'][$type]
: [];
2017-02-01 09:53:02 -05:00
}
2017-06-19 13:49:28 -04:00
/**
* Retrieve a list item
*
* Does not apply to MAL
*
* @param string $listId
* @return array
*/
2017-02-01 09:53:02 -05:00
public function getListItem(string $listId): array
{
return [];
}
2017-06-19 13:49:28 -04:00
/**
* Update a list item
*
* @param AnimeFormItem $data
2017-06-19 13:49:28 -04:00
* @param string $type "anime" or "manga"
* @return Request
*/
public function updateListItem(AnimeFormItem $data, string $type = 'anime'): Request
2017-02-01 09:53:02 -05:00
{
2018-01-16 14:58:07 -05:00
$updateData = [];
if ($type === 'anime')
{
$updateData = $this->animeListTransformer->untransform($data);
}
else if ($type === 'manga')
{
$updateData = $this->mangaListTransformer->untransform($data);
}
return $this->listItem->update($updateData['id'], $updateData['data'], $type);
2017-02-01 09:53:02 -05:00
}
2017-06-19 13:49:28 -04:00
/**
* Delete a list item
*
* @param string $id
* @param string $type "anime" or "manga"
* @return Request
*/
public function deleteListItem(string $id, string $type = 'anime'): Request
2017-02-01 09:53:02 -05:00
{
return $this->listItem->delete($id, $type);
2017-02-01 09:53:02 -05:00
}
2016-12-20 12:55:43 -05:00
}