linkmobility-php-client/src/ValueObject/Sender.php

75 lines
2.2 KiB
PHP
Raw Normal View History

2022-06-20 14:47:33 +02:00
<?php
2022-07-13 10:50:45 +02:00
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
2022-06-20 14:47:33 +02:00
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\NoSenderDefinedException;
2022-06-24 16:00:56 +02:00
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\LoggerHandler;
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 Sender extends ValueObject
2022-06-20 14:47:33 +02:00
{
2022-06-24 16:00:56 +02:00
/**
* @param string|null $number
* @param string|null $iso2CountryCode
2022-06-24 16:00:56 +02:00
*
* @throws NumberParseException
* @throws RecipientException
2022-06-24 16:00:56 +02:00
*/
public function __construct(string $number = null, string $iso2CountryCode = null)
2022-06-20 14:47:33 +02:00
{
try {
2022-07-14 12:07:18 +02:00
if (is_null($number) || is_null($iso2CountryCode)) {
throw new NoSenderDefinedException();
}
2022-07-14 12:07:18 +02:00
Assert::that($iso2CountryCode)->string()->length(2);
$phoneUtil = $this->getPhoneNumberUtil();
2022-07-14 12:07:18 +02:00
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164);
2022-07-14 12:07:18 +02:00
if (false === $phoneUtil->isValidNumber($phoneNumber)) {
throw new RecipientException(ExceptionMessages::INVALID_SENDER);
}
2022-06-20 14:47:33 +02:00
2022-07-14 12:07:18 +02:00
parent::__construct($number);
} catch (NoSenderDefinedException $e) {
LoggerHandler::getInstance()->getLogger()->debug(
ExceptionMessages::DEBUG_NOSENDERORCOUNTRYCODE
);
}
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();
}
public function getFormatted(): string
{
return ltrim(parent::getFormatted(), '+');
}
2022-06-20 14:47:33 +02:00
}