UNPKG

4.18 kBPlain TextView Raw
1#import <EXPaymentsStripe/EXTPSConvert.h>
2#define TO_DOUBLE(x) ([((NSNumber *)x) doubleValue])
3#define TO_UNSIGNED_INTEGER(x) ([((NSNumber *)x) unsignedIntegerValue]);
4
5@implementation EXTPSConvert
6
7+ (NSString *)STPBankAccountHolderTypeToString:(STPBankAccountHolderType)type {
8 NSString *string = nil;
9 switch (type) {
10 case STPBankAccountHolderTypeCompany: {
11 string = @"company";
12 }
13 break;
14 case STPBankAccountHolderTypeIndividual:
15 default: {
16 string = @"individual";
17 }
18 break;
19 }
20 return string;
21}
22
23+ (NSString *)STPBankAccountStatusToString:(STPBankAccountStatus)status {
24 NSString *string = nil;
25 switch (status) {
26 case STPBankAccountStatusValidated: {
27 string = @"validated";
28 }
29 break;
30 case STPBankAccountStatusVerified: {
31 string = @"verified";
32 }
33 break;
34 case STPBankAccountStatusErrored: {
35 string = @"errored";
36 }
37 break;
38 case STPBankAccountStatusNew:
39 default: {
40 string = @"new";
41 }
42 break;
43 }
44 return string;
45}
46
47+ (STPBankAccountHolderType)holderType:(id)json
48{
49 NSDictionary *mapping = @{
50 @"individual": @(STPBankAccountHolderTypeIndividual),
51 @"company": @(STPBankAccountHolderTypeCompany),
52 };
53
54 if ([json isEqual:@"individual"]) {
55 return (STPBankAccountHolderType)mapping[@"individual"];
56 }
57 return (STPBankAccountHolderType)mapping[@"company"];
58}
59
60+ (STPBankAccountStatus)statusType:(id)json
61{
62 NSDictionary *mapping = @{
63 @"new": @(STPBankAccountStatusNew),
64 @"validated": @(STPBankAccountStatusValidated),
65 @"verified": @(STPBankAccountStatusVerified),
66 @"errored": @(STPBankAccountStatusErrored)
67 };
68
69 if ([mapping valueForKey:json]) {
70 return (STPBankAccountStatus)[mapping valueForKey:json];
71 }
72 return STPBankAccountStatusNew;
73}
74
75+ (UIColor *)UIColor:(id)json
76{
77 if (!json) {
78 return nil;
79 }
80 if ([json isKindOfClass:[NSArray class]]) {
81 NSArray *components = (NSArray<NSNumber *> *)json;
82 CGFloat alpha = components.count > 3 ? TO_DOUBLE(components[3]) : 1.0;
83 return [UIColor colorWithRed:TO_DOUBLE(components[0])
84 green:TO_DOUBLE(components[1])
85 blue:TO_DOUBLE(components[2])
86 alpha:alpha];
87 } else if ([json isKindOfClass:[NSNumber class]]) {
88 NSUInteger argb = TO_UNSIGNED_INTEGER(json);
89 CGFloat a = ((argb >> 24) & 0xFF) / 255.0;
90 CGFloat r = ((argb >> 16) & 0xFF) / 255.0;
91 CGFloat g = ((argb >> 8) & 0xFF) / 255.0;
92 CGFloat b = (argb & 0xFF) / 255.0;
93 return [UIColor colorWithRed:r green:g blue:b alpha:a];
94 } else {
95 NSLog(@"a UIColor. Did you forget to call processColor() on the JS side?\n");
96 return nil;
97 }
98}
99
100+ (NSArray *)EXConvertArrayValue:(SEL) type value:(id) json
101{
102 __block BOOL copy = NO;
103 __block NSArray *values = json = [EXTPSConvert NSArray:json];
104 [json enumerateObjectsUsingBlock:^(id jsonValue, NSUInteger idx, __unused BOOL *stop) {
105 id value = [EXTPSConvert performSelector:type withObject:jsonValue];
106 if (copy) {
107 if (value) {
108 [(NSMutableArray *)values addObject:value];
109 }
110 } else if (value != jsonValue) {
111 // Converted value is different, so we'll need to copy the array
112 values = [[NSMutableArray alloc] initWithCapacity:values.count];
113 for (NSUInteger i = 0; i < idx; i++) {
114 [(NSMutableArray *)values addObject:json[i]];
115 }
116 if (value) {
117 [(NSMutableArray *)values addObject:value];
118 }
119 copy = YES;
120 }
121 }];
122 return values;
123}
124
125EX_ENUM_CONVERTER(UIKeyboardAppearance, (@{
126 @"default": @(UIKeyboardAppearanceDefault),
127 @"light": @(UIKeyboardAppearanceLight),
128 @"dark": @(UIKeyboardAppearanceDark),
129 }), UIKeyboardAppearanceDefault, integerValue)
130
131EX_JSON_ARRAY_CONVERTER(NSArray)
132EX_JSON_ARRAY_CONVERTER(NSString)
133EX_JSON_ARRAY_CONVERTER_NAMED(NSArray<NSString *>, NSStringArray)
134EX_JSON_ARRAY_CONVERTER(NSDictionary)
135EX_JSON_ARRAY_CONVERTER(NSNumber)
136
137@end
138
139