Created
May 31, 2018 08:54
-
-
Save alOneh/edf0ffebb060b9d6d9905bc99c9c67f3 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Faker\Provider; | |
use libphonenumber\NumberParseException; | |
use libphonenumber\PhoneNumber; | |
use libphonenumber\PhoneNumberUtil; | |
use libphonenumber\PhoneNumberFormat; | |
class PhoneNumberProvider | |
{ | |
/** | |
* @param string $value | |
* | |
* @return PhoneNumber | |
*/ | |
public static function createPhoneNumber(string $value = ''): PhoneNumber | |
{ | |
$util = PhoneNumberUtil::getInstance(); | |
if (!$value) { | |
$exampleNumber = $util->getExampleNumber(['US', 'FR', 'BE'][random_int(0, 2)]); | |
if (null === $exampleNumber) { | |
throw new \InvalidArgumentException('Unable to generate a fake phone number.'); | |
} | |
$value = $util->format($exampleNumber, PhoneNumberFormat::E164); | |
} | |
try { | |
return $util->parse($value, PhoneNumberUtil::UNKNOWN_REGION); | |
} catch (NumberParseException $e) { | |
throw new \InvalidArgumentException('PhoneNumberProvider : '.$e->getMessage().' in '.$value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment