|
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 - 2016 |
|
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
|
|
|
* Data transformation class for zippered Hummingbird manga |
|
20
|
|
|
*/ |
|
21
|
|
|
class MangaListTransformer extends AbstractTransformer { |
|
22
|
|
|
|
|
23
|
|
|
use \Aviat\Ion\StringWrapper; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Remap zipped anime data to a more logical form |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $item manga entry item |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function transform($item) |
|
32
|
|
|
{ |
|
33
|
|
|
$manga =& $item['manga']; |
|
34
|
|
|
|
|
35
|
|
|
$rating = (is_numeric($item['rating'])) |
|
36
|
|
|
? intval(2 * $item['rating']) |
|
37
|
|
|
: '-'; |
|
38
|
|
|
|
|
39
|
|
|
$total_chapters = ($manga['chapter_count'] > 0) |
|
40
|
|
|
? $manga['chapter_count'] |
|
41
|
|
|
: '-'; |
|
42
|
|
|
|
|
43
|
|
|
$total_volumes = ($manga['volume_count'] > 0) |
|
44
|
|
|
? $manga['volume_count'] |
|
45
|
|
|
: '-'; |
|
46
|
|
|
|
|
47
|
|
|
$map = [ |
|
48
|
|
|
'id' => $item['id'], |
|
49
|
|
|
'chapters' => [ |
|
50
|
|
|
'read' => $item['chapters_read'], |
|
51
|
|
|
'total' => $total_chapters |
|
52
|
|
|
], |
|
53
|
|
|
'volumes' => [ |
|
54
|
|
|
'read' => $item['volumes_read'], |
|
55
|
|
|
'total' => $total_volumes |
|
56
|
|
|
], |
|
57
|
|
|
'manga' => [ |
|
58
|
|
|
'title' => $manga['romaji_title'], |
|
59
|
|
|
'alternate_title' => NULL, |
|
60
|
|
|
'slug' => $manga['id'], |
|
61
|
|
|
'url' => 'https://hummingbird.me/manga/' . $manga['id'], |
|
62
|
|
|
'type' => $manga['manga_type'], |
|
63
|
|
|
'image' => $manga['poster_image_thumb'], |
|
64
|
|
|
'genres' => $manga['genres'], |
|
65
|
|
|
], |
|
66
|
|
|
'reading_status' => $item['status'], |
|
67
|
|
|
'notes' => $item['notes'], |
|
68
|
|
|
'rereading' => (bool)$item['rereading'], |
|
69
|
|
|
'reread' => $item['reread_count'], |
|
70
|
|
|
'user_rating' => $rating |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
if (array_key_exists('english_title', $manga)) |
|
74
|
|
|
{ |
|
75
|
|
|
$diff = levenshtein($manga['romaji_title'], $manga['english_title']); |
|
76
|
|
|
|
|
77
|
|
|
// If the titles are REALLY similar, don't bother showing both |
|
78
|
|
|
if ($diff >= 5) |
|
79
|
|
|
{ |
|
80
|
|
|
$map['manga']['alternate_title'] = $manga['english_title']; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $map; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Untransform data to update the api |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $item |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function untransform($item) |
|
94
|
|
|
{ |
|
95
|
|
|
$private = (array_key_exists('private', $item)) |
|
96
|
|
|
? (bool)$item['private'] |
|
97
|
|
|
: false; |
|
98
|
|
|
|
|
99
|
|
|
$rereading = (array_key_exists('rereading', $item)) |
|
100
|
|
|
? (bool)$item['rereading'] |
|
101
|
|
|
: false; |
|
102
|
|
|
|
|
103
|
|
|
$map = [ |
|
104
|
|
|
'id' => $item['id'], |
|
105
|
|
|
'manga_id' => $item['manga_id'], |
|
106
|
|
|
'status' => $item['status'], |
|
107
|
|
|
'chapters_read' => (int)$item['chapters_read'], |
|
108
|
|
|
'volumes_read' => (int)$item['volumes_read'], |
|
109
|
|
|
'rereading' => $rereading, |
|
110
|
|
|
'reread_count' => (int)$item['reread_count'], |
|
111
|
|
|
'private' => $private, |
|
112
|
|
|
'notes' => $item['notes'], |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
if ($item['new_rating'] !== $item['old_rating']) |
|
116
|
|
|
{ |
|
117
|
|
|
$map['rating'] = ($item['new_rating'] > 0) |
|
118
|
|
|
? $item['new_rating'] / 2 |
|
119
|
|
|
: $item['old_rating'] / 2; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $map; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
// End of MangaListTransformer.php |