From d6964f4ac0da421316d59d562753ff21cae9a886 Mon Sep 17 00:00:00 2001 From: Damien TUPINIER Date: Wed, 10 Jan 2024 16:54:02 +0100 Subject: [PATCH 1/2] Telephone: Implementation of PhoneNumber to manage the telephone number format --- composer.json | 5 +++-- src/Formatter/Property/TelephoneFormatter.php | 2 +- src/Property/Telephone.php | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index bbf457b..93f6f8e 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,11 @@ } ], "require": { - "php": ">=7.1" + "php": ">=7.1", + "brick/phonenumber": "^0.6.0" }, "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..31e6fa6 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); + + $this->phoneNumber = PhoneNumber::parse($telephoneNumber); + $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(); From ef6b1130160d40f9ce2af9a7ffa59ebda654633c Mon Sep 17 00:00:00 2001 From: Damien TUPINIER Date: Fri, 2 Feb 2024 07:31:30 +0100 Subject: [PATCH 2/2] Telephone: add region code of PhoneNumber --- composer.json | 3 ++- src/Property/Telephone.php | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 93f6f8e..ee2d54a 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ ], "require": { "php": ">=7.1", - "brick/phonenumber": "^0.6.0" + "brick/phonenumber": "^0.6.0", + "ext-intl": "*" }, "require-dev": { "phpunit/phpunit": "^10.5", diff --git a/src/Property/Telephone.php b/src/Property/Telephone.php index 31e6fa6..8b78c51 100644 --- a/src/Property/Telephone.php +++ b/src/Property/Telephone.php @@ -29,8 +29,8 @@ final class Telephone implements PropertyInterface, NodeInterface public function __construct(string $telephoneNumber, Type $type = null, Value $value = null) { - - $this->phoneNumber = PhoneNumber::parse($telephoneNumber); + $regionCode = \Locale::getRegion(\Locale::getDefault()); + $this->phoneNumber = PhoneNumber::parse($telephoneNumber, $regionCode); $this->telephoneNumber = $this->phoneNumber->format(PhoneNumberFormat::NATIONAL); $this->type = $type ?? Type::home();