-
Notifications
You must be signed in to change notification settings - Fork 483
Add default phone number to the Phone Auth number flow. #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7cb83f1
Add default phone number to the Phone Auth number flow.
yramanchuk 90e0a0d
Update code style. Fix phone number validation method.
yramanchuk 623c011
Update code style. Fix phone number validation method.
yramanchuk 6608ec1
Update signature of FUIPhoneNumber init methods.
yramanchuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// Copyright (c) 2016 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "FUICountryCodes.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
FOUNDATION_EXPORT NSString * const FUIPhoneNumberValidationErrorDomain; | ||
|
||
typedef NS_ENUM(NSInteger, FUIPhoneNumberValidationError) { | ||
FUIPhoneNumberValidationErrorMissingPlus = 0, | ||
FUIPhoneNumberValidationErrorMissingDialCode = 1, | ||
FUIPhoneNumberValidationErrorMissingNumber = 2, | ||
}; | ||
|
||
/** Encapsulates a phone number with the raw and the normalized representations */ | ||
@interface FUIPhoneNumber : NSObject | ||
|
||
@property (nonatomic, readonly) FUICountryCodeInfo *countryCode; | ||
@property (nonatomic, copy, readonly) NSString *rawPhoneNumber; | ||
@property (nonatomic, copy, readonly) NSString *normalizedPhoneNumber; | ||
|
||
/** @fn initWithNormalizedPhoneNumber: | ||
@brief Attempts to parse the given phone number into a raw phone number and country code. | ||
Parse behavior: | ||
If given phone number starts with a '+' character, then look for the country code matching | ||
the prefix of the number. | ||
Otherwise use the normalized number as the raw number, and find the country code using the | ||
device locale. | ||
@param normalizedPhoneNumber (required) A phone number string that will be parsed into | ||
a raw phone number and country code. | ||
@return object or nil if any of the required parameters is nil. | ||
*/ | ||
- (instancetype)initWithNormalizedPhoneNumber:(NSString *)normalizedPhoneNumber; | ||
|
||
/** @fn initWithRawPhoneNumber:countryCode: | ||
@param rawPhoneNumber (required) The raw phone number without country code | ||
@param countryCode (required) The country code information | ||
@return object or nil if any of the required parameters is nil. | ||
*/ | ||
- (instancetype)initWithRawPhoneNumber:(NSString *)rawPhoneNumber | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
countryCode:(FUICountryCodeInfo *)countryCode; | ||
|
||
/** @fn initWithNormalizedPhoneNumber:rawPhoneNumber:countryCode: | ||
@param normalizedPhoneNumber (optional) The phone number returned from the endpoint; | ||
if null or empty it will be computed ('+' + rawCountryCode + rawPhoneNumber) | ||
@param rawPhoneNumber (required) The raw phone number without country code | ||
@param countryCode (required) The country code information | ||
@return object or nil if any of the required parameters is nil. | ||
*/ | ||
- (instancetype)initWithNormalizedPhoneNumber:(NSString *)normalizedPhoneNumber | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
rawPhoneNumber:(NSString *)rawPhoneNumber | ||
countryCode:(FUICountryCodeInfo *)countryCode; | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
|
||
/** @fn validate: | ||
@brief Checks if current phone number has valid international format. | ||
@param errorRef The error which occurred, if any. | ||
@return True if phone number format is valid. | ||
*/ | ||
- (BOOL)validate:(NSError *__autoreleasing _Nullable *_Nullable)errorRef; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// Copyright (c) 2016 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import "FUIPhoneNumber.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
NSString * const FUIPhoneNumberValidationErrorDomain = @"FUIPhoneNumberValidationErrorDomain"; | ||
|
||
@implementation FUIPhoneNumber | ||
|
||
- (instancetype)initWithNormalizedPhoneNumber:(NSString *)normalizedPhoneNumber { | ||
NSAssert(normalizedPhoneNumber, @"normalizedPhoneNumber can't be nil"); | ||
NSString *rawPhoneNumber; | ||
FUICountryCodes *codes = [[FUICountryCodes alloc] init]; | ||
FUICountryCodeInfo *countryCode = [codes countryCodeInfoForPhoneNumber:normalizedPhoneNumber]; | ||
|
||
if (countryCode) { | ||
// Add 1 for the '+' character | ||
NSInteger countryCodeLength = countryCode.dialCode.length + 1; | ||
if (normalizedPhoneNumber.length >= countryCodeLength) { | ||
rawPhoneNumber = [normalizedPhoneNumber substringFromIndex:countryCodeLength]; | ||
} | ||
} | ||
if (!rawPhoneNumber) { | ||
rawPhoneNumber = normalizedPhoneNumber; | ||
countryCode = [codes countryCodeInfoFromDeviceLocale]; | ||
} | ||
return [self initWithRawPhoneNumber:rawPhoneNumber countryCode:countryCode]; | ||
} | ||
|
||
- (instancetype)initWithNormalizedPhoneNumber:(NSString *)normalizedPhoneNumber | ||
rawPhoneNumber:(NSString *)rawPhoneNumber | ||
countryCode:(FUICountryCodeInfo *)countryCode { | ||
NSAssert(normalizedPhoneNumber, @"normalizedPhoneNumber can't be nil"); | ||
NSAssert(rawPhoneNumber, @"rawPhoneNumber can't be nil"); | ||
NSAssert(countryCode, @"countryCode can't be nil"); | ||
if (self = [super init]) { | ||
_countryCode = countryCode; | ||
_rawPhoneNumber = rawPhoneNumber; | ||
_normalizedPhoneNumber = normalizedPhoneNumber; | ||
} | ||
return self; | ||
} | ||
|
||
- (instancetype)initWithRawPhoneNumber:(NSString *)rawPhoneNumber | ||
countryCode:(FUICountryCodeInfo *)countryCode { | ||
NSAssert(rawPhoneNumber, @"rawPhoneNumber can't be nil"); | ||
NSAssert(countryCode, @"countryCode can't be nil"); | ||
NSString *dialCode = countryCode.dialCode; | ||
NSAssert(dialCode.length, @"dialCode can't be empty"); | ||
if ([dialCode characterAtIndex:0] != '+') { | ||
dialCode = [@"+" stringByAppendingString:dialCode]; | ||
} | ||
NSString *normalizedPhoneNumber = [NSString stringWithFormat:@"%@%@", dialCode, rawPhoneNumber]; | ||
|
||
return [self initWithNormalizedPhoneNumber:normalizedPhoneNumber | ||
rawPhoneNumber:rawPhoneNumber | ||
countryCode:countryCode]; | ||
} | ||
|
||
- (BOOL)validate:(NSError *__autoreleasing _Nullable *_Nullable)errorRef { | ||
// The first character is always the '+' | ||
BOOL firstCharacterIsPlus = [_normalizedPhoneNumber characterAtIndex:0] == '+'; | ||
if (!firstCharacterIsPlus) { | ||
if (errorRef) { | ||
NSString *message = [NSString stringWithFormat:@"Phone number %@ should start with '+'", | ||
_normalizedPhoneNumber]; | ||
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : message }; | ||
*errorRef = [NSError errorWithDomain:FUIPhoneNumberValidationErrorDomain | ||
code:FUIPhoneNumberValidationErrorMissingPlus | ||
userInfo:userInfo]; | ||
} | ||
return false; | ||
} | ||
BOOL containsMoreThanThePlus = self.normalizedPhoneNumber.length > 1; | ||
if (!containsMoreThanThePlus) { | ||
if (errorRef) { | ||
NSString *message = [NSString stringWithFormat:@"Phone number %@ should have only one '+'", | ||
_normalizedPhoneNumber]; | ||
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : message }; | ||
*errorRef = [NSError errorWithDomain:FUIPhoneNumberValidationErrorDomain | ||
code:FUIPhoneNumberValidationErrorMissingDialCode | ||
userInfo:userInfo]; | ||
} | ||
return false; | ||
} | ||
BOOL containsMoreThanTheCountryCode = | ||
self.normalizedPhoneNumber.length > 1 + self.countryCode.dialCode.length; | ||
if (!containsMoreThanTheCountryCode) { | ||
if (errorRef) { | ||
NSString *message = | ||
[NSString stringWithFormat:@"Phone number %@ should have only one country code", | ||
_normalizedPhoneNumber]; | ||
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : message }; | ||
*errorRef = [NSError errorWithDomain:FUIPhoneNumberValidationErrorDomain | ||
code:FUIPhoneNumberValidationErrorMissingNumber | ||
userInfo:userInfo]; | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three initializers need to return
nullable instancetype
, notinstancetype
.