Skip to content

Commit

Permalink
Changing extension to avoid breaking contract
Browse files Browse the repository at this point in the history
  • Loading branch information
wallymathieu committed Sep 21, 2018
1 parent 919679f commit 482fb94
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
43 changes: 2 additions & 41 deletions csharp/PhoneNumbers/LeniencyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,7 @@ namespace PhoneNumbers
{
public static class LeniencyExtensions
{
public static bool Verify(this Leniency leniency, PhoneNumber number, string candidate, PhoneNumberUtil util)
{
switch (leniency)
{
case Leniency.POSSIBLE:
return util.IsPossibleNumber(number);
case Leniency.VALID:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util))
return false;
return PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util);
}
case Leniency.STRICT_GROUPING:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
!PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
{
return false;
}
return PhoneNumberMatcher.CheckNumberGroupingIsValid(
number, candidate, util, PhoneNumberMatcher.AllNumberGroupsRemainGrouped);
}
case Leniency.EXACT_GROUPING:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
!PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
{
return false;
}
return PhoneNumberMatcher.CheckNumberGroupingIsValid(
number, candidate, util, PhoneNumberMatcher.AllNumberGroupsAreExactlyPresent);
}
default:
throw new ArgumentOutOfRangeException(nameof(leniency), leniency, null);
}
}
public static bool Verify(this Leniency leniency, PhoneNumber number, string candidate, PhoneNumberUtil util)=>
util.Verify(leniency, number, candidate, util);
}
}
45 changes: 44 additions & 1 deletion csharp/PhoneNumbers/PhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace PhoneNumbers
{
Expand Down Expand Up @@ -480,6 +482,47 @@ public enum Leniency
EXACT_GROUPING
}

public bool Verify(Leniency leniency, PhoneNumber number, string candidate, PhoneNumberUtil util)
{
switch (leniency)
{
case Leniency.POSSIBLE:
return IsPossibleNumber(number);
case Leniency.VALID:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util))
return false;
return PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util);
}
case Leniency.STRICT_GROUPING:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
!PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
{
return false;
}
return PhoneNumberMatcher.CheckNumberGroupingIsValid(
number, candidate, util, PhoneNumberMatcher.AllNumberGroupsRemainGrouped);
}
case Leniency.EXACT_GROUPING:
{
if (!util.IsValidNumber(number) ||
!PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
!PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
{
return false;
}
return PhoneNumberMatcher.CheckNumberGroupingIsValid(
number, candidate, util, PhoneNumberMatcher.AllNumberGroupsAreExactlyPresent);
}
default:
throw new ArgumentOutOfRangeException(nameof(leniency), leniency, null);
}
}

// This class implements a singleton, so the only constructor is private.
private PhoneNumberUtil()
Expand Down Expand Up @@ -3296,4 +3339,4 @@ public bool CanBeInternationallyDialled(PhoneNumber number)
return !IsNumberMatchingDesc(nationalSignificantNumber, metadata.NoInternationalDialling);
}
}
}
}

0 comments on commit 482fb94

Please sign in to comment.