Repository: marmelroy/PhoneNumberKit (swift)

Issue:
Parsing doesn't work properly updating from 3.5.10 to 3.6.1
### New Issue Checklist

- [x] Updated PhoneNumberKit to the latest version / Specified the podspec directly from the repo
- [X] Phone number formatted correctly on [JavaScript version](https://htmlpreview.github.io/?https://github.com/google/libphonenumber/blob/master/javascript/i18n/phonenumbers/demo-compiled.html)
- [x] I searched for [existing GitHub issues](https://github.com/marmelroy/PhoneNumberKit)

### Steps to reproduce
```swift
let kit = PhoneNumberKit()
let kitNumber = try kit.parse("420777666555", withRegion: "CZ")
let formattedNumber = kit.format(kitNumber, toType: .international)
XCTAssertEqual(formattedNumber, "+420 777 666 555")
```

##### Expected result

Assert succeed

##### Actual result

It fails with: `PhoneNumberKit.PhoneNumberError.invalidNumber`

### Environment

- Integration through SPM - version `3.6.1`
- Xcode 14.3.1


### Notes

- Our tests caught it - it worked properly on `3.5.10`

Pull request description:
Fix #660

Patch:
diff --git a/PhoneNumberKit/PhoneNumberKit.swift b/PhoneNumberKit/PhoneNumberKit.swift
index 4958fedda..811847a03 100644
--- a/PhoneNumberKit/PhoneNumberKit.swift
+++ b/PhoneNumberKit/PhoneNumberKit.swift
@@ -36,7 +36,19 @@ public final class PhoneNumberKit: NSObject {
     ///   - ignoreType: Avoids number type checking for faster performance.
     /// - Returns: PhoneNumber object.
     public func parse(_ numberString: String, withRegion region: String = PhoneNumberKit.defaultRegionCode(), ignoreType: Bool = false) throws -> PhoneNumber {
-        return try self.parseManager.parse(numberString, withRegion: region, ignoreType: ignoreType)
+        let region = region.uppercased()
+        do {
+            return try self.parseManager.parse(numberString, withRegion: region, ignoreType: ignoreType)
+        } catch {
+            guard numberString.first != "+", let regionMetadata = metadataManager.filterTerritories(byCountry: region) else {
+                throw error
+            }
+            let countryCode = String(regionMetadata.countryCode)
+            guard numberString.prefix(countryCode.count) == countryCode else {
+                throw error
+            }
+            return try self.parse("+\(numberString)", withRegion: region, ignoreType: ignoreType)
+        }
     }
 
     /// Parses an array of number strings. Optimised for performance. Invalid numbers are ignored in the resulting array


Test patch:
diff --git a/PhoneNumberKitTests/PhoneNumberKitTests.swift b/PhoneNumberKitTests/PhoneNumberKitTests.swift
index 23cc39668..69012bcf0 100644
--- a/PhoneNumberKitTests/PhoneNumberKitTests.swift
+++ b/PhoneNumberKitTests/PhoneNumberKitTests.swift
@@ -457,4 +457,9 @@ class PhoneNumberKitTests: XCTestCase {
             }
         }
     }
+    
+    func testValidCZNumbers() {
+        let numbers = ["420734593819", "+420734593819", "734593819"]
+        numbers.forEach { XCTAssertTrue(phoneNumberKit.isValidPhoneNumber($0, withRegion: "CZ")) }
+    }
 }