10
10
11
11
#import " JSONAPIResourceFormatter.h"
12
12
#import " JSONAPIResourceLinker.h"
13
+ #import " JSONAPIResourceModeler.h"
13
14
14
15
#import < objc/runtime.h>
15
16
#import < objc/message.h>
@@ -29,30 +30,20 @@ @implementation JSONAPIResource
29
30
#pragma mark - Class Methods
30
31
31
32
+ (NSArray *)jsonAPIResources : (NSArray *)array withLinked : (NSDictionary *)linked {
32
- return [JSONAPIResource jsonAPIResources: array withLinked: linked withClass: [self class ]];
33
- }
34
-
35
- + (NSArray *)jsonAPIResources : (NSArray *)array withLinked : (NSDictionary *)linked withClass : (Class )resourceObjectClass {
36
- if (resourceObjectClass == nil ) {
37
- resourceObjectClass = [self class ];
38
- }
39
33
40
34
NSMutableArray *mutableArray = [[NSMutableArray alloc ] init ];
41
35
for (NSDictionary *dict in array) {
36
+ NSString *type = dict[@" type" ] ?: @" " ;
37
+ Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: type]];
42
38
[mutableArray addObject: [[resourceObjectClass alloc ] initWithDictionary: dict withLinked: linked]];
43
39
}
44
40
45
41
return mutableArray;
46
42
}
47
43
48
44
+ (id )jsonAPIResource : (NSDictionary *)dictionary withLinked : (NSDictionary *)linked {
49
- return [JSONAPIResource jsonAPIResource: dictionary withLinked: linked withClass: [self class ]];
50
- }
51
-
52
- + (id )jsonAPIResource : (NSDictionary *)dictionary withLinked : (NSDictionary *)linked withClass : (Class )resourceObjectClass {
53
- if (resourceObjectClass == nil ) {
54
- resourceObjectClass = [self class ];
55
- }
45
+ NSString *type = dictionary[@" type" ] ?: @" " ;
46
+ Class resourceObjectClass = [JSONAPIResourceModeler resourceForLinkedType: [JSONAPIResourceLinker linkedType: type]];
56
47
57
48
return [[resourceObjectClass alloc ] initWithDictionary: dictionary withLinked: linked];
58
49
}
@@ -146,32 +137,39 @@ - (void)setWithDictionary:(NSDictionary*)dict {
146
137
}
147
138
148
139
- (void )linkLinks : (NSDictionary *)linked {
140
+
149
141
// Loops through links of resources
150
- for (NSString *linkTypeUnmapped in self.links .allKeys ) {
151
-
152
- NSString *linkType = [JSONAPIResourceLinker linkedType: linkTypeUnmapped];
153
- if (linkType == nil ) {
154
- linkType = linkTypeUnmapped;
155
- }
142
+ for (NSString *linkKey in self.links .allKeys ) {
156
143
157
144
// Gets linked objects for the resource
158
- id linksTo = [self .links objectForKey: linkTypeUnmapped];
159
- if ([linksTo isKindOfClass: [NSNumber class ]] == YES || [linksTo isKindOfClass: [NSString class ]] == YES ) {
160
- JSONAPIResource *linkedResource = [[linked objectForKey: linkType] objectForKey: linksTo];
161
- if (linkedResource != nil ) {
162
- [self .__resourceLinks setObject: linkedResource forKey: linkTypeUnmapped];
163
- }
145
+ id linksTo = [self .links objectForKey: linkKey];
146
+ if ([linksTo isKindOfClass: [NSDictionary class ]] == YES ) {
147
+
148
+ NSString *linkType = linksTo[@" type" ];
149
+ linkType = [JSONAPIResourceLinker linkedType: linkType] ?: linkType;
164
150
165
- } else if ([linksTo isKindOfClass: [NSArray class ]] == YES ) {
166
- NSMutableArray *linkedResources = [NSMutableArray array ];
167
- [self .__resourceLinks setObject: linkedResources forKey: linkTypeUnmapped];
168
- for (id linkedId in linksTo) {
169
- JSONAPIResource *linkedResource = [[linked objectForKey: linkType] objectForKey: linkedId];
151
+ if (linksTo[@" id" ] != nil ) {
152
+ id linksToId = linksTo[@" id" ];
153
+
154
+ JSONAPIResource *linkedResource = [[linked objectForKey: linkType] objectForKey: linksToId];
170
155
if (linkedResource != nil ) {
171
- [linkedResources addObject: linkedResource];
156
+ [self .__resourceLinks setObject: linkedResource forKey: linkKey];
157
+ }
158
+ } else if (linksTo[@" ids" ] != nil ) {
159
+ id linksToIds = linksTo[@" ids" ];
160
+
161
+ NSMutableArray *linkedResources = [NSMutableArray array ];
162
+ [self .__resourceLinks setObject: linkedResources forKey: linkKey];
163
+
164
+ for (id linkedId in linksToIds) {
165
+ JSONAPIResource *linkedResource = [[linked objectForKey: linkType] objectForKey: linkedId];
166
+ if (linkedResource != nil ) {
167
+ [linkedResources addObject: linkedResource];
168
+ }
172
169
}
173
170
}
174
171
172
+
175
173
}
176
174
}
177
175
0 commit comments