API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MangaListTransformer.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird 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 HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.0
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\API\Kitsu\Transformer;
  17. use Aviat\AnimeClient\API\Kitsu;
  18. use Aviat\Ion\StringWrapper;
  19. use Aviat\Ion\Transformer\AbstractTransformer;
  20. /**
  21. * Data transformation class for zippered Hummingbird manga
  22. */
  23. class MangaListTransformer extends AbstractTransformer {
  24. use StringWrapper;
  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. $included = $item['included'];
  34. $mangaId = $item['relationships']['media']['data']['id'];
  35. $manga = $included['manga'][$mangaId];
  36. $genres = array_column($manga['relationships']['genres'], 'name') ?? [];
  37. sort($genres);
  38. $rating = (int) $item['attributes']['rating'] !== 0
  39. ? (int) 2 * $item['attributes']['rating']
  40. : '-';
  41. $totalChapters = ((int) $manga['chapterCount'] !== 0)
  42. ? $manga['chapterCount']
  43. : '-';
  44. $totalVolumes = ((int) $manga['volumeCount'] !== 0)
  45. ? $manga['volumeCount']
  46. : '-';
  47. $readChapters = ((int) $item['attributes']['progress'] !== 0)
  48. ? $item['attributes']['progress']
  49. : '-';
  50. $MALid = NULL;
  51. if (array_key_exists('mappings', $manga['relationships']))
  52. {
  53. foreach ($manga['relationships']['mappings'] as $mapping)
  54. {
  55. if ($mapping['externalSite'] === 'myanimelist/manga')
  56. {
  57. $MALid = $mapping['externalId'];
  58. break;
  59. }
  60. }
  61. }
  62. $map = [
  63. 'id' => $item['id'],
  64. 'mal_id' => $MALid,
  65. 'chapters' => [
  66. 'read' => $readChapters,
  67. 'total' => $totalChapters
  68. ],
  69. 'volumes' => [
  70. 'read' => '-', //$item['attributes']['volumes_read'],
  71. 'total' => $totalVolumes
  72. ],
  73. 'manga' => [
  74. 'id' => $mangaId,
  75. 'titles' => Kitsu::filterTitles($manga),
  76. 'alternate_title' => NULL,
  77. 'slug' => $manga['slug'],
  78. 'url' => 'https://kitsu.io/manga/' . $manga['slug'],
  79. 'type' => $manga['mangaType'],
  80. 'image' => $manga['posterImage']['small'],
  81. 'genres' => $genres,
  82. ],
  83. 'reading_status' => $item['attributes']['status'],
  84. 'notes' => $item['attributes']['notes'],
  85. 'rereading' => (bool)$item['attributes']['reconsuming'],
  86. 'reread' => $item['attributes']['reconsumeCount'],
  87. 'user_rating' => $rating,
  88. ];
  89. return $map;
  90. }
  91. /**
  92. * Untransform data to update the api
  93. *
  94. * @param array $item
  95. * @return array
  96. */
  97. public function untransform($item)
  98. {
  99. $rereading = (array_key_exists('rereading', $item)) && (bool)$item['rereading'];
  100. $map = [
  101. 'id' => $item['id'],
  102. 'mal_id' => $item['mal_id'],
  103. 'data' => [
  104. 'status' => $item['status'],
  105. 'reconsuming' => $rereading,
  106. 'reconsumeCount' => (int)$item['reread_count'],
  107. 'notes' => $item['notes'],
  108. ],
  109. ];
  110. if (is_numeric($item['chapters_read']) && $item['chapters_read'] > 0)
  111. {
  112. $map['data']['progress'] = (int)$item['chapters_read'];
  113. }
  114. if (is_numeric($item['new_rating']) && $item['new_rating'] > 0)
  115. {
  116. $map['data']['rating'] = $item['new_rating'] / 2;
  117. }
  118. return $map;
  119. }
  120. }
  121. // End of MangaListTransformer.php