Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| InternationalPhoneNumber | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
6 | |
0.00% |
0 / 1 |
| validate | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| convert | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| toPhoneNumber | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createRandom | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\CountryAndPhoneNumber; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\ExampleValue; |
| 6 | use Apie\Core\Attributes\FakeMethod; |
| 7 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 8 | use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface; |
| 9 | use Apie\Core\ValueObjects\IsStringValueObject; |
| 10 | use Apie\CountryAndPhoneNumber\Factories\PhoneNumberFactory; |
| 11 | use Faker\Generator; |
| 12 | use libphonenumber\PhoneNumberFormat; |
| 13 | use libphonenumber\PhoneNumberUtil; |
| 14 | use PrinsFrank\Standards\Country\CountryAlpha2; |
| 15 | use ReflectionClass; |
| 16 | |
| 17 | #[FakeMethod('createRandom')] |
| 18 | #[Description('International phone number in E164 format, or example +31611223344.')] |
| 19 | #[ExampleValue('+31611223344')] |
| 20 | #[ExampleValue('+12025550123')] |
| 21 | final class InternationalPhoneNumber implements StringValueObjectInterface |
| 22 | { |
| 23 | use IsStringValueObject; |
| 24 | |
| 25 | public static function validate(string $input): void |
| 26 | { |
| 27 | $phoneUtil = PhoneNumberUtil::getInstance(); |
| 28 | if (!$phoneUtil->isPossibleNumber($input)) { |
| 29 | throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | protected function convert(string $input): string |
| 34 | { |
| 35 | $phoneUtil = PhoneNumberUtil::getInstance(); |
| 36 | $phone = $phoneUtil->parse($input); |
| 37 | return $phoneUtil->format($phone, PhoneNumberFormat::E164); |
| 38 | } |
| 39 | |
| 40 | public function toPhoneNumber(): PhoneNumber |
| 41 | { |
| 42 | $phoneUtil = PhoneNumberUtil::getInstance(); |
| 43 | $phone = $phoneUtil->parse($this->internal); |
| 44 | return PhoneNumberFactory::createFrom( |
| 45 | $this->internal, |
| 46 | CountryAlpha2::from($phoneUtil->getRegionCodeForNumber($phone)) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public static function createRandom(Generator $generator): self |
| 51 | { |
| 52 | $phoneNumber = ''; |
| 53 | do { |
| 54 | $country = $generator->randomElement(CountryAlpha2::cases()); |
| 55 | $phoneNumberUtil = PhoneNumberUtil::getInstance(); |
| 56 | $phoneNumberObject = $phoneNumberUtil->getExampleNumber($country->value); |
| 57 | if ($phoneNumberObject) { |
| 58 | $phoneNumber = $phoneNumberUtil->format($phoneNumberObject, PhoneNumberFormat::E164); |
| 59 | } |
| 60 | } while ($phoneNumber === ''); |
| 61 | return new InternationalPhoneNumber($phoneNumber); |
| 62 | } |
| 63 | } |