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

JsonAPI.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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;
  17. /**
  18. * Class encapsulating Json API data structure for a request or response
  19. */
  20. final class JsonAPI {
  21. /**
  22. * The full data array
  23. *
  24. * Basic structure is generally like so:
  25. * [
  26. * 'id' => '12016665',
  27. * 'type' => 'libraryEntries',
  28. * 'links' => [
  29. * 'self' => 'https://kitsu.io/api/edge/library-entries/13016665'
  30. * ],
  31. * 'attributes' => [
  32. *
  33. * ]
  34. * ]
  35. *
  36. * @var array
  37. */
  38. protected $data = [];
  39. /**
  40. * Inline all included data
  41. *
  42. * @param array $data - The raw JsonAPI response data
  43. * @return array
  44. */
  45. public static function organizeData(array $data): array
  46. {
  47. // relationships that have singular data
  48. $singular = [
  49. 'waifu'
  50. ];
  51. // Reorganize included data
  52. $included = array_key_exists('included', $data)
  53. ? static::organizeIncluded($data['included'])
  54. : [];
  55. // Inline organized data
  56. foreach($data['data'] as $i => &$item)
  57. {
  58. if (array_key_exists('relationships', $item))
  59. {
  60. foreach($item['relationships'] as $relType => $props)
  61. {
  62. if (array_keys($props) === ['links'])
  63. {
  64. unset($item['relationships'][$relType]);
  65. if (empty($item['relationships']))
  66. {
  67. unset($item['relationships']);
  68. }
  69. continue;
  70. }
  71. if (array_key_exists('links', $props))
  72. {
  73. unset($item['relationships'][$relType]['links']);
  74. }
  75. if (array_key_exists('data', $props))
  76. {
  77. if (empty($props['data']))
  78. {
  79. unset($item['relationships'][$relType]['data']);
  80. if (empty($item['relationships'][$relType]))
  81. {
  82. unset($item['relationships'][$relType]);
  83. }
  84. continue;
  85. }
  86. // Single data item
  87. else if (array_key_exists('id', $props['data']))
  88. {
  89. $idKey = $props['data']['id'];
  90. $typeKey = $props['data']['type'];
  91. $relationship =& $item['relationships'][$relType];
  92. unset($relationship['data']);
  93. if (in_array($relType, $singular))
  94. {
  95. $relationship = $included[$typeKey][$idKey];
  96. continue;
  97. }
  98. if ($relType === $typeKey)
  99. {
  100. $relationship[$idKey] = $included[$typeKey][$idKey];
  101. continue;
  102. }
  103. $relationship[$typeKey][$idKey] = $included[$typeKey][$idKey];
  104. }
  105. // Multiple data items
  106. else
  107. {
  108. foreach($props['data'] as $j => $datum)
  109. {
  110. $idKey = $props['data'][$j]['id'];
  111. $typeKey = $props['data'][$j]['type'];
  112. $relationship =& $item['relationships'][$relType];
  113. if ($relType === $typeKey)
  114. {
  115. $relationship[$idKey] = $included[$typeKey][$idKey];
  116. continue;
  117. }
  118. $relationship[$typeKey][$idKey][$j] = $included[$typeKey][$idKey];
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. $data['data']['included'] = $included;
  126. return $data['data'];
  127. }
  128. /**
  129. * Restructure included data to make it simpler to inline
  130. *
  131. * @param array $included
  132. * @return array
  133. */
  134. public static function organizeIncluded(array $included): array
  135. {
  136. $organized = [];
  137. // First pass, create [ type => items[] ] structure
  138. foreach($included as &$item)
  139. {
  140. $type = $item['type'];
  141. $id = $item['id'];
  142. $organized[$type] = $organized[$type] ?? [];
  143. $newItem = [];
  144. foreach(['attributes', 'relationships'] as $key)
  145. {
  146. if (array_key_exists($key, $item))
  147. {
  148. // Remove 'links' type relationships
  149. if ($key === 'relationships')
  150. {
  151. foreach($item['relationships'] as $relType => $props)
  152. {
  153. if (array_keys($props) === ['links'])
  154. {
  155. unset($item['relationships'][$relType]);
  156. if (empty($item['relationships']))
  157. {
  158. continue 2;
  159. }
  160. }
  161. }
  162. }
  163. $newItem[$key] = $item[$key];
  164. }
  165. }
  166. $organized[$type][$id] = $newItem;
  167. }
  168. // Second pass, go through and fill missing relationships in the first pass
  169. foreach($organized as $type => $items)
  170. {
  171. foreach($items as $id => $item)
  172. {
  173. if (array_key_exists('relationships', $item) && is_array($item['relationships']))
  174. {
  175. foreach($item['relationships'] as $relType => $props)
  176. {
  177. if (array_key_exists('data', $props) && is_array($props['data']) && array_key_exists('id', $props['data']))
  178. {
  179. if (array_key_exists($props['data']['id'], $organized[$props['data']['type']]))
  180. {
  181. $idKey = $props['data']['id'];
  182. $typeKey = $props['data']['type'];
  183. $relationship =& $organized[$type][$id]['relationships'][$relType];
  184. unset($relationship['links']);
  185. unset($relationship['data']);
  186. if ($relType === $typeKey)
  187. {
  188. $relationship[$idKey] = $included[$typeKey][$idKey];
  189. continue;
  190. }
  191. $relationship[$typeKey][$idKey] = $organized[$typeKey][$idKey];
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. return $organized;
  199. }
  200. /**
  201. * Take organized includes and inline them, where applicable
  202. *
  203. * @param array $included
  204. * @param string $key The key of the include to inline the other included values into
  205. * @return array
  206. */
  207. public static function inlineIncludedRelationships(array $included, string $key): array
  208. {
  209. $inlined = [
  210. $key => []
  211. ];
  212. foreach ($included[$key] as $itemId => $item)
  213. {
  214. // Duplicate the item for the output
  215. $inlined[$key][$itemId] = $item;
  216. foreach($item['relationships'] as $type => $ids)
  217. {
  218. $inlined[$key][$itemId]['relationships'][$type] = [];
  219. foreach($ids as $id)
  220. {
  221. $inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id];
  222. }
  223. }
  224. }
  225. return $inlined;
  226. }
  227. /**
  228. * Reorganizes 'included' data to be keyed by
  229. * type => [
  230. * id => data/attributes,
  231. * ]
  232. *
  233. * @param array $includes
  234. * @return array
  235. */
  236. public static function organizeIncludes(array $includes): array
  237. {
  238. $organized = [];
  239. foreach ($includes as $item)
  240. {
  241. $type = $item['type'];
  242. $id = $item['id'];
  243. $organized[$type] = $organized[$type] ?? [];
  244. $organized[$type][$id] = $item['attributes'];
  245. if (array_key_exists('relationships', $item))
  246. {
  247. $organized[$type][$id]['relationships'] = static::organizeRelationships($item['relationships']);
  248. }
  249. }
  250. return $organized;
  251. }
  252. /**
  253. * Reorganize relationship mappings to make them simpler to use
  254. *
  255. * Remove verbose structure, and just map:
  256. * type => [ idArray ]
  257. *
  258. * @param array $relationships
  259. * @return array
  260. */
  261. public static function organizeRelationships(array $relationships): array
  262. {
  263. $organized = [];
  264. foreach($relationships as $key => $data)
  265. {
  266. if ( ! array_key_exists('data', $data))
  267. {
  268. continue;
  269. }
  270. $organized[$key] = $organized[$key] ?? [];
  271. foreach ($data['data'] as $item)
  272. {
  273. if (\is_array($item) && array_key_exists('id', $item))
  274. {
  275. $organized[$key][] = $item['id'];
  276. }
  277. }
  278. }
  279. return $organized;
  280. }
  281. }