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 D3\LinkmobilityClient\Exceptions\ExceptionMessages;
|
|
|
|
use D3\LinkmobilityClient\Exceptions\RecipientException;
|
|
|
|
use libphonenumber\NumberParseException;
|
|
|
|
use libphonenumber\PhoneNumberFormat;
|
|
|
|
use libphonenumber\PhoneNumberUtil;
|
2022-06-20 14:47:33 +02:00
|
|
|
|
|
|
|
class Sender extends StringValueObject
|
|
|
|
{
|
2022-06-24 16:00:56 +02:00
|
|
|
/**
|
|
|
|
* @param string $number
|
|
|
|
* @param string $iso2CountryCode
|
|
|
|
*
|
|
|
|
* @throws RecipientException
|
2022-07-01 09:24:16 +02:00
|
|
|
* @throws NumberParseException
|
2022-06-24 16:00:56 +02:00
|
|
|
*/
|
2022-06-24 14:35:17 +02:00
|
|
|
public function __construct(string $number, string $iso2CountryCode)
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
2022-06-24 14:35:17 +02:00
|
|
|
Assert::that($iso2CountryCode)->string()->length(2);
|
|
|
|
|
2022-07-11 15:06:18 +02:00
|
|
|
$phoneUtil = $this->getPhoneNumberUtil();
|
2022-07-01 09:24:16 +02:00
|
|
|
|
2022-07-13 10:41:23 +02:00
|
|
|
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
|
2022-07-12 10:04:04 +02:00
|
|
|
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
|
2022-07-01 09:24:16 +02:00
|
|
|
|
|
|
|
if (false === $phoneUtil->isValidNumber($phoneNumber)) {
|
2022-07-13 10:41:23 +02:00
|
|
|
throw new RecipientException(ExceptionMessages::INVALID_SENDER);
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 10:41:23 +02:00
|
|
|
parent::__construct($number);
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
2022-07-11 15:06:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PhoneNumberUtil
|
|
|
|
*/
|
|
|
|
protected function getPhoneNumberUtil(): PhoneNumberUtil
|
|
|
|
{
|
|
|
|
return PhoneNumberUtil::getInstance();
|
|
|
|
}
|
2022-07-12 10:04:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getFormatted()
|
|
|
|
{
|
2022-07-12 13:17:56 +02:00
|
|
|
return parent::getFormatted();
|
2022-07-12 10:04:04 +02:00
|
|
|
}
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|