diff --git a/composer.json b/composer.json index bbf457b..ee2d54a 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,12 @@ } ], "require": { - "php": ">=7.1" + "php": ">=7.1", + "brick/phonenumber": "^0.6.0", + "ext-intl": "*" }, "require-dev": { - "phpunit/phpunit": "^6.5", + "phpunit/phpunit": "^10.5", "squizlabs/php_codesniffer": "^3.2", "mikey179/vfsstream": "^1.6" }, diff --git a/src/Formatter/Property/TelephoneFormatter.php b/src/Formatter/Property/TelephoneFormatter.php index ff5700f..8c9752e 100644 --- a/src/Formatter/Property/TelephoneFormatter.php +++ b/src/Formatter/Property/TelephoneFormatter.php @@ -20,6 +20,6 @@ public function getVcfString(): string { return $this->telephone->getNode() . ';TYPE=' . $this->telephone->getType()->__toString() . - ';VALUE=' . $this->telephone->getValue() . ':tel:' . $this->telephone->getTelephoneNumber(); + ';VALUE=' . $this->telephone->getValue() . ':' . $this->telephone->getTelephoneNumberURI(); } } diff --git a/src/Property/Telephone.php b/src/Property/Telephone.php index 379df23..8b78c51 100644 --- a/src/Property/Telephone.php +++ b/src/Property/Telephone.php @@ -4,6 +4,8 @@ namespace JeroenDesloovere\VCard\Property; +use Brick\PhoneNumber\PhoneNumber; +use Brick\PhoneNumber\PhoneNumberFormat; use JeroenDesloovere\VCard\Formatter\Property\NodeFormatterInterface; use JeroenDesloovere\VCard\Formatter\Property\TelephoneFormatter; use JeroenDesloovere\VCard\Parser\Property\NodeParserInterface; @@ -16,6 +18,9 @@ final class Telephone implements PropertyInterface, NodeInterface /** @var string */ protected $telephoneNumber; + /** @var PhoneNumber */ + protected $phoneNumber; + /** @var Type */ private $type; @@ -24,7 +29,10 @@ final class Telephone implements PropertyInterface, NodeInterface public function __construct(string $telephoneNumber, Type $type = null, Value $value = null) { - $this->telephoneNumber = str_replace(' ', '-', $telephoneNumber); + $regionCode = \Locale::getRegion(\Locale::getDefault()); + $this->phoneNumber = PhoneNumber::parse($telephoneNumber, $regionCode); + $this->telephoneNumber = $this->phoneNumber->format(PhoneNumberFormat::NATIONAL); + $this->type = $type ?? Type::home(); $this->value = $value ?? Value::uri(); } @@ -49,6 +57,16 @@ public function getTelephoneNumber(): string return $this->telephoneNumber; } + public function getTelephoneNumberURI(): string + { + return $this->phoneNumber->format(PhoneNumberFormat::RFC3966); + } + + public function getPhoneNumber(): PhoneNumber + { + return $this->phoneNumber; + } + public static function getParser(): NodeParserInterface { return new TelephoneParser();