NBPhoneMetaData.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // NBPhoneMetaData.m
  3. // libPhoneNumber
  4. //
  5. //
  6. #import "NBPhoneMetaData.h"
  7. #import "NBNumberFormat.h"
  8. #import "NBPhoneNumberDesc.h"
  9. #import "NSArray+NBAdditions.h"
  10. @implementation NBPhoneMetaData
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. _numberFormats = [[NSMutableArray alloc] init];
  15. _intlNumberFormats = [[NSMutableArray alloc] init];
  16. _leadingZeroPossible = NO;
  17. _mainCountryForCode = NO;
  18. _sameMobileAndFixedLinePattern = NO;
  19. _internationalPrefix = @"NA";
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithEntry:(NSArray *)entry {
  24. self = [super init];
  25. if (self && entry != nil) {
  26. _generalDesc = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:1]];
  27. _fixedLine = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:2]];
  28. _mobile = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:3]];
  29. _tollFree = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:4]];
  30. _premiumRate = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:5]];
  31. _sharedCost = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:6]];
  32. _personalNumber = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:7]];
  33. _voip = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:8]];
  34. _pager = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:21]];
  35. _uan = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:25]];
  36. _emergency = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:27]];
  37. _voicemail = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:28]];
  38. _noInternationalDialling =
  39. [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:24]];
  40. _codeID = [entry nb_safeStringAtIndex:9];
  41. _countryCode = [entry nb_safeNumberAtIndex:10];
  42. _internationalPrefix = [entry nb_safeStringAtIndex:11];
  43. _preferredInternationalPrefix = [entry nb_safeStringAtIndex:17];
  44. _nationalPrefix = [entry nb_safeStringAtIndex:12];
  45. _preferredExtnPrefix = [entry nb_safeStringAtIndex:13];
  46. _nationalPrefixForParsing = [entry nb_safeStringAtIndex:15];
  47. _nationalPrefixTransformRule = [entry nb_safeStringAtIndex:16];
  48. _sameMobileAndFixedLinePattern = [[entry nb_safeNumberAtIndex:18] boolValue];
  49. _numberFormats = [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:19]];
  50. _intlNumberFormats = [self numberFormatsFromEntry:[entry nb_safeArrayAtIndex:20]];
  51. _mainCountryForCode = [[entry nb_safeNumberAtIndex:22] boolValue];
  52. _leadingDigits = [entry nb_safeStringAtIndex:23];
  53. _leadingZeroPossible = [[entry nb_safeNumberAtIndex:26] boolValue];
  54. #if SHORT_NUMBER_SUPPORT
  55. _shortCode = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:29]];
  56. _standardRate = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:30]];
  57. _carrierSpecific = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:31]];
  58. _smsServices = [[NBPhoneNumberDesc alloc] initWithEntry:[entry nb_safeArrayAtIndex:33]];
  59. #endif // SHORT_NUMBER_SUPPORT
  60. }
  61. return self;
  62. }
  63. - (NSArray<NBNumberFormat *> *)numberFormatsFromEntry:(NSArray *)entry {
  64. NSMutableArray *formats = [NSMutableArray arrayWithCapacity:entry.count];
  65. for (NSArray *format in entry) {
  66. NBNumberFormat *numberFormat = [[NBNumberFormat alloc] initWithEntry:format];
  67. [formats addObject:numberFormat];
  68. }
  69. return formats;
  70. }
  71. - (NSString *)description {
  72. return [NSString
  73. stringWithFormat:
  74. @"* codeID[%@] countryCode[%@] generalDesc[%@] fixedLine[%@] mobile[%@] tollFree[%@] "
  75. @"premiumRate[%@] sharedCost[%@] personalNumber[%@] voip[%@] pager[%@] uan[%@] "
  76. @"emergency[%@] voicemail[%@] noInternationalDialling[%@] internationalPrefix[%@] "
  77. @"preferredInternationalPrefix[%@] nationalPrefix[%@] preferredExtnPrefix[%@] "
  78. @"nationalPrefixForParsing[%@] nationalPrefixTransformRule[%@] "
  79. @"sameMobileAndFixedLinePattern[%@] numberFormats[%@] intlNumberFormats[%@] "
  80. @"mainCountryForCode[%@] leadingDigits[%@] leadingZeroPossible[%@]",
  81. _codeID, _countryCode, _generalDesc, _fixedLine, _mobile, _tollFree, _premiumRate,
  82. _sharedCost, _personalNumber, _voip, _pager, _uan, _emergency, _voicemail,
  83. _noInternationalDialling, _internationalPrefix, _preferredInternationalPrefix,
  84. _nationalPrefix, _preferredExtnPrefix, _nationalPrefixForParsing,
  85. _nationalPrefixTransformRule, _sameMobileAndFixedLinePattern ? @"Y" : @"N",
  86. _numberFormats, _intlNumberFormats, _mainCountryForCode ? @"Y" : @"N", _leadingDigits,
  87. _leadingZeroPossible ? @"Y" : @"N"];
  88. }
  89. #ifdef NB_USE_EXTENSIONS
  90. // We believe these methods are unused.
  91. // If you would like them back (not behind a flag) please file a bug with a reason for needing
  92. // them.
  93. - (instancetype)initWithCoder:(NSCoder *)coder {
  94. if (self = [super init]) {
  95. _generalDesc = [coder decodeObjectForKey:@"generalDesc"];
  96. _fixedLine = [coder decodeObjectForKey:@"fixedLine"];
  97. _mobile = [coder decodeObjectForKey:@"mobile"];
  98. _tollFree = [coder decodeObjectForKey:@"tollFree"];
  99. _premiumRate = [coder decodeObjectForKey:@"premiumRate"];
  100. _sharedCost = [coder decodeObjectForKey:@"sharedCost"];
  101. _personalNumber = [coder decodeObjectForKey:@"personalNumber"];
  102. _voip = [coder decodeObjectForKey:@"voip"];
  103. _pager = [coder decodeObjectForKey:@"pager"];
  104. _uan = [coder decodeObjectForKey:@"uan"];
  105. _emergency = [coder decodeObjectForKey:@"emergency"];
  106. _voicemail = [coder decodeObjectForKey:@"voicemail"];
  107. _noInternationalDialling = [coder decodeObjectForKey:@"noInternationalDialling"];
  108. _codeID = [coder decodeObjectForKey:@"codeID"];
  109. _countryCode = [coder decodeObjectForKey:@"countryCode"];
  110. _internationalPrefix = [coder decodeObjectForKey:@"internationalPrefix"];
  111. _preferredInternationalPrefix = [coder decodeObjectForKey:@"preferredInternationalPrefix"];
  112. _nationalPrefix = [coder decodeObjectForKey:@"nationalPrefix"];
  113. _preferredExtnPrefix = [coder decodeObjectForKey:@"preferredExtnPrefix"];
  114. _nationalPrefixForParsing = [coder decodeObjectForKey:@"nationalPrefixForParsing"];
  115. _nationalPrefixTransformRule = [coder decodeObjectForKey:@"nationalPrefixTransformRule"];
  116. _sameMobileAndFixedLinePattern =
  117. [[coder decodeObjectForKey:@"sameMobileAndFixedLinePattern"] boolValue];
  118. _numberFormats = [coder decodeObjectForKey:@"numberFormats"];
  119. _intlNumberFormats = [coder decodeObjectForKey:@"intlNumberFormats"];
  120. _mainCountryForCode = [[coder decodeObjectForKey:@"mainCountryForCode"] boolValue];
  121. _leadingDigits = [coder decodeObjectForKey:@"leadingDigits"];
  122. _leadingZeroPossible = [[coder decodeObjectForKey:@"leadingZeroPossible"] boolValue];
  123. }
  124. return self;
  125. }
  126. - (void)encodeWithCoder:(NSCoder *)coder {
  127. [coder encodeObject:_generalDesc forKey:@"generalDesc"];
  128. [coder encodeObject:_fixedLine forKey:@"fixedLine"];
  129. [coder encodeObject:_mobile forKey:@"mobile"];
  130. [coder encodeObject:_tollFree forKey:@"tollFree"];
  131. [coder encodeObject:_premiumRate forKey:@"premiumRate"];
  132. [coder encodeObject:_sharedCost forKey:@"sharedCost"];
  133. [coder encodeObject:_personalNumber forKey:@"personalNumber"];
  134. [coder encodeObject:_voip forKey:@"voip"];
  135. [coder encodeObject:_pager forKey:@"pager"];
  136. [coder encodeObject:_uan forKey:@"uan"];
  137. [coder encodeObject:_emergency forKey:@"emergency"];
  138. [coder encodeObject:_voicemail forKey:@"voicemail"];
  139. [coder encodeObject:_noInternationalDialling forKey:@"noInternationalDialling"];
  140. [coder encodeObject:_codeID forKey:@"codeID"];
  141. [coder encodeObject:_countryCode forKey:@"countryCode"];
  142. [coder encodeObject:_internationalPrefix forKey:@"internationalPrefix"];
  143. [coder encodeObject:_preferredInternationalPrefix forKey:@"preferredInternationalPrefix"];
  144. [coder encodeObject:_nationalPrefix forKey:@"nationalPrefix"];
  145. [coder encodeObject:_preferredExtnPrefix forKey:@"preferredExtnPrefix"];
  146. [coder encodeObject:_nationalPrefixForParsing forKey:@"nationalPrefixForParsing"];
  147. [coder encodeObject:_nationalPrefixTransformRule forKey:@"nationalPrefixTransformRule"];
  148. [coder encodeObject:[NSNumber numberWithBool:_sameMobileAndFixedLinePattern]
  149. forKey:@"sameMobileAndFixedLinePattern"];
  150. [coder encodeObject:_numberFormats forKey:@"numberFormats"];
  151. [coder encodeObject:_intlNumberFormats forKey:@"intlNumberFormats"];
  152. [coder encodeObject:[NSNumber numberWithBool:_mainCountryForCode] forKey:@"mainCountryForCode"];
  153. [coder encodeObject:_leadingDigits forKey:@"leadingDigits"];
  154. [coder encodeObject:[NSNumber numberWithBool:_leadingZeroPossible] forKey:@"leadingZeroPossible"];
  155. }
  156. #endif // NB_USE_EXTENSIONS
  157. @end