forked from iziz/libPhoneNumber-iOS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNBMetadataHelper.m
More file actions
200 lines (157 loc) · 6.11 KB
/
NBMetadataHelper.m
File metadata and controls
200 lines (157 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// NBMetadataHelper.m
// libPhoneNumber
//
// Created by tabby on 2015. 2. 8..
// Copyright (c) 2015년 ohtalk.me. All rights reserved.
//
#import "NBMetadataHelper.h"
#import "NBPhoneMetaData.h"
#import "NBGeneratedPhoneNumberMetaData.h"
@interface NBMetadataHelper ()
// Cached metadata
@property(nonatomic, strong) NBPhoneMetaData *cachedMetaData;
@property(nonatomic, strong) NSString *cachedMetaDataKey;
@end
@implementation NBMetadataHelper
/*
Terminologies
- Country Number (CN) = Country code for i18n calling
- Country Code (CC) : ISO country codes (2 chars)
Ref. site (countrycode.org)
*/
+ (NSDictionary *)phoneNumberDataMap {
static NSDictionary *phoneNumberDataDictionary;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Data is a gzipped JSON file that is embedded in the binary.
// See GeneratePhoneNumberHeader.sh and PhoneNumberMetaData.h for details.
NSMutableData* gunzippedData = [NSMutableData dataWithLength:kPhoneNumberMetaDataExpandedLength];
z_stream zStream;
memset(&zStream, 0, sizeof(zStream));
__attribute((unused)) int err = inflateInit2(&zStream, 16);
NSAssert(err == Z_OK, @"Unable to init stream. err = %d", err);
zStream.next_in = kPhoneNumberMetaData;
zStream.avail_in = (uint)kPhoneNumberMetaDataCompressedLength;
zStream.next_out = (Bytef *)gunzippedData.bytes;
zStream.avail_out = (uint)gunzippedData.length;
err = inflate(&zStream, Z_FINISH);
NSAssert(err == Z_STREAM_END, @"Unable to inflate compressed data. err = %d", err);
err = inflateEnd(&zStream);
NSAssert(err == Z_OK, @"Unable to inflate compressed data. err = %d", err);
NSError *error = nil;
phoneNumberDataDictionary = [NSJSONSerialization JSONObjectWithData:gunzippedData
options:0
error:&error];
NSAssert(error == nil, @"Unable to convert JSON - %@", error);
});
return phoneNumberDataDictionary;
}
+ (NSDictionary *)CCode2CNMap {
static NSMutableDictionary *mapCCode2CN;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *countryCodeToRegionCodeMap = [self CN2CCodeMap];
mapCCode2CN = [[NSMutableDictionary alloc] init];
for (NSString *countryCode in countryCodeToRegionCodeMap) {
NSArray *regionCodes = countryCodeToRegionCodeMap[countryCode];
for (NSString *regionCode in regionCodes) {
mapCCode2CN[regionCode] = countryCode;
}
}
});
return mapCCode2CN;
}
+(NSDictionary *)CN2CCodeMap {
return [self phoneNumberDataMap][@"countryCodeToRegionCodeMap"];
}
- (NSArray*)getAllMetadata
{
NSArray *countryCodes = [NSLocale ISOCountryCodes];
NSMutableArray *resultMetadata = [[NSMutableArray alloc] init];
for (NSString *countryCode in countryCodes) {
id countryDictionaryInstance = [NSDictionary dictionaryWithObject:countryCode forKey:NSLocaleCountryCode];
NSString *identifier = [NSLocale localeIdentifierFromComponents:countryDictionaryInstance];
NSString *country = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
NSMutableDictionary *countryMeta = [[NSMutableDictionary alloc] init];
if (country) {
[countryMeta setObject:country forKey:@"name"];
}
if (countryCode) {
[countryMeta setObject:countryCode forKey:@"code"];
}
NBPhoneMetaData *metaData = [self getMetadataForRegion:countryCode];
if (metaData) {
[countryMeta setObject:metaData forKey:@"metadata"];
}
[resultMetadata addObject:countryMeta];
}
return resultMetadata;
}
+ (NSArray *)regionCodeFromCountryCode:(NSNumber *)countryCodeNumber
{
NSArray *res = [self CN2CCodeMap][[countryCodeNumber stringValue]];
if ([res isKindOfClass:[NSArray class]] && [res count] > 0) {
return res;
}
return nil;
}
+ (NSString *)countryCodeFromRegionCode:(NSString* )regionCode
{
return [self CCode2CNMap][regionCode];
}
+ (NSString *)normalizeNonBreakingSpace:(NSString *)aString
{
return [aString stringByReplacingOccurrencesOfString:NB_NON_BREAKING_SPACE withString:@" "];
}
/**
* Returns the metadata for the given region code or {@code nil} if the region
* code is invalid or unknown.
*
* @param {?string} regionCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForRegion:(NSString *)regionCode
{
if ([[self class] hasValue:regionCode] == NO) {
return nil;
}
regionCode = [regionCode uppercaseString];
if (_cachedMetaDataKey && [_cachedMetaDataKey isEqualToString:regionCode]) {
return _cachedMetaData;
}
NSDictionary *dict = [[self class] phoneNumberDataMap][@"countryToMetadata"];
NSArray *entry = dict[regionCode];
if (entry) {
NBPhoneMetaData *metadata = [[NBPhoneMetaData alloc] initWithEntry:entry];
_cachedMetaData = metadata;
_cachedMetaDataKey = regionCode;
return metadata;
}
return nil;
}
/**
* @param countryCallingCode countryCallingCode
* @return {i18n.phonenumbers.PhoneMetadata}
*/
- (NBPhoneMetaData *)getMetadataForNonGeographicalRegion:(NSNumber *)countryCallingCode
{
NSString *countryCallingCodeStr = countryCallingCode.stringValue;
return [self getMetadataForRegion:countryCallingCodeStr];
}
#pragma mark - Regular expression Utilities -
+ (BOOL)hasValue:(NSString*)string
{
static dispatch_once_t onceToken;
static NSCharacterSet *whitespaceCharSet = nil;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *spaceCharSet = [NSMutableCharacterSet characterSetWithCharactersInString:NB_NON_BREAKING_SPACE];
[spaceCharSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
whitespaceCharSet = spaceCharSet;
});
if (string == nil || [string stringByTrimmingCharactersInSet:whitespaceCharSet].length <= 0) {
return NO;
}
return YES;
}
@end