NBNumberFormat.m 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NBPhoneNumberFormat.m
  3. // libPhoneNumber
  4. //
  5. //
  6. #import "NBNumberFormat.h"
  7. #import "NSArray+NBAdditions.h"
  8. @implementation NBNumberFormat
  9. - (instancetype)initWithPattern:(NSString *)pattern
  10. withFormat:(NSString *)format
  11. withLeadingDigitsPatterns:(NSArray *)leadingDigitsPatterns
  12. withNationalPrefixFormattingRule:(NSString *)nationalPrefixFormattingRule
  13. whenFormatting:(BOOL)nationalPrefixOptionalWhenFormatting
  14. withDomesticCarrierCodeFormattingRule:(NSString *)domesticCarrierCodeFormattingRule {
  15. self = [super init];
  16. if (self) {
  17. _pattern = pattern;
  18. _format = format;
  19. _leadingDigitsPatterns = leadingDigitsPatterns;
  20. _nationalPrefixFormattingRule = nationalPrefixFormattingRule;
  21. _nationalPrefixOptionalWhenFormatting = nationalPrefixOptionalWhenFormatting;
  22. _domesticCarrierCodeFormattingRule = domesticCarrierCodeFormattingRule;
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithEntry:(NSArray *)entry {
  27. self = [super init];
  28. if (self && entry != nil) {
  29. _pattern = [entry nb_safeStringAtIndex:1];
  30. _format = [entry nb_safeStringAtIndex:2];
  31. _leadingDigitsPatterns = [entry nb_safeArrayAtIndex:3];
  32. _nationalPrefixFormattingRule = [entry nb_safeStringAtIndex:4];
  33. _nationalPrefixOptionalWhenFormatting = [[entry nb_safeNumberAtIndex:6] boolValue];
  34. _domesticCarrierCodeFormattingRule = [entry nb_safeStringAtIndex:5];
  35. }
  36. return self;
  37. }
  38. - (NSString *)description {
  39. return [NSString
  40. stringWithFormat:
  41. @"[pattern:%@, format:%@, leadingDigitsPattern:%@, nationalPrefixFormattingRule:%@, "
  42. @"nationalPrefixOptionalWhenFormatting:%@, domesticCarrierCodeFormattingRule:%@]",
  43. self.pattern, self.format, self.leadingDigitsPatterns, self.nationalPrefixFormattingRule,
  44. self.nationalPrefixOptionalWhenFormatting ? @"Y" : @"N",
  45. self.domesticCarrierCodeFormattingRule];
  46. }
  47. - (id)copyWithZone:(NSZone *)zone {
  48. return [[NBNumberFormat alloc] initWithPattern:self.pattern
  49. withFormat:self.format
  50. withLeadingDigitsPatterns:self.leadingDigitsPatterns
  51. withNationalPrefixFormattingRule:self.nationalPrefixFormattingRule
  52. whenFormatting:self.nationalPrefixOptionalWhenFormatting
  53. withDomesticCarrierCodeFormattingRule:self.domesticCarrierCodeFormattingRule];
  54. }
  55. #ifdef NB_USE_EXTENSIONS
  56. // We believe these methods are unused.
  57. // If you would like them back (not behind a flag) please file a bug with a reason for needing
  58. // them.
  59. - (instancetype)initWithCoder:(NSCoder *)coder {
  60. if (self = [super init]) {
  61. _pattern = [coder decodeObjectForKey:@"pattern"];
  62. _format = [coder decodeObjectForKey:@"format"];
  63. _leadingDigitsPatterns = [coder decodeObjectForKey:@"leadingDigitsPatterns"];
  64. _nationalPrefixFormattingRule = [coder decodeObjectForKey:@"nationalPrefixFormattingRule"];
  65. _nationalPrefixOptionalWhenFormatting =
  66. [[coder decodeObjectForKey:@"nationalPrefixOptionalWhenFormatting"] boolValue];
  67. _domesticCarrierCodeFormattingRule =
  68. [coder decodeObjectForKey:@"domesticCarrierCodeFormattingRule"];
  69. }
  70. return self;
  71. }
  72. - (void)encodeWithCoder:(NSCoder *)coder {
  73. [coder encodeObject:self.pattern forKey:@"pattern"];
  74. [coder encodeObject:self.format forKey:@"format"];
  75. [coder encodeObject:self.leadingDigitsPatterns forKey:@"leadingDigitsPatterns"];
  76. [coder encodeObject:self.nationalPrefixFormattingRule forKey:@"nationalPrefixFormattingRule"];
  77. [coder encodeObject:[NSNumber numberWithBool:self.nationalPrefixOptionalWhenFormatting]
  78. forKey:@"nationalPrefixOptionalWhenFormatting"];
  79. [coder encodeObject:self.domesticCarrierCodeFormattingRule
  80. forKey:@"domesticCarrierCodeFormattingRule"];
  81. }
  82. #endif // NB_USE_EXTENSIONS
  83. @end