API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
anime
manga
kitsu
anilist
single-user

AnimeListTransformer.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\MAL\Transformer;
  17. use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
  18. use Aviat\AnimeClient\Types\{AnimeFormItem, AnimeFormItemData};
  19. use Aviat\Ion\Transformer\AbstractTransformer;
  20. /**
  21. * Transformer for updating MAL List
  22. */
  23. final class AnimeListTransformer extends AbstractTransformer {
  24. /**
  25. * Identity transformation
  26. *
  27. * @param array $item
  28. * @return array
  29. */
  30. public function transform($item)
  31. {
  32. return $item;
  33. }
  34. /**
  35. * Transform Kitsu episode data to MAL episode data
  36. *
  37. * @param array $item
  38. * @return AnimeFormItem
  39. */
  40. public function untransform(array $item): AnimeFormItem
  41. {
  42. $map = new AnimeFormItem([
  43. 'id' => $item['mal_id'],
  44. 'data' => new AnimeFormItemData([]),
  45. ]);
  46. foreach($item['data'] as $key => $value)
  47. {
  48. switch($key)
  49. {
  50. case 'progress':
  51. $map['data']['episode'] = $value;
  52. break;
  53. case 'notes':
  54. $map['data']['comments'] = $value;
  55. break;
  56. case 'rating':
  57. $map['data']['score'] = $value * 2;
  58. break;
  59. case 'reconsuming':
  60. $map['data']['enable_rewatching'] = (bool) $value;
  61. break;
  62. case 'reconsumeCount':
  63. $map['data']['times_rewatched'] = $value;
  64. break;
  65. case 'status':
  66. $map['data']['status'] = AnimeWatchingStatus::KITSU_TO_MAL[$value];
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. return $map;
  73. }
  74. }