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

65 lines
1.5 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 libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
2022-06-20 14:47:33 +02:00
class Recipient extends StringValueObject
{
/**
* @var string
*/
private $countryCode;
2022-06-20 14:47:33 +02:00
/**
* @param string $number
* @param string $iso2CountryCode
*
* @throws NumberParseException
*/
public function __construct(string $number, string $iso2CountryCode)
{
Assert::that($iso2CountryCode)->string()->length(2);
2022-07-11 15:06:18 +02:00
$phoneUtil = $this->getPhoneNumberUtil();
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
parent::__construct($number);
$this->countryCode = $iso2CountryCode;
}
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();
}
/**
* @return string
*/
2022-07-13 10:41:23 +02:00
public function getCountryCode(): string
{
return $this->countryCode;
2022-06-20 14:47:33 +02:00
}
}