Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
19 / 20
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
InternationalPhoneNumber
95.00% covered (success)
95.00%
19 / 20
75.00% covered (warning)
75.00%
3 / 4
6
0.00% covered (danger)
0.00%
0 / 1
 validate
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 convert
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 toPhoneNumber
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 createRandom
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\CountryAndPhoneNumber;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Attributes\ExampleValue;
6use Apie\Core\Attributes\FakeMethod;
7use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
8use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
9use Apie\Core\ValueObjects\IsStringValueObject;
10use Apie\CountryAndPhoneNumber\Factories\PhoneNumberFactory;
11use Faker\Generator;
12use libphonenumber\PhoneNumberFormat;
13use libphonenumber\PhoneNumberUtil;
14use PrinsFrank\Standards\Country\CountryAlpha2;
15use ReflectionClass;
16
17#[FakeMethod('createRandom')]
18#[Description('International phone number in E164 format, or example +31611223344.')]
19#[ExampleValue('+31611223344')]
20#[ExampleValue('+12025550123')]
21final 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}