NBPhoneNumber.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // NBPhoneNumber.m
  3. // libPhoneNumber
  4. //
  5. //
  6. #import "NBPhoneNumber.h"
  7. #import "NBPhoneNumberDefines.h"
  8. @implementation NBPhoneNumber
  9. - (instancetype)init {
  10. self = [super init];
  11. if (self) {
  12. self.nationalNumber = @-1;
  13. self.countryCode = @-1;
  14. self.numberOfLeadingZeros = @(1);
  15. }
  16. return self;
  17. }
  18. - (void)clearCountryCodeSource {
  19. [self setCountryCodeSource:nil];
  20. }
  21. - (NBECountryCodeSource)getCountryCodeSourceOrDefault {
  22. if (nil == self.countryCodeSource) {
  23. return NBECountryCodeSourceFROM_NUMBER_WITH_PLUS_SIGN;
  24. }
  25. return [self.countryCodeSource integerValue];
  26. }
  27. - (NSUInteger)hash {
  28. // See https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
  29. NSUInteger hash = self.countryCode.hash;
  30. hash ^= self.nationalNumber.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
  31. hash ^= self.numberOfLeadingZeros.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
  32. hash ^= self.extension.hash + 0x9e3779b9 + (hash << 6) + (hash >> 2);
  33. return hash;
  34. }
  35. - (BOOL)isEqual:(id)object {
  36. if (![object isKindOfClass:[NBPhoneNumber class]]) {
  37. return NO;
  38. }
  39. NBPhoneNumber *other = object;
  40. return ([self.countryCode isEqualToNumber:other.countryCode]) &&
  41. ([self.nationalNumber isEqualToNumber:other.nationalNumber]) &&
  42. (self.italianLeadingZero == other.italianLeadingZero) &&
  43. ([self.numberOfLeadingZeros isEqualToNumber:other.numberOfLeadingZeros]) &&
  44. ((self.extension == nil && other.extension == nil) ||
  45. [self.extension isEqualToString:other.extension]);
  46. }
  47. - (id)copyWithZone:(NSZone *)zone {
  48. NBPhoneNumber *phoneNumberCopy = [[NBPhoneNumber allocWithZone:zone] init];
  49. phoneNumberCopy.countryCode = [self.countryCode copy];
  50. phoneNumberCopy.nationalNumber = [self.nationalNumber copy];
  51. phoneNumberCopy.extension = [self.extension copy];
  52. phoneNumberCopy.italianLeadingZero = self.italianLeadingZero;
  53. phoneNumberCopy.numberOfLeadingZeros = [self.numberOfLeadingZeros copy];
  54. phoneNumberCopy.rawInput = [self.rawInput copy];
  55. phoneNumberCopy.countryCodeSource = [self.countryCodeSource copy];
  56. phoneNumberCopy.preferredDomesticCarrierCode = [self.preferredDomesticCarrierCode copy];
  57. return phoneNumberCopy;
  58. }
  59. - (instancetype)initWithCoder:(NSCoder *)coder {
  60. if (self = [super init]) {
  61. self.countryCode = [coder decodeObjectForKey:@"countryCode"];
  62. self.nationalNumber = [coder decodeObjectForKey:@"nationalNumber"];
  63. self.extension = [coder decodeObjectForKey:@"extension"];
  64. self.italianLeadingZero = [[coder decodeObjectForKey:@"italianLeadingZero"] boolValue];
  65. self.numberOfLeadingZeros = [coder decodeObjectForKey:@"numberOfLeadingZeros"];
  66. self.rawInput = [coder decodeObjectForKey:@"rawInput"];
  67. self.countryCodeSource = [coder decodeObjectForKey:@"countryCodeSource"];
  68. self.preferredDomesticCarrierCode = [coder decodeObjectForKey:@"preferredDomesticCarrierCode"];
  69. }
  70. return self;
  71. }
  72. - (void)encodeWithCoder:(NSCoder *)coder {
  73. [coder encodeObject:self.countryCode forKey:@"countryCode"];
  74. [coder encodeObject:self.nationalNumber forKey:@"nationalNumber"];
  75. [coder encodeObject:self.extension forKey:@"extension"];
  76. [coder encodeObject:[NSNumber numberWithBool:self.italianLeadingZero]
  77. forKey:@"italianLeadingZero"];
  78. [coder encodeObject:self.numberOfLeadingZeros forKey:@"numberOfLeadingZeros"];
  79. [coder encodeObject:self.rawInput forKey:@"rawInput"];
  80. [coder encodeObject:self.countryCodeSource forKey:@"countryCodeSource"];
  81. [coder encodeObject:self.preferredDomesticCarrierCode forKey:@"preferredDomesticCarrierCode"];
  82. }
  83. - (NSString *)description {
  84. return [NSString stringWithFormat:
  85. @" - countryCode[%@], nationalNumber[%@], extension[%@], "
  86. @"italianLeadingZero[%@], numberOfLeadingZeros[%@], rawInput[%@] "
  87. @"countryCodeSource[%@] preferredDomesticCarrierCode[%@]",
  88. self.countryCode, self.nationalNumber, self.extension,
  89. self.italianLeadingZero ? @"Y" : @"N", self.numberOfLeadingZeros,
  90. self.rawInput, self.countryCodeSource, self.preferredDomesticCarrierCode];
  91. }
  92. @end