From b17390bf79ca57c810c5f56c2d7dc9c1483d0845 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Sat, 21 Nov 2015 18:46:03 +0100 Subject: [PATCH] Adding fixed or mobile type and test --- PhoneNumberKit/Constants.swift | 2 ++ PhoneNumberKit/PhoneNumberParser.swift | 10 +++++++++- PhoneNumberKitTests/PhoneNumberKitTests.swift | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/PhoneNumberKit/Constants.swift b/PhoneNumberKit/Constants.swift index da91d2794..d96df2edc 100644 --- a/PhoneNumberKit/Constants.swift +++ b/PhoneNumberKit/Constants.swift @@ -59,6 +59,7 @@ public enum PNParsingError: ErrorType { - FixedLine: Fixed line numbers - Mobile: Mobile numbers + - FixedOrMobile: Either fixed or mobile numbers if we can't tell conclusively. - Pager: Pager numbers - PersonalNumber: Personal number numbers - PremiumRate: Premium rate numbers @@ -72,6 +73,7 @@ public enum PNParsingError: ErrorType { public enum PNPhoneNumberType { case FixedLine case Mobile + case FixedOrMobile case Pager case PersonalNumber case PremiumRate diff --git a/PhoneNumberKit/PhoneNumberParser.swift b/PhoneNumberKit/PhoneNumberParser.swift index 8ea93d97f..a43e2ab2d 100644 --- a/PhoneNumberKit/PhoneNumberParser.swift +++ b/PhoneNumberKit/PhoneNumberParser.swift @@ -113,7 +113,15 @@ class PhoneNumberParser { return .Unknown } if (isNumberMatchingDesc(nationalNumber, numberDesc: metadata.fixedLine)) { - return .FixedLine + if metadata.fixedLine?.nationalNumberPattern == metadata.mobile?.nationalNumberPattern { + return .FixedOrMobile + } + else if (isNumberMatchingDesc(nationalNumber, numberDesc: metadata.mobile)) { + return .FixedOrMobile + } + else { + return .FixedLine + } } if (isNumberMatchingDesc(nationalNumber, numberDesc: metadata.mobile)) { return .Mobile diff --git a/PhoneNumberKitTests/PhoneNumberKitTests.swift b/PhoneNumberKitTests/PhoneNumberKitTests.swift index fc9317992..4aeab20e6 100644 --- a/PhoneNumberKitTests/PhoneNumberKitTests.swift +++ b/PhoneNumberKitTests/PhoneNumberKitTests.swift @@ -31,6 +31,19 @@ class PhoneNumberKitTests: XCTestCase { XCTAssert(true) } } + + // Invalid american number, GitHub issue #9 by lobodin + func testAmbiguousFixedOrMobileNumber() { + do { + let phoneNumber = try PhoneNumber(rawNumber: "+16307792428", region: "US") + print(phoneNumber.toE164()) + XCTAssertEqual(phoneNumber.type, PNPhoneNumberType.FixedOrMobile) + } + catch { + XCTFail() + } + } + // Italian number with a leading zero func testItalianLeadingZero() {