From 1d972f31ccd3efb11dbfb9a13bc43613bdf8770a Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 20 Mar 2024 19:25:06 +0200 Subject: [PATCH 01/12] getCountryCode() --- composer.json | 4 +- composer.lock | 274 +++++++++++++++++++++++++------ src/Utopia/Messaging/Adapter.php | 31 +++- 3 files changed, 254 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index 557c913..ef3c9b4 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,9 @@ "php": ">=8.0.0", "ext-curl": "*", "ext-openssl": "*", - "phpmailer/phpmailer": "6.9.1" + "phpmailer/phpmailer": "6.9.1", + "giggsey/libphonenumber-for-php-lite": "8.13.30" + }, "require-dev": { "phpunit/phpunit": "10.5.10", diff --git a/composer.lock b/composer.lock index af48473..d674d53 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,90 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "618005e66f0fcc6b1ddf653a1864845b", + "content-hash": "a3489a7fb8a6667e3c9775bcbc783ecf", "packages": [ + { + "name": "giggsey/libphonenumber-for-php-lite", + "version": "8.13.30", + "source": { + "type": "git", + "url": "https://github.com/giggsey/libphonenumber-for-php-lite.git", + "reference": "7b205dac7d90a791f7bbae77f2ced2c90b0c5df1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/giggsey/libphonenumber-for-php-lite/zipball/7b205dac7d90a791f7bbae77f2ced2c90b0c5df1", + "reference": "7b205dac7d90a791f7bbae77f2ced2c90b0c5df1", + "shasum": "" + }, + "require": { + "php": "^8.0", + "symfony/polyfill-mbstring": "^1.17" + }, + "conflict": { + "giggsey/libphonenumber-for-php": "*" + }, + "require-dev": { + "ext-dom": "*", + "friendsofphp/php-cs-fixer": "^3.12", + "infection/infection": "^0.26.16", + "pear/pear-core-minimal": "^1.10.11", + "pear/pear_exception": "^1.0.2", + "pear/versioncontrol_git": "^0.5", + "phing/phing": "^2.17.4", + "phpstan/extension-installer": "^1.2", + "phpstan/phpstan": "^1.8", + "phpstan/phpstan-phpunit": "^1.2", + "phpunit/phpunit": "^9.5.26", + "symfony/console": "^6.0" + }, + "suggest": { + "giggsey/libphonenumber-for-php": "Use libphonenumber-for-php for geocoding, carriers, timezones and matching" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "libphonenumber\\": "src/" + }, + "exclude-from-classmap": [ + "/src/data/", + "/src/carrier/data/", + "/src/geocoding/data/", + "/src/timezone/data/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Joshua Gigg", + "email": "giggsey@gmail.com", + "homepage": "https://giggsey.com/" + } + ], + "description": "A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber", + "homepage": "https://github.com/giggsey/libphonenumber-for-php-lite", + "keywords": [ + "geocoding", + "geolocation", + "libphonenumber", + "mobile", + "phonenumber", + "validation" + ], + "support": { + "issues": "https://github.com/giggsey/libphonenumber-for-php-lite/issues", + "source": "https://github.com/giggsey/libphonenumber-for-php-lite" + }, + "time": "2024-02-09T12:16:33+00:00" + }, { "name": "phpmailer/phpmailer", "version": "v6.9.1", @@ -86,6 +168,86 @@ } ], "time": "2023-11-25T22:23:28+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.29.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9773676c8a1bb1f8d4340a62efe641cf76eda7ec", + "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.29.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-01-29T20:11:03+00:00" } ], "packages-dev": [ @@ -216,16 +378,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.0.0", + "version": "v5.0.2", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc" + "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/4a21235f7e56e713259a6f76bf4b5ea08502b9dc", - "reference": "4a21235f7e56e713259a6f76bf4b5ea08502b9dc", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", + "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", "shasum": "" }, "require": { @@ -268,26 +430,27 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" }, - "time": "2024-01-07T17:17:35+00:00" + "time": "2024-03-05T20:51:40+00:00" }, { "name": "phar-io/manifest", - "version": "2.0.3", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/phar-io/manifest.git", - "reference": "97803eca37d319dfa7826cc2437fc020857acb53" + "reference": "54750ef60c58e43759730615a392c31c80e23176" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phar-io/manifest/zipball/97803eca37d319dfa7826cc2437fc020857acb53", - "reference": "97803eca37d319dfa7826cc2437fc020857acb53", + "url": "https://api.github.com/repos/phar-io/manifest/zipball/54750ef60c58e43759730615a392c31c80e23176", + "reference": "54750ef60c58e43759730615a392c31c80e23176", "shasum": "" }, "require": { "ext-dom": "*", + "ext-libxml": "*", "ext-phar": "*", "ext-xmlwriter": "*", "phar-io/version": "^3.0.1", @@ -328,9 +491,15 @@ "description": "Component for reading phar.io manifest information from a PHP Archive (PHAR)", "support": { "issues": "https://github.com/phar-io/manifest/issues", - "source": "https://github.com/phar-io/manifest/tree/2.0.3" + "source": "https://github.com/phar-io/manifest/tree/2.0.4" }, - "time": "2021-07-20T11:28:43+00:00" + "funding": [ + { + "url": "https://github.com/theseer", + "type": "github" + } + ], + "time": "2024-03-03T12:33:53+00:00" }, { "name": "phar-io/version", @@ -447,16 +616,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "10.1.11", + "version": "10.1.12", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "78c3b7625965c2513ee96569a4dbb62601784145" + "reference": "842f72662d6b9edda84c4b6f13885fd9cd53dc63" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/78c3b7625965c2513ee96569a4dbb62601784145", - "reference": "78c3b7625965c2513ee96569a4dbb62601784145", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/842f72662d6b9edda84c4b6f13885fd9cd53dc63", + "reference": "842f72662d6b9edda84c4b6f13885fd9cd53dc63", "shasum": "" }, "require": { @@ -513,7 +682,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.11" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.12" }, "funding": [ { @@ -521,7 +690,7 @@ "type": "github" } ], - "time": "2023-12-21T15:38:30+00:00" + "time": "2024-03-02T07:22:05+00:00" }, { "name": "phpunit/php-file-iterator", @@ -869,16 +1038,16 @@ }, { "name": "sebastian/cli-parser", - "version": "2.0.0", + "version": "2.0.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/cli-parser.git", - "reference": "efdc130dbbbb8ef0b545a994fd811725c5282cae" + "reference": "c34583b87e7b7a8055bf6c450c2c77ce32a24084" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/efdc130dbbbb8ef0b545a994fd811725c5282cae", - "reference": "efdc130dbbbb8ef0b545a994fd811725c5282cae", + "url": "https://api.github.com/repos/sebastianbergmann/cli-parser/zipball/c34583b87e7b7a8055bf6c450c2c77ce32a24084", + "reference": "c34583b87e7b7a8055bf6c450c2c77ce32a24084", "shasum": "" }, "require": { @@ -913,7 +1082,8 @@ "homepage": "https://github.com/sebastianbergmann/cli-parser", "support": { "issues": "https://github.com/sebastianbergmann/cli-parser/issues", - "source": "https://github.com/sebastianbergmann/cli-parser/tree/2.0.0" + "security": "https://github.com/sebastianbergmann/cli-parser/security/policy", + "source": "https://github.com/sebastianbergmann/cli-parser/tree/2.0.1" }, "funding": [ { @@ -921,7 +1091,7 @@ "type": "github" } ], - "time": "2023-02-03T06:58:15+00:00" + "time": "2024-03-02T07:12:49+00:00" }, { "name": "sebastian/code-unit", @@ -1171,16 +1341,16 @@ }, { "name": "sebastian/diff", - "version": "5.1.0", + "version": "5.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "fbf413a49e54f6b9b17e12d900ac7f6101591b7f" + "reference": "c41e007b4b62af48218231d6c2275e4c9b975b2e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/fbf413a49e54f6b9b17e12d900ac7f6101591b7f", - "reference": "fbf413a49e54f6b9b17e12d900ac7f6101591b7f", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/c41e007b4b62af48218231d6c2275e4c9b975b2e", + "reference": "c41e007b4b62af48218231d6c2275e4c9b975b2e", "shasum": "" }, "require": { @@ -1188,7 +1358,7 @@ }, "require-dev": { "phpunit/phpunit": "^10.0", - "symfony/process": "^4.2 || ^5" + "symfony/process": "^6.4" }, "type": "library", "extra": { @@ -1226,7 +1396,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", "security": "https://github.com/sebastianbergmann/diff/security/policy", - "source": "https://github.com/sebastianbergmann/diff/tree/5.1.0" + "source": "https://github.com/sebastianbergmann/diff/tree/5.1.1" }, "funding": [ { @@ -1234,7 +1404,7 @@ "type": "github" } ], - "time": "2023-12-22T10:55:06+00:00" + "time": "2024-03-02T07:15:17+00:00" }, { "name": "sebastian/environment", @@ -1302,16 +1472,16 @@ }, { "name": "sebastian/exporter", - "version": "5.1.1", + "version": "5.1.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "64f51654862e0f5e318db7e9dcc2292c63cdbddc" + "reference": "955288482d97c19a372d3f31006ab3f37da47adf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/64f51654862e0f5e318db7e9dcc2292c63cdbddc", - "reference": "64f51654862e0f5e318db7e9dcc2292c63cdbddc", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/955288482d97c19a372d3f31006ab3f37da47adf", + "reference": "955288482d97c19a372d3f31006ab3f37da47adf", "shasum": "" }, "require": { @@ -1368,7 +1538,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", "security": "https://github.com/sebastianbergmann/exporter/security/policy", - "source": "https://github.com/sebastianbergmann/exporter/tree/5.1.1" + "source": "https://github.com/sebastianbergmann/exporter/tree/5.1.2" }, "funding": [ { @@ -1376,20 +1546,20 @@ "type": "github" } ], - "time": "2023-09-24T13:22:09+00:00" + "time": "2024-03-02T07:17:12+00:00" }, { "name": "sebastian/global-state", - "version": "6.0.1", + "version": "6.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "7ea9ead78f6d380d2a667864c132c2f7b83055e4" + "reference": "987bafff24ecc4c9ac418cab1145b96dd6e9cbd9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/7ea9ead78f6d380d2a667864c132c2f7b83055e4", - "reference": "7ea9ead78f6d380d2a667864c132c2f7b83055e4", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/987bafff24ecc4c9ac418cab1145b96dd6e9cbd9", + "reference": "987bafff24ecc4c9ac418cab1145b96dd6e9cbd9", "shasum": "" }, "require": { @@ -1423,14 +1593,14 @@ } ], "description": "Snapshotting of global state", - "homepage": "http://www.github.com/sebastianbergmann/global-state", + "homepage": "https://www.github.com/sebastianbergmann/global-state", "keywords": [ "global state" ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", "security": "https://github.com/sebastianbergmann/global-state/security/policy", - "source": "https://github.com/sebastianbergmann/global-state/tree/6.0.1" + "source": "https://github.com/sebastianbergmann/global-state/tree/6.0.2" }, "funding": [ { @@ -1438,7 +1608,7 @@ "type": "github" } ], - "time": "2023-07-19T07:19:23+00:00" + "time": "2024-03-02T07:19:19+00:00" }, { "name": "sebastian/lines-of-code", @@ -1784,16 +1954,16 @@ }, { "name": "theseer/tokenizer", - "version": "1.2.2", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96" + "reference": "737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/b2ad5003ca10d4ee50a12da31de12a5774ba6b96", - "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2", + "reference": "737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2", "shasum": "" }, "require": { @@ -1822,7 +1992,7 @@ "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", "support": { "issues": "https://github.com/theseer/tokenizer/issues", - "source": "https://github.com/theseer/tokenizer/tree/1.2.2" + "source": "https://github.com/theseer/tokenizer/tree/1.2.3" }, "funding": [ { @@ -1830,7 +2000,7 @@ "type": "github" } ], - "time": "2023-11-20T00:12:19+00:00" + "time": "2024-03-03T12:36:25+00:00" } ], "aliases": [], @@ -1847,5 +2017,5 @@ "platform-overrides": { "php": "8.2" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.2.0" } diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 5b29355..f6bd088 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -2,6 +2,11 @@ namespace Utopia\Messaging; + +use Exception; +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumberUtil; + abstract class Adapter { /** @@ -139,7 +144,7 @@ protected function request( * error: string|null * }> * - * @throws \Exception + * @throws Exception */ protected function requestMulti( string $method, @@ -248,4 +253,26 @@ protected function requestMulti( return $responses; } -} + + + /** + * @throws Exception + */ + public function getCountryCode(string $phone): string + { + if (empty($phone)) { + throw new Exception('No phone number was passed.'); + } + + $helper = PhoneNumberUtil::getInstance(); + + try { + return $helper + ->parse($phone) + ->getCountryCode(); + + } catch (NumberParseException $e) { + throw new Exception("Error parsing phone: " . $e->getMessage()); + } + } +} \ No newline at end of file From efe30a5d1fc051d10678b3f7d534127f0901cbf5 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:23:52 +0300 Subject: [PATCH 02/12] getCountryCode() --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index d674d53..a961c55 100644 --- a/composer.lock +++ b/composer.lock @@ -616,16 +616,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "10.1.12", + "version": "10.1.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "842f72662d6b9edda84c4b6f13885fd9cd53dc63" + "reference": "e3f51450ebffe8e0efdf7346ae966a656f7d5e5b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/842f72662d6b9edda84c4b6f13885fd9cd53dc63", - "reference": "842f72662d6b9edda84c4b6f13885fd9cd53dc63", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/e3f51450ebffe8e0efdf7346ae966a656f7d5e5b", + "reference": "e3f51450ebffe8e0efdf7346ae966a656f7d5e5b", "shasum": "" }, "require": { @@ -682,7 +682,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.12" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.14" }, "funding": [ { @@ -690,7 +690,7 @@ "type": "github" } ], - "time": "2024-03-02T07:22:05+00:00" + "time": "2024-03-12T15:33:41+00:00" }, { "name": "phpunit/php-file-iterator", From 6b0f79d7daf010224ddd6e24b5e61c2dbec2ba8a Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:26:48 +0300 Subject: [PATCH 03/12] getCountryCode() --- src/Utopia/Messaging/Adapter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index f6bd088..48775c8 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -2,7 +2,6 @@ namespace Utopia\Messaging; - use Exception; use libphonenumber\NumberParseException; use libphonenumber\PhoneNumberUtil; @@ -275,4 +274,4 @@ public function getCountryCode(string $phone): string throw new Exception("Error parsing phone: " . $e->getMessage()); } } -} \ No newline at end of file +} From f7d777e15cf7082805239feba6ab252bbc2526b2 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:39:55 +0300 Subject: [PATCH 04/12] getCountryCode() --- src/Utopia/Messaging/Adapter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 48775c8..8da13d2 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -255,9 +255,11 @@ protected function requestMulti( /** + * @param string $phone + * @return int|null * @throws Exception */ - public function getCountryCode(string $phone): string + public function getCountryCode(string $phone): ?int { if (empty($phone)) { throw new Exception('No phone number was passed.'); From 7aa598b6b1caddb7059e66abe4da8a4543d98bd5 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:50:32 +0300 Subject: [PATCH 05/12] getCountryCode() --- src/Utopia/Messaging/Adapter/SMS/Mock.php | 26 +++++++++++++++++++++++ tests/Messaging/Adapter/SMS/SMSTest.php | 1 + 2 files changed, 27 insertions(+) diff --git a/src/Utopia/Messaging/Adapter/SMS/Mock.php b/src/Utopia/Messaging/Adapter/SMS/Mock.php index 9f39cd3..bcf69d2 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Mock.php +++ b/src/Utopia/Messaging/Adapter/SMS/Mock.php @@ -2,6 +2,9 @@ namespace Utopia\Messaging\Adapter\SMS; +use Exception; +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumberUtil; use Utopia\Messaging\Adapter\SMS as SMSAdapter; use Utopia\Messaging\Messages\SMS as SMSMessage; use Utopia\Messaging\Response; @@ -69,4 +72,27 @@ protected function process(SMSMessage $message): array return $response->toArray(); } + + /** + * @param string $phone + * @return int|null + * @throws Exception + */ + public function getCountryCode(string $phone): ?int + { + if (empty($phone)) { + throw new \Exception('No phone number was passed.'); + } + + $helper = PhoneNumberUtil::getInstance(); + + try { + return $helper + ->parse($phone) + ->getCountryCode(); + + } catch (NumberParseException $e) { + throw new \Exception("Error parsing phone: " . $e->getMessage()); + } + } } diff --git a/tests/Messaging/Adapter/SMS/SMSTest.php b/tests/Messaging/Adapter/SMS/SMSTest.php index 848b034..0403c40 100644 --- a/tests/Messaging/Adapter/SMS/SMSTest.php +++ b/tests/Messaging/Adapter/SMS/SMSTest.php @@ -32,5 +32,6 @@ public function testSendSMS(): void $this->assertEquals('POST', $smsRequest['method']); $this->assertEquals('+987654321', $smsRequest['data']['from']); $this->assertEquals('+123456789', $smsRequest['data']['to']); + var_dump($sender->getCountryCode($smsRequest['data']['to'])); } } From 13bffb9ac6f89f9424ed41a52f951ca3f194480f Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:53:41 +0300 Subject: [PATCH 06/12] getCountryCode() --- tests/Messaging/Adapter/SMS/SMSTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Messaging/Adapter/SMS/SMSTest.php b/tests/Messaging/Adapter/SMS/SMSTest.php index 0403c40..ece8a05 100644 --- a/tests/Messaging/Adapter/SMS/SMSTest.php +++ b/tests/Messaging/Adapter/SMS/SMSTest.php @@ -32,6 +32,6 @@ public function testSendSMS(): void $this->assertEquals('POST', $smsRequest['method']); $this->assertEquals('+987654321', $smsRequest['data']['from']); $this->assertEquals('+123456789', $smsRequest['data']['to']); - var_dump($sender->getCountryCode($smsRequest['data']['to'])); + $this->assertEquals(1, $sender->getCountryCode($smsRequest['data']['to'])); } } From 30ef3284c32f983d3a509c75804a34600391ca85 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 7 Apr 2024 17:54:54 +0300 Subject: [PATCH 07/12] getCountryCode() --- tests/Messaging/Adapter/SMS/SMSTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Messaging/Adapter/SMS/SMSTest.php b/tests/Messaging/Adapter/SMS/SMSTest.php index ece8a05..a83ed44 100644 --- a/tests/Messaging/Adapter/SMS/SMSTest.php +++ b/tests/Messaging/Adapter/SMS/SMSTest.php @@ -32,6 +32,7 @@ public function testSendSMS(): void $this->assertEquals('POST', $smsRequest['method']); $this->assertEquals('+987654321', $smsRequest['data']['from']); $this->assertEquals('+123456789', $smsRequest['data']['to']); + $this->assertEquals(9, $sender->getCountryCode($smsRequest['data']['from'])); $this->assertEquals(1, $sender->getCountryCode($smsRequest['data']['to'])); } } From 9ff260b9c9006f8dcf7c36a42f9b7a6b2d365589 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Wed, 8 May 2024 12:53:16 +0300 Subject: [PATCH 08/12] Update src/Utopia/Messaging/Adapter.php Co-authored-by: Christy Jacob --- src/Utopia/Messaging/Adapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 8da13d2..37ee604 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -262,7 +262,7 @@ protected function requestMulti( public function getCountryCode(string $phone): ?int { if (empty($phone)) { - throw new Exception('No phone number was passed.'); + throw new Exception('$phone cannot be empty.'); } $helper = PhoneNumberUtil::getInstance(); From 6d640863a82a865744320e1c8f97f5972387345e Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 8 May 2024 13:16:22 +0300 Subject: [PATCH 09/12] getCountryCode() --- composer.json | 3 +- composer.lock | 37 ++++++++++++----------- src/Utopia/Messaging/Adapter.php | 5 ++- src/Utopia/Messaging/Adapter/SMS/Mock.php | 22 -------------- 4 files changed, 22 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index ef3c9b4..5fc8099 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,7 @@ "ext-curl": "*", "ext-openssl": "*", "phpmailer/phpmailer": "6.9.1", - "giggsey/libphonenumber-for-php-lite": "8.13.30" - + "giggsey/libphonenumber-for-php-lite": "8.13.36" }, "require-dev": { "phpunit/phpunit": "10.5.10", diff --git a/composer.lock b/composer.lock index a961c55..85062ad 100644 --- a/composer.lock +++ b/composer.lock @@ -4,24 +4,24 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a3489a7fb8a6667e3c9775bcbc783ecf", + "content-hash": "77d4d5965c6b792d5c6d2b2a3e71a851", "packages": [ { "name": "giggsey/libphonenumber-for-php-lite", - "version": "8.13.30", + "version": "8.13.36", "source": { "type": "git", "url": "https://github.com/giggsey/libphonenumber-for-php-lite.git", - "reference": "7b205dac7d90a791f7bbae77f2ced2c90b0c5df1" + "reference": "144bbe70d67664b5245910a475c7190ff140ab4b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/giggsey/libphonenumber-for-php-lite/zipball/7b205dac7d90a791f7bbae77f2ced2c90b0c5df1", - "reference": "7b205dac7d90a791f7bbae77f2ced2c90b0c5df1", + "url": "https://api.github.com/repos/giggsey/libphonenumber-for-php-lite/zipball/144bbe70d67664b5245910a475c7190ff140ab4b", + "reference": "144bbe70d67664b5245910a475c7190ff140ab4b", "shasum": "" }, "require": { - "php": "^8.0", + "php": "^8.1", "symfony/polyfill-mbstring": "^1.17" }, "conflict": { @@ -30,16 +30,17 @@ "require-dev": { "ext-dom": "*", "friendsofphp/php-cs-fixer": "^3.12", - "infection/infection": "^0.26.16", + "infection/infection": "^0.28", "pear/pear-core-minimal": "^1.10.11", "pear/pear_exception": "^1.0.2", - "pear/versioncontrol_git": "^0.5", + "pear/versioncontrol_git": "^0.7", "phing/phing": "^2.17.4", "phpstan/extension-installer": "^1.2", "phpstan/phpstan": "^1.8", "phpstan/phpstan-phpunit": "^1.2", - "phpunit/phpunit": "^9.5.26", - "symfony/console": "^6.0" + "phpunit/phpunit": "^10.5", + "symfony/console": "^6.0", + "symfony/var-exporter": "^6.0" }, "suggest": { "giggsey/libphonenumber-for-php": "Use libphonenumber-for-php for geocoding, carriers, timezones and matching" @@ -86,7 +87,7 @@ "issues": "https://github.com/giggsey/libphonenumber-for-php-lite/issues", "source": "https://github.com/giggsey/libphonenumber-for-php-lite" }, - "time": "2024-02-09T12:16:33+00:00" + "time": "2024-05-03T06:31:11+00:00" }, { "name": "phpmailer/phpmailer", @@ -1408,16 +1409,16 @@ }, { "name": "sebastian/environment", - "version": "6.0.1", + "version": "6.1.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "43c751b41d74f96cbbd4e07b7aec9675651e2951" + "reference": "8074dbcd93529b357029f5cc5058fd3e43666984" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/43c751b41d74f96cbbd4e07b7aec9675651e2951", - "reference": "43c751b41d74f96cbbd4e07b7aec9675651e2951", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/8074dbcd93529b357029f5cc5058fd3e43666984", + "reference": "8074dbcd93529b357029f5cc5058fd3e43666984", "shasum": "" }, "require": { @@ -1432,7 +1433,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "6.0-dev" + "dev-main": "6.1-dev" } }, "autoload": { @@ -1460,7 +1461,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/environment/issues", "security": "https://github.com/sebastianbergmann/environment/security/policy", - "source": "https://github.com/sebastianbergmann/environment/tree/6.0.1" + "source": "https://github.com/sebastianbergmann/environment/tree/6.1.0" }, "funding": [ { @@ -1468,7 +1469,7 @@ "type": "github" } ], - "time": "2023-04-11T05:39:26+00:00" + "time": "2024-03-23T08:47:14+00:00" }, { "name": "sebastian/exporter", diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 37ee604..cb1e266 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -3,7 +3,6 @@ namespace Utopia\Messaging; use Exception; -use libphonenumber\NumberParseException; use libphonenumber\PhoneNumberUtil; abstract class Adapter @@ -272,8 +271,8 @@ public function getCountryCode(string $phone): ?int ->parse($phone) ->getCountryCode(); - } catch (NumberParseException $e) { - throw new Exception("Error parsing phone: " . $e->getMessage()); + } catch (\Throwable $th) { + throw new Exception("Error parsing phone: " . $th->getMessage()); } } } diff --git a/src/Utopia/Messaging/Adapter/SMS/Mock.php b/src/Utopia/Messaging/Adapter/SMS/Mock.php index bcf69d2..7fd2ce7 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Mock.php +++ b/src/Utopia/Messaging/Adapter/SMS/Mock.php @@ -73,26 +73,4 @@ protected function process(SMSMessage $message): array return $response->toArray(); } - /** - * @param string $phone - * @return int|null - * @throws Exception - */ - public function getCountryCode(string $phone): ?int - { - if (empty($phone)) { - throw new \Exception('No phone number was passed.'); - } - - $helper = PhoneNumberUtil::getInstance(); - - try { - return $helper - ->parse($phone) - ->getCountryCode(); - - } catch (NumberParseException $e) { - throw new \Exception("Error parsing phone: " . $e->getMessage()); - } - } } From 37108ef5aa8a242de32eda046c9fb8c067d0719e Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 8 May 2024 13:17:54 +0300 Subject: [PATCH 10/12] getCountryCode() --- src/Utopia/Messaging/Adapter/SMS/Mock.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/SMS/Mock.php b/src/Utopia/Messaging/Adapter/SMS/Mock.php index 7fd2ce7..9f39cd3 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Mock.php +++ b/src/Utopia/Messaging/Adapter/SMS/Mock.php @@ -2,9 +2,6 @@ namespace Utopia\Messaging\Adapter\SMS; -use Exception; -use libphonenumber\NumberParseException; -use libphonenumber\PhoneNumberUtil; use Utopia\Messaging\Adapter\SMS as SMSAdapter; use Utopia\Messaging\Messages\SMS as SMSMessage; use Utopia\Messaging\Response; @@ -72,5 +69,4 @@ protected function process(SMSMessage $message): array return $response->toArray(); } - } From 3b7dba73b66bbe5362c1d0cf2c0ddfd1e64e8b41 Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Wed, 8 May 2024 20:06:07 +0300 Subject: [PATCH 11/12] Update src/Utopia/Messaging/Adapter.php Co-authored-by: Christy Jacob --- src/Utopia/Messaging/Adapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index cb1e266..250fbf3 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -267,7 +267,7 @@ public function getCountryCode(string $phone): ?int $helper = PhoneNumberUtil::getInstance(); try { - return $helper + return $helper ->parse($phone) ->getCountryCode(); From 724ba39fd7d5f701f54370201050e423b9de005f Mon Sep 17 00:00:00 2001 From: Shimon Newman Date: Wed, 8 May 2024 20:06:19 +0300 Subject: [PATCH 12/12] Update tests/Messaging/Adapter/SMS/SMSTest.php Co-authored-by: Christy Jacob --- tests/Messaging/Adapter/SMS/SMSTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Messaging/Adapter/SMS/SMSTest.php b/tests/Messaging/Adapter/SMS/SMSTest.php index a83ed44..9ce9058 100644 --- a/tests/Messaging/Adapter/SMS/SMSTest.php +++ b/tests/Messaging/Adapter/SMS/SMSTest.php @@ -32,7 +32,7 @@ public function testSendSMS(): void $this->assertEquals('POST', $smsRequest['method']); $this->assertEquals('+987654321', $smsRequest['data']['from']); $this->assertEquals('+123456789', $smsRequest['data']['to']); - $this->assertEquals(9, $sender->getCountryCode($smsRequest['data']['from'])); + $this->assertEquals(98, $sender->getCountryCode($smsRequest['data']['from'])); $this->assertEquals(1, $sender->getCountryCode($smsRequest['data']['to'])); } }