diff --git a/composer.json b/composer.json index 3a06304..b8df5b3 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,6 @@ "require": { "php": "^7.4", "composer/installers": "~2.0", - "giggsey/libphonenumber-for-php": "^8.13", "paragonie/paserk": "^1.1", "paragonie/paseto": "^2.4" }, diff --git a/composer.lock b/composer.lock index dab4fbd..fb6619a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5d8de1ac3aacbb5f49902ae36765475c", + "content-hash": "5a9d93fd5b4f386388287c8e9133244e", "packages": [ { "name": "composer/installers", @@ -262,127 +262,6 @@ }, "time": "2022-12-19T11:08:26+00:00" }, - { - "name": "giggsey/libphonenumber-for-php", - "version": "8.13.49", - "dist": { - "type": "zip", - "url": "https://repo.cdek.ru/repository/cdek-php/giggsey/libphonenumber-for-php/8.13.49/giggsey-libphonenumber-for-php-8.13.49.zip", - "reference": "3f5414aeccd152e5a6d02e2064e40713cad65a07", - "shasum": "" - }, - "require": { - "giggsey/locale": "^2.0", - "php": "^7.4|^8.0", - "symfony/polyfill-mbstring": "^1.17" - }, - "replace": { - "giggsey/libphonenumber-for-php-lite": "self.version" - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "^3.64", - "pear/pear-core-minimal": "^1.10", - "pear/pear_exception": "^1.0", - "pear/versioncontrol_git": "^0.7", - "phing/phing": "^3.0", - "php-coveralls/php-coveralls": "^2.0", - "phpunit/phpunit": "^9.6", - "symfony/console": "^v5.2", - "symfony/var-exporter": "^5.2" - }, - "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/" - ] - }, - "license": [ - "Apache-2.0" - ], - "authors": [ - { - "name": "Joshua Gigg", - "email": "giggsey@gmail.com", - "homepage": "https://giggsey.com/" - } - ], - "description": "PHP Port of Google's libphonenumber", - "homepage": "https://github.com/giggsey/libphonenumber-for-php", - "keywords": [ - "geocoding", - "geolocation", - "libphonenumber", - "mobile", - "phonenumber", - "validation" - ], - "support": { - "issues": "https://github.com/giggsey/libphonenumber-for-php/issues", - "source": "https://github.com/giggsey/libphonenumber-for-php" - }, - "time": "2024-11-04T10:44:34+00:00" - }, - { - "name": "giggsey/locale", - "version": "2.7.0", - "dist": { - "type": "zip", - "url": "https://repo.cdek.ru/repository/cdek-php/giggsey/locale/2.7.0/giggsey-locale-2.7.0.zip", - "reference": "a5c65ea3c2630f27ccb78977990eefbee6dd8f97", - "shasum": "" - }, - "require": { - "php": "^7.4|^8.0" - }, - "require-dev": { - "ext-json": "*", - "friendsofphp/php-cs-fixer": "^3.64", - "pear/pear-core-minimal": "^1.9", - "pear/pear_exception": "^1.0", - "pear/versioncontrol_git": "^0.5", - "phing/phing": "^2.7", - "php-coveralls/php-coveralls": "^2.0", - "phpunit/phpunit": "^8.5|^9.5", - "symfony/console": "^5.0|^6.0", - "symfony/filesystem": "^5.0|^6.0", - "symfony/finder": "^5.0|^6.0", - "symfony/process": "^5.0|^6.0", - "symfony/var-exporter": "^5.2|^6.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "Giggsey\\Locale\\": "src/" - } - }, - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Joshua Gigg", - "email": "giggsey@gmail.com", - "homepage": "https://giggsey.com/" - } - ], - "description": "Locale functions required by libphonenumber-for-php", - "support": { - "issues": "https://github.com/giggsey/Locale/issues", - "source": "https://github.com/giggsey/Locale/tree/2.7.0" - }, - "time": "2024-11-04T11:18:07+00:00" - }, { "name": "paragonie/constant_time_encoding", "version": "v2.7.0", @@ -923,66 +802,6 @@ "source": "https://github.com/phpseclib/phpseclib/tree/2.0.47" }, "time": "2024-02-26T04:55:38+00:00" - }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.31.0", - "dist": { - "type": "zip", - "url": "https://repo.cdek.ru/repository/cdek-php/symfony/polyfill-mbstring/v1.31.0/symfony-polyfill-mbstring-v1.31.0.zip", - "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "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\\": "" - } - }, - "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.31.0" - }, - "time": "2024-09-09T11:45:10+00:00" } ], "packages-dev": [ @@ -2459,9 +2278,9 @@ "platform": { "php": "^7.4" }, - "platform-dev": [], + "platform-dev": {}, "platform-overrides": { "php": "7.4" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Actions/OrderCreateAction.php b/src/Actions/OrderCreateAction.php index 5799a05..e571126 100644 --- a/src/Actions/OrderCreateAction.php +++ b/src/Actions/OrderCreateAction.php @@ -193,8 +193,7 @@ private function createOrder(array $packages): array private function buildRequestData(): array { $countryCode = $this->order->country ?: 'RU'; - $recipientNumber = $this->order->phone; - PhoneValidator::new()($recipientNumber, $countryCode); + $recipientNumber = PhoneValidator::new()($this->order->phone, $countryCode); $deliveryMethod = $this->shipping->getMethod(); diff --git a/src/CoreApi.php b/src/CoreApi.php index c60d308..3d22bcb 100644 --- a/src/CoreApi.php +++ b/src/CoreApi.php @@ -102,6 +102,21 @@ public function orderHistory(int $orderId): array )->data(); } + /** + * @throws ApiException + * @throws CoreAuthException + * @throws \Cdek\Exceptions\CacheException + */ + public function validatePhone(string $phone, ?string $country = null): string + { + return HttpClient::sendJsonRequest( + $this->getEndpoint('validate/phone'), + 'GET', + $this->getToken(), + ['phone' => $phone, 'country' => $country], + )->data()['phone']; + } + /** * @throws ShopRegistrationException * @throws CoreAuthException diff --git a/src/Validator/PhoneValidator.php b/src/Validator/PhoneValidator.php index 34db730..8f5d97d 100644 --- a/src/Validator/PhoneValidator.php +++ b/src/Validator/PhoneValidator.php @@ -9,25 +9,23 @@ namespace Cdek\Validator { + use Cdek\CoreApi; + use Cdek\Exceptions\External\HttpClientException; use Cdek\Exceptions\InvalidPhoneException; use Cdek\Traits\CanBeCreated; - use libphonenumber\NumberParseException; - use libphonenumber\PhoneNumberUtil; class PhoneValidator { use CanBeCreated; + /** * @throws \Cdek\Exceptions\InvalidPhoneException */ - public function __invoke(string $phone, string $countryCode = null): void + public function __invoke(string $phone, string $countryCode = null): string { - $phoneNumUtil = PhoneNumberUtil::getInstance(); try { - if (!$phoneNumUtil->isValidNumber($phoneNumUtil->parse($phone, $countryCode))) { - throw new InvalidPhoneException($phone); - } - } catch (NumberParseException $e) { + return (new CoreApi)->validatePhone($phone, $countryCode); + } catch (HttpClientException $e) { throw new InvalidPhoneException($phone); } }