|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Anime List Client |
|
4
|
|
|
* |
|
5
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @package AnimeListClient |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @version 4.0 |
|
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer; |
|
18
|
|
|
|
|
19
|
|
|
use Aviat\AnimeClient\API\Kitsu; |
|
20
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Transformer for anime list |
|
24
|
|
|
*/ |
|
25
|
|
|
class AnimeListTransformer extends AbstractTransformer { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Convert raw api response to a more |
|
29
|
|
|
* logical and workable structure |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $item API library item |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function transform($item) |
|
35
|
|
|
{ |
|
36
|
|
|
/* ?><pre><?= json_encode($item, \JSON_PRETTY_PRINT) ?></pre><?php */ |
|
37
|
|
|
$included = $item['included']; |
|
38
|
|
|
$animeId = $item['relationships']['media']['data']['id']; |
|
39
|
|
|
$anime = $included['anime'][$animeId]; |
|
40
|
|
|
|
|
41
|
|
|
$genres = array_column($anime['relationships']['genres'], 'name') ?? []; |
|
42
|
|
|
sort($genres); |
|
43
|
|
|
|
|
44
|
|
|
$rating = (int) 2 * $item['attributes']['rating']; |
|
45
|
|
|
|
|
46
|
|
|
$total_episodes = array_key_exists('episodeCount', $anime) && (int) $anime['episodeCount'] !== 0 |
|
47
|
|
|
? (int) $anime['episodeCount'] |
|
48
|
|
|
: '-'; |
|
49
|
|
|
|
|
50
|
|
|
$MALid = NULL; |
|
51
|
|
|
|
|
52
|
|
|
if (array_key_exists('mappings', $anime['relationships'])) |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($anime['relationships']['mappings'] as $mapping) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($mapping['externalSite'] === 'myanimelist/anime') |
|
57
|
|
|
{ |
|
58
|
|
|
$MALid = $mapping['externalId']; |
|
59
|
|
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$streamingLinks = (array_key_exists('streamingLinks', $anime['relationships'])) |
|
65
|
|
|
? Kitsu::parseListItemStreamingLinks($included, $animeId) |
|
66
|
|
|
: []; |
|
67
|
|
|
|
|
68
|
|
|
return [ |
|
69
|
|
|
'id' => $item['id'], |
|
70
|
|
|
'mal_id' => $MALid, |
|
71
|
|
|
'episodes' => [ |
|
72
|
|
|
'watched' => (int) $item['attributes']['progress'] !== '0' |
|
|
|
|
|
|
73
|
|
|
? (int) $item['attributes']['progress'] |
|
74
|
|
|
: '-', |
|
75
|
|
|
'total' => $total_episodes, |
|
76
|
|
|
'length' => $anime['episodeLength'], |
|
77
|
|
|
], |
|
78
|
|
|
'airing' => [ |
|
79
|
|
|
'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']), |
|
80
|
|
|
'started' => $anime['startDate'], |
|
81
|
|
|
'ended' => $anime['endDate'] |
|
82
|
|
|
], |
|
83
|
|
|
'anime' => [ |
|
84
|
|
|
'age_rating' => $anime['ageRating'], |
|
85
|
|
|
'title' => $anime['canonicalTitle'], |
|
86
|
|
|
'titles' => Kitsu::filterTitles($anime), |
|
87
|
|
|
'slug' => $anime['slug'], |
|
88
|
|
|
'type' => $this->string($anime['showType'])->upperCaseFirst()->__toString(), |
|
89
|
|
|
'image' => $anime['posterImage']['small'], |
|
90
|
|
|
'genres' => $genres, |
|
91
|
|
|
'streaming_links' => $streamingLinks, |
|
92
|
|
|
], |
|
93
|
|
|
'watching_status' => $item['attributes']['status'], |
|
94
|
|
|
'notes' => $item['attributes']['notes'], |
|
95
|
|
|
'rewatching' => (bool) $item['attributes']['reconsuming'], |
|
96
|
|
|
'rewatched' => (int) $item['attributes']['reconsumeCount'], |
|
97
|
|
|
'user_rating' => ($rating === 0) ? '-' : (int) $rating, |
|
98
|
|
|
'private' => (bool) $item['attributes']['private'] ?? false, |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Convert transformed data to |
|
104
|
|
|
* api response format |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $item Transformed library item |
|
107
|
|
|
* @return array API library item |
|
108
|
|
|
*/ |
|
109
|
|
|
public function untransform($item) |
|
110
|
|
|
{ |
|
111
|
|
|
$privacy = (array_key_exists('private', $item) && $item['private']); |
|
112
|
|
|
$rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']); |
|
113
|
|
|
|
|
114
|
|
|
$untransformed = [ |
|
115
|
|
|
'id' => $item['id'], |
|
116
|
|
|
'mal_id' => $item['mal_id'] ?? null, |
|
117
|
|
|
'data' => [ |
|
118
|
|
|
'status' => $item['watching_status'], |
|
119
|
|
|
'reconsuming' => $rewatching, |
|
120
|
|
|
'reconsumeCount' => $item['rewatched'], |
|
121
|
|
|
'notes' => $item['notes'], |
|
122
|
|
|
'progress' => $item['episodes_watched'], |
|
123
|
|
|
'private' => $privacy |
|
124
|
|
|
] |
|
125
|
|
|
]; |
|
126
|
|
|
|
|
127
|
|
|
if ( ! empty($item['user_rating'])) |
|
128
|
|
|
{ |
|
129
|
|
|
$untransformed['data']['rating'] = $item['user_rating'] / 2; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $untransformed; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
// End of AnimeListTransformer.php |