Skip to content

Instantly share code, notes, and snippets.

@alOneh
Created May 31, 2018 08:54
Show Gist options
  • Save alOneh/edf0ffebb060b9d6d9905bc99c9c67f3 to your computer and use it in GitHub Desktop.
Save alOneh/edf0ffebb060b9d6d9905bc99c9c67f3 to your computer and use it in GitHub Desktop.
<?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