Do you wish to register an account?
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.

330 lines
7.2KB

  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu to manage anime and manga watch lists
  6. *
  7. * PHP version 7.1
  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.1
  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 ( ! is_array($item))
  59. {
  60. continue;
  61. }
  62. if (array_key_exists('relationships', $item))
  63. {
  64. foreach($item['relationships'] as $relType => $props)
  65. {
  66. if (array_keys($props) === ['links'])
  67. {
  68. unset($item['relationships'][$relType]);
  69. if (empty($item['relationships']))
  70. {
  71. unset($item['relationships']);
  72. }
  73. continue;
  74. }
  75. if (array_key_exists('links', $props))
  76. {
  77. unset($item['relationships'][$relType]['links']);
  78. }
  79. if (array_key_exists('data', $props))
  80. {
  81. if (empty($props['data']))
  82. {
  83. unset($item['relationships'][$relType]['data']);
  84. if (empty($item['relationships'][$relType]))
  85. {
  86. unset($item['relationships'][$relType]);
  87. }
  88. continue;
  89. }
  90. // Single data item
  91. else if (array_key_exists('id', $props['data']))
  92. {
  93. $idKey = $props['data']['id'];
  94. $typeKey = $props['data']['type'];
  95. $relationship =& $item['relationships'][$relType];
  96. unset($relationship['data']);
  97. if (in_array($relType, $singular))
  98. {
  99. $relationship = $included[$typeKey][$idKey];
  100. continue;
  101. }
  102. if ($relType === $typeKey)
  103. {
  104. $relationship[$idKey] = $included[$typeKey][$idKey];
  105. continue;
  106. }
  107. $relationship[$typeKey][$idKey] = $included[$typeKey][$idKey];
  108. }
  109. // Multiple data items
  110. else
  111. {
  112. foreach($props['data'] as $j => $datum)
  113. {
  114. $idKey = $props['data'][$j]['id'];
  115. $typeKey = $props['data'][$j]['type'];
  116. $relationship =& $item['relationships'][$relType];
  117. if ($relType === $typeKey)
  118. {
  119. $relationship[$idKey] = $included[$typeKey][$idKey];
  120. continue;
  121. }
  122. $relationship[$typeKey][$idKey][$j] = $included[$typeKey][$idKey];
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. $data['data']['included'] = $included;
  130. return $data['data'];
  131. }
  132. /**
  133. * Restructure included data to make it simpler to inline
  134. *
  135. * @param array $included
  136. * @return array
  137. */
  138. public static function organizeIncluded(array $included): array
  139. {
  140. $organized = [];
  141. // First pass, create [ type => items[] ] structure
  142. foreach($included as &$item)
  143. {
  144. $type = $item['type'];
  145. $id = $item['id'];
  146. $organized[$type] = $organized[$type] ?? [];
  147. $newItem = [];
  148. foreach(['attributes', 'relationships'] as $key)
  149. {
  150. if (array_key_exists($key, $item))
  151. {
  152. // Remove 'links' type relationships
  153. if ($key === 'relationships')
  154. {
  155. foreach($item['relationships'] as $relType => $props)
  156. {
  157. if (array_keys($props) === ['links'])
  158. {
  159. unset($item['relationships'][$relType]);
  160. if (empty($item['relationships']))
  161. {
  162. continue 2;
  163. }
  164. }
  165. }
  166. }
  167. $newItem[$key] = $item[$key];
  168. }
  169. }
  170. $organized[$type][$id] = $newItem;
  171. }
  172. // Second pass, go through and fill missing relationships in the first pass
  173. foreach($organized as $type => $items)
  174. {
  175. foreach($items as $id => $item)
  176. {
  177. if (array_key_exists('relationships', $item) && is_array($item['relationships']))
  178. {
  179. foreach($item['relationships'] as $relType => $props)
  180. {
  181. if (array_key_exists('data', $props) && is_array($props['data']) && array_key_exists('id', $props['data']))
  182. {
  183. if (array_key_exists($props['data']['id'], $organized[$props['data']['type']]))
  184. {
  185. $idKey = $props['data']['id'];
  186. $typeKey = $props['data']['type'];
  187. $relationship =& $organized[$type][$id]['relationships'][$relType];
  188. unset($relationship['links']);
  189. unset($relationship['data']);
  190. if ($relType === $typeKey)
  191. {
  192. $relationship[$idKey] = $included[$typeKey][$idKey];
  193. continue;
  194. }
  195. $relationship[$typeKey][$idKey] = $organized[$typeKey][$idKey];
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. return $organized;
  203. }
  204. /**
  205. * Take organized includes and inline them, where applicable
  206. *
  207. * @param array $included
  208. * @param string $key The key of the include to inline the other included values into
  209. * @return array
  210. */
  211. public static function inlineIncludedRelationships(array $included, string $key): array
  212. {
  213. $inlined = [
  214. $key => []
  215. ];
  216. foreach ($included[$key] as $itemId => $item)
  217. {
  218. // Duplicate the item for the output
  219. $inlined[$key][$itemId] = $item;
  220. foreach($item['relationships'] as $type => $ids)
  221. {
  222. $inlined[$key][$itemId]['relationships'][$type] = [];
  223. foreach($ids as $id)
  224. {
  225. $inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id];
  226. }
  227. }
  228. }
  229. return $inlined;
  230. }
  231. /**
  232. * Reorganizes 'included' data to be keyed by
  233. * type => [
  234. * id => data/attributes,
  235. * ]
  236. *
  237. * @param array $includes
  238. * @return array
  239. */
  240. public static function organizeIncludes(array $includes): array
  241. {
  242. $organized = [];
  243. foreach ($includes as $item)
  244. {
  245. $type = $item['type'];
  246. $id = $item['id'];
  247. $organized[$type] = $organized[$type] ?? [];
  248. $organized[$type][$id] = $item['attributes'];
  249. if (array_key_exists('relationships', $item))
  250. {
  251. $organized[$type][$id]['relationships'] = static::organizeRelationships($item['relationships']);
  252. }
  253. }
  254. return $organized;
  255. }
  256. /**
  257. * Reorganize relationship mappings to make them simpler to use
  258. *
  259. * Remove verbose structure, and just map:
  260. * type => [ idArray ]
  261. *
  262. * @param array $relationships
  263. * @return array
  264. */
  265. public static function organizeRelationships(array $relationships): array
  266. {
  267. $organized = [];
  268. foreach($relationships as $key => $data)
  269. {
  270. if ( ! array_key_exists('data', $data))
  271. {
  272. continue;
  273. }
  274. $organized[$key] = $organized[$key] ?? [];
  275. foreach ($data['data'] as $item)
  276. {
  277. if (\is_array($item) && array_key_exists('id', $item))
  278. {
  279. $organized[$key][] = $item['id'];
  280. }
  281. }
  282. }
  283. return $organized;
  284. }
  285. }