generated from descom-es/laravel-package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
<?php | ||
|
||
namespace DescomMarket\Common\Parsers; | ||
|
||
use Exception; | ||
use libphonenumber\PhoneNumberFormat; | ||
use libphonenumber\PhoneNumberUtil; | ||
|
||
final class MobileNumberParser | ||
{ | ||
private static $defaultRegion = 'ES'; | ||
|
||
public static function parse(string $mobile): ?string | ||
{ | ||
$phoneUtil = PhoneNumberUtil::getInstance(); | ||
|
||
try { | ||
$phoneNumber = $phoneUtil->parse($mobile, static::$defaultRegion); | ||
|
||
$mobileE164 = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164); | ||
|
||
if (method_exists(static::class, 'validate' . static::$defaultRegion)) { | ||
$validateMethod = 'validate' . static::$defaultRegion; | ||
|
||
if (! static::$validateMethod($mobileE164)) { | ||
return null; | ||
} | ||
} | ||
|
||
return $mobileE164; | ||
} catch (Exception $e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static function validateES(string $mobileE164): bool | ||
{ | ||
$length = strlen($mobileE164); | ||
|
||
if ($length !== 12) { | ||
return false; | ||
} | ||
|
||
$prefix = substr($mobileE164, 0, 4); | ||
|
||
if ($prefix !== '+346' && $prefix !== '+347') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains 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,43 @@ | ||
<?php | ||
|
||
namespace DescomMarket\Common\Tests\Feature\Parsers; | ||
|
||
use DescomMarket\Common\Parsers\MobileNumberParser; | ||
use DescomMarket\Common\Tests\TestCase; | ||
|
||
class MobileNumberParserTest extends TestCase | ||
{ | ||
public function testParseValid() | ||
{ | ||
$test = collect([ | ||
'666666666' => '+34666666666', | ||
'766666666' => '+34766666666', | ||
'666 666 666' => '+34666666666', | ||
'34666-666-666' => '+34666666666', | ||
'+34666 666-666' => '+34666666666', | ||
'0034666-666 666' => '+34666666666', | ||
]); | ||
|
||
$test->each(function ($expected, $mobile) { | ||
$parsedMobile = MobileNumberParser::parse($mobile); | ||
|
||
$this->assertEquals($expected, $parsedMobile); | ||
}); | ||
} | ||
|
||
public function testParseInvalid() | ||
{ | ||
$test = collect([ | ||
'aa', | ||
'61234567', | ||
'6123456789', | ||
'812345678', | ||
]); | ||
|
||
$test->each(function ($mobile) { | ||
$parsedMobile = MobileNumberParser::parse($mobile); | ||
|
||
$this->assertNull($parsedMobile); | ||
}); | ||
} | ||
} |