2022-06-20 14:47:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\LinkmobilityClient\ValueObject;
|
|
|
|
|
|
|
|
use Assert\Assert;
|
2022-06-24 16:00:56 +02:00
|
|
|
use libphonenumber\NumberParseException;
|
|
|
|
use libphonenumber\PhoneNumberFormat;
|
|
|
|
use libphonenumber\PhoneNumberUtil;
|
2022-06-20 14:47:33 +02:00
|
|
|
|
|
|
|
class Recipient extends StringValueObject
|
|
|
|
{
|
2022-06-24 14:35:17 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $countryCode;
|
2022-06-20 14:47:33 +02:00
|
|
|
|
2022-06-24 14:35:17 +02:00
|
|
|
public function __construct(string $number, string $iso2CountryCode)
|
|
|
|
{
|
|
|
|
Assert::that($iso2CountryCode)->string()->length(2);
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
$phoneUtil = PhoneNumberUtil::getInstance();
|
2022-06-24 14:35:17 +02:00
|
|
|
try {
|
|
|
|
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
|
2022-06-24 16:00:56 +02:00
|
|
|
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
|
|
|
|
} catch ( NumberParseException $e) {
|
2022-06-24 14:35:17 +02:00
|
|
|
var_dump($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($number);
|
|
|
|
$this->countryCode = $iso2CountryCode;
|
|
|
|
}
|
2022-06-20 14:47:33 +02:00
|
|
|
|
2022-06-24 14:35:17 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCountryCode() :string
|
|
|
|
{
|
|
|
|
return $this->countryCode;
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
|
|
|
}
|