Skip to content

Commit

Permalink
Merge pull request smartemailing#26 from smartemailing/remove-paramet…
Browse files Browse the repository at this point in the history
…er-exception

cleaned up phonenumber
  • Loading branch information
mstrouhal authored Sep 14, 2018
2 parents f079dd6 + e492bbf commit c173ab2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 40 deletions.
9 changes: 8 additions & 1 deletion src/Helpers/StringHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ final public static function sanitize(
final public static function removeWhitespace(
string $value
): string {
return (string) \preg_replace(
// removed whitespace
$value = (string) \preg_replace(
'/\s+/',
'',
$value
);
// removed unbreakable whitespace
return (string) \preg_replace(
'~\x{00a0}~',
'',
$value
);
}

final public static function normalizeWhitespace(
Expand Down
96 changes: 57 additions & 39 deletions src/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Consistence\Type\ObjectMixinTrait;
use Nette\Utils\Strings;
use SmartEmailing\Types\ExtractableTraits\StringExtractableTrait;
use SmartEmailing\Types\Helpers\StringHelpers;

final class PhoneNumber
{
Expand All @@ -18,27 +19,27 @@ final class PhoneNumber
/**
* @var int[]
*/
private static $phoneCodes = [
private static $countryCodesToPhoneCodes = [
CountryCode::SI => 386,
CountryCode::MH => 692,
CountryCode::CZ => 420,
CountryCode::SK => 421,
CountryCode::CY => 357,
CountryCode::IE => 353,
CountryCode::FI => 358,
CountryCode::LU => 352,
CountryCode::AT => 43,
CountryCode::BE => 32,
CountryCode::FR => 33,
CountryCode::HU => 36,
CountryCode::GB => 44,
CountryCode::DE => 49,
CountryCode::US => 1,
CountryCode::PL => 48,
CountryCode::IT => 39,
CountryCode::SE => 46,
CountryCode::SI => 386,
CountryCode::MH => 692,
CountryCode::NL => 31,
CountryCode::CY => 357,
CountryCode::IE => 353,
CountryCode::DK => 45,
CountryCode::FI => 358,
CountryCode::LU => 352,
CountryCode::US => 1,
];

/**
Expand Down Expand Up @@ -97,52 +98,69 @@ public function getValue(): string
private function initilize(
string $value
): bool {
$value = $this->preprocessValue(
$value = StringHelpers::removeWhitespace(
$value
);

foreach (self::$phoneCodes as $countryCode => $phoneCode) {

$prefix = '+' . $phoneCode;

if (!Strings::startsWith($value, $prefix)) {
continue;
}

$plainNumber = Strings::after($value, $prefix);

if (!\ctype_digit($plainNumber)) {
return false;
}

$plainNumberLength = Strings::length($value) - Strings::length($prefix);
$matchingCountryCodes = $this->getMatchingCountryCodes(
$value
);

foreach (self::$phoneNumberLengths[$countryCode] as $phoneNumberLength) {
if ($phoneNumberLength !== $plainNumberLength) {
continue;
}
foreach ($matchingCountryCodes as $matchingCountryCode) {
$match = $this->matchLengthForCountry(
$matchingCountryCode,
$value
);

$this->country = CountryCode::get($countryCode);
if ($match) {
$this->value = $value;
$this->country = CountryCode::from($matchingCountryCode);
return true;
}
}

return false;
}

private function preprocessValue(
private function matchLengthForCountry(
string $countryCode,
string $value
): string {
$value = (string) \preg_replace(
'/\s+/',
'',
$value
): bool {
$afterCountryCode = Strings::after(
$value,
(string) self::$countryCodesToPhoneCodes[$countryCode]
);
return (string) \preg_replace(
'~\x{00a0}~',
'',
$value

if (!\ctype_digit($afterCountryCode)) {
return false;
}

$lenght = Strings::length($afterCountryCode);

return \in_array(
$lenght,
self::$phoneNumberLengths[$countryCode],
true
);
}

/**
* @param string $value
* @return string[] matching country code constants
*/
private function getMatchingCountryCodes(
string $value
): array {
$matching = [];
foreach (self::$countryCodesToPhoneCodes as $countryCode => $phoneCode) {
$prefix = '+' . $phoneCode;
if (!Strings::startsWith($value, $prefix)) {
continue;
}

$matching[] = $countryCode;
}
return $matching;
}

}

0 comments on commit c173ab2

Please sign in to comment.