1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* 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 AnimeListClient |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @version 4.0 |
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\API; |
18
|
|
|
|
19
|
|
|
use Aviat\Ion\Json; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class encapsulating Json API data structure for a request or response |
23
|
|
|
*/ |
24
|
|
|
class JsonAPI { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The full data array |
28
|
|
|
* |
29
|
|
|
* Basic structure is generally like so: |
30
|
|
|
* @example [ |
31
|
|
|
* 'id' => '12016665', |
32
|
|
|
* 'type' => 'libraryEntries', |
33
|
|
|
* 'links' => [ |
34
|
|
|
* 'self' => 'https://kitsu.io/api/edge/library-entries/13016665' |
35
|
|
|
* ], |
36
|
|
|
* 'attributes' => [ |
37
|
|
|
* |
38
|
|
|
* ] |
39
|
|
|
* ] |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $data = []; |
44
|
|
|
|
45
|
|
|
public static function inlineRawIncludes(array &$data, string $key): array |
46
|
|
|
{ |
47
|
|
|
foreach($data['data'] as $i => &$item) |
48
|
|
|
{ |
49
|
|
|
$item[$key] = $data['included'][$i]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $data['data']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Take organized includes and inline them, where applicable |
57
|
|
|
* |
58
|
|
|
* @param array $included |
59
|
|
|
* @param string $key The key of the include to inline the other included values into |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public static function inlineIncludedRelationships(array $included, string $key): array |
63
|
|
|
{ |
64
|
|
|
$inlined = [ |
65
|
|
|
$key => [] |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
foreach ($included[$key] as $itemId => $item) |
69
|
|
|
{ |
70
|
|
|
// Duplicate the item for the output |
71
|
|
|
$inlined[$key][$itemId] = $item; |
72
|
|
|
|
73
|
|
|
foreach($item['relationships'] as $type => $ids) |
74
|
|
|
{ |
75
|
|
|
$inlined[$key][$itemId]['relationships'][$type] = []; |
76
|
|
|
foreach($ids as $id) |
77
|
|
|
{ |
78
|
|
|
$inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $inlined; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Reorganizes 'included' data to be keyed by |
88
|
|
|
* type => [ |
89
|
|
|
* id => data/attributes, |
90
|
|
|
* ] |
91
|
|
|
* |
92
|
|
|
* @param array $includes |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public static function organizeIncludes(array $includes): array |
96
|
|
|
{ |
97
|
|
|
$organized = []; |
98
|
|
|
|
99
|
|
|
foreach ($includes as $item) |
100
|
|
|
{ |
101
|
|
|
$type = $item['type']; |
102
|
|
|
$id = $item['id']; |
103
|
|
|
$organized[$type] = $organized[$type] ?? []; |
104
|
|
|
$organized[$type][$id] = $item['attributes']; |
105
|
|
|
|
106
|
|
|
if (array_key_exists('relationships', $item)) |
107
|
|
|
{ |
108
|
|
|
$organized[$type][$id]['relationships'] = static::organizeRelationships($item['relationships']); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $organized; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Reorganize relationship mappings to make them simpler to use |
117
|
|
|
* |
118
|
|
|
* Remove verbose structure, and just map: |
119
|
|
|
* type => [ idArray ] |
120
|
|
|
* |
121
|
|
|
* @param array $relationships |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function organizeRelationships(array $relationships): array |
125
|
|
|
{ |
126
|
|
|
$organized = []; |
127
|
|
|
|
128
|
|
|
foreach($relationships as $key => $data) |
129
|
|
|
{ |
130
|
|
|
if ( ! array_key_exists('data', $data)) |
131
|
|
|
{ |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$organized[$key] = $organized[$key] ?? []; |
136
|
|
|
|
137
|
|
|
foreach ($data['data'] as $item) |
138
|
|
|
{ |
139
|
|
|
$organized[$key][] = $item['id']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $organized; |
144
|
|
|
} |
145
|
|
|
} |