1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Hummingbird Anime Client |
4
|
|
|
* |
5
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* @package HummingbirdAnimeClient |
8
|
|
|
* @author Timothy J. Warren |
9
|
|
|
* @copyright Copyright (c) 2015 |
10
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Aviat\AnimeClient\Hummingbird\Transformer; |
15
|
|
|
|
16
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Transformer for anime list |
20
|
|
|
*/ |
21
|
|
|
class AnimeListTransformer extends AbstractTransformer { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Convert raw api response to a more |
25
|
|
|
* logical and workable structure |
26
|
|
|
* |
27
|
|
|
* @param array $item API library item |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function transform($item) |
31
|
|
|
{ |
32
|
|
|
$anime =& $item['anime']; |
33
|
|
|
$genres = $this->linearize_genres($item['anime']['genres']); |
34
|
|
|
|
35
|
|
|
$rating = NULL; |
36
|
|
|
if ($item['rating']['type'] === 'advanced') |
37
|
|
|
{ |
38
|
|
|
$rating = (is_numeric($item['rating']['value'])) |
39
|
|
|
? intval(2 * $item['rating']['value']) |
40
|
|
|
: '-'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$total_episodes = (is_numeric($anime['episode_count'])) |
44
|
|
|
? $anime['episode_count'] |
45
|
|
|
: '-'; |
46
|
|
|
|
47
|
|
|
$alternate_title = NULL; |
48
|
|
|
if (array_key_exists('alternate_title', $anime)) |
49
|
|
|
{ |
50
|
|
|
// If the alternate title is very similar, or |
51
|
|
|
// a subset of the main title, don't list the |
52
|
|
|
// alternate title |
53
|
|
|
$not_subset = stripos($anime['title'], $anime['alternate_title']) === FALSE; |
54
|
|
|
$diff = levenshtein($anime['title'], $anime['alternate_title']); |
55
|
|
|
if ($not_subset && $diff >= 5) |
56
|
|
|
{ |
57
|
|
|
$alternate_title = $anime['alternate_title']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [ |
62
|
|
|
'episodes' => [ |
63
|
|
|
'watched' => $item['episodes_watched'], |
64
|
|
|
'total' => $total_episodes, |
65
|
|
|
'length' => $anime['episode_length'], |
66
|
|
|
], |
67
|
|
|
'airing' => [ |
68
|
|
|
'status' => $anime['status'], |
69
|
|
|
'started' => $anime['started_airing'], |
70
|
|
|
'ended' => $anime['finished_airing'] |
71
|
|
|
], |
72
|
|
|
'anime' => [ |
73
|
|
|
'age_rating' => $anime['age_rating'], |
74
|
|
|
'title' => $anime['title'], |
75
|
|
|
'alternate_title' => $alternate_title, |
76
|
|
|
'slug' => $anime['slug'], |
77
|
|
|
'url' => $anime['url'], |
78
|
|
|
'type' => $anime['show_type'], |
79
|
|
|
'image' => $anime['cover_image'], |
80
|
|
|
'genres' => $genres, |
81
|
|
|
], |
82
|
|
|
'id' => $item['id'], |
83
|
|
|
'watching_status' => $item['status'], |
84
|
|
|
'notes' => $item['notes'], |
85
|
|
|
'rewatching' => (bool) $item['rewatching'], |
86
|
|
|
'rewatched' => $item['rewatched_times'], |
87
|
|
|
'user_rating' => $rating, |
88
|
|
|
'private' => (bool) $item['private'], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Convert transformed data to |
94
|
|
|
* api response format |
95
|
|
|
* |
96
|
|
|
* @param array $item Transformed library item |
97
|
|
|
* @return array API library item |
98
|
|
|
*/ |
99
|
|
|
public function untransform($item) |
100
|
|
|
{ |
101
|
|
|
// Messy mapping of boolean values to their API string equivalents |
102
|
|
|
$privacy = 'public'; |
103
|
|
|
if (array_key_exists('private', $item) && $item['private'] == TRUE) |
104
|
|
|
{ |
105
|
|
|
$privacy = 'private'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$rewatching = 'false'; |
109
|
|
|
if (array_key_exists('rewatching', $item) && $item['rewatching'] == TRUE) |
110
|
|
|
{ |
111
|
|
|
$rewatching = 'true'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
'id' => $item['id'], |
116
|
|
|
'status' => $item['watching_status'], |
117
|
|
|
'sane_rating_update' => $item['user_rating'] / 2, |
118
|
|
|
'rewatching' => $rewatching, |
119
|
|
|
'rewatched_times' => $item['rewatched'], |
120
|
|
|
'notes' => $item['notes'], |
121
|
|
|
'episodes_watched' => $item['episodes_watched'], |
122
|
|
|
'privacy' => $privacy |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Simplify structure of genre list |
128
|
|
|
* |
129
|
|
|
* @param array $raw_genres |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function linearize_genres(array $raw_genres) |
133
|
|
|
{ |
134
|
|
|
$genres = []; |
135
|
|
|
|
136
|
|
|
foreach ($raw_genres as $genre) |
137
|
|
|
{ |
138
|
|
|
$genres[] = $genre['name']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $genres; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
// End of AnimeListTransformer.php |