HummingBirdAnimeClient/src/API/MAL/Model.php

170 lines
3.6 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>
* @copyright 2015 - 2017 Timothy J. Warren
* @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};
2017-02-01 09:53:02 -05:00
use Aviat\Ion\Di\ContainerAware;
/**
* MyAnimeList API Model
*/
class Model {
use ContainerAware;
use MALTrait;
/**
* @var AnimeListTransformer
*/
protected $animeListTransformer;
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
{
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): 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
2017-06-19 13:49:28 -04:00
return $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 array $data
* @param string $type "anime" or "manga"
* @return Request
*/
public function updateListItem(array $data, string $type = 'anime'): Request
2017-02-01 09:53:02 -05:00
{
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
}