Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CanCreateRandomPhoneNumber | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |
0.00% |
0 / 1 |
| fromCountry | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getUtil | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createRandomInstance | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | namespace Apie\CountryAndPhoneNumber\Concerns; |
| 3 | |
| 4 | use libphonenumber\PhoneNumberFormat; |
| 5 | use libphonenumber\PhoneNumberUtil; |
| 6 | use LogicException; |
| 7 | use PrinsFrank\Standards\Country\CountryAlpha2; |
| 8 | use RegRev\RegRev; |
| 9 | |
| 10 | trait CanCreateRandomPhoneNumber |
| 11 | { |
| 12 | abstract public static function fromCountry(): CountryAlpha2; |
| 13 | abstract protected static function getUtil(): PhoneNumberUtil; |
| 14 | |
| 15 | public static function createRandomInstance(): static |
| 16 | { |
| 17 | $phoneNumberUtil = self::getUtil(); |
| 18 | $country = self::fromCountry(); |
| 19 | |
| 20 | $metadata = $phoneNumberUtil->getMetadataForRegion($country->value); |
| 21 | if ($metadata) { |
| 22 | $pattern = $metadata->getFixedLine()->getNationalNumberPattern(); |
| 23 | if ($pattern) { |
| 24 | return new static(RegRev::generate('^' . str_replace('?:', '', $pattern) . '$')); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | $phoneNumberObject = $phoneNumberUtil->getExampleNumber($country->value); |
| 29 | if ($phoneNumberObject) { |
| 30 | return new static($phoneNumberUtil->format($phoneNumberObject, PhoneNumberFormat::E164)); |
| 31 | } |
| 32 | throw new LogicException('I have no logic to create a fake phone number for ' . __CLASS__); |
| 33 | } |
| 34 | } |