From e6aa98c5580a46c1173b247d1766f06df0e0a201 Mon Sep 17 00:00:00 2001 From: philipp lang <philipp@aweos.de> Date: Thu, 2 Mar 2023 23:36:11 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 1 + composer.json | 20 ++++ composer.lock | 228 +++++++++++++++++++++++++++++++++++++++++ src/Formatter.php | 43 ++++++++ src/ValidPhoneRule.php | 43 ++++++++ 5 files changed, 335 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/Formatter.php create mode 100644 src/ValidPhoneRule.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a10009e --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "zoomyboy/phone", + "description": "Phone number formatting", + "type": "library", + "license": "MIT", + "require": { + "giggsey/libphonenumber-for-php": "^8.13" + }, + "autoload": { + "psr-4": { + "Zoomyboy\\Phone\\": "src/" + } + }, + "authors": [ + { + "name": "Philipp Lang", + "email": "philipp@zoomyboy.de" + } + ] +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..ada15bf --- /dev/null +++ b/composer.lock @@ -0,0 +1,228 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "dd924814eadc3d1bf5f0dacc5175e87a", + "packages": [ + { + "name": "giggsey/libphonenumber-for-php", + "version": "8.13.6", + "source": { + "type": "git", + "url": "https://github.com/giggsey/libphonenumber-for-php.git", + "reference": "5631645d2ca28e4f62daea7baddcba8c90f4bcbf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/giggsey/libphonenumber-for-php/zipball/5631645d2ca28e4f62daea7baddcba8c90f4bcbf", + "reference": "5631645d2ca28e4f62daea7baddcba8c90f4bcbf", + "shasum": "" + }, + "require": { + "giggsey/locale": "^1.7|^2.0", + "php": ">=5.3.2", + "symfony/polyfill-mbstring": "^1.17" + }, + "require-dev": { + "pear/pear-core-minimal": "^1.9", + "pear/pear_exception": "^1.0", + "pear/versioncontrol_git": "^0.5", + "phing/phing": "^2.7", + "php-coveralls/php-coveralls": "^1.0|^2.0", + "symfony/console": "^2.8|^3.0|^v4.4|^v5.2", + "symfony/phpunit-bridge": "^4.2 || ^5" + }, + "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": "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": "2023-02-10T10:33:16+00:00" + }, + { + "name": "giggsey/locale", + "version": "2.3", + "source": { + "type": "git", + "url": "https://github.com/giggsey/Locale.git", + "reference": "5f035523740be40d40ac768a123c9bcc1ae12f56" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/giggsey/Locale/zipball/5f035523740be40d40ac768a123c9bcc1ae12f56", + "reference": "5f035523740be40d40ac768a123c9bcc1ae12f56", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "require-dev": { + "ext-json": "*", + "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" + }, + "type": "library", + "autoload": { + "psr-4": { + "Giggsey\\Locale\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "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.3" + }, + "time": "2022-10-19T20:03:30+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.27.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.27-dev" + }, + "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.27.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": "2022-11-03T14:55:06+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/src/Formatter.php b/src/Formatter.php new file mode 100644 index 0000000..4f240fe --- /dev/null +++ b/src/Formatter.php @@ -0,0 +1,43 @@ +<?php + +namespace Zoomyboy\Phone; + +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; + +class Formatter +{ + public function format(?string $value): ?string + { + if (!$value) { + return null; + } + + $util = PhoneNumberUtil::getInstance(); + $number = $util->parse($value, 'DE'); + + if (!$util->isValidNumber($number)) { + return null; + } + + return $util->format($number, PhoneNumberFormat::INTERNATIONAL); + } + + public function validate(?string $value): bool + { + if (!$value) { + return false; + } + + $util = PhoneNumberUtil::getInstance(); + try { + $number = $util->parse($value, 'DE'); + } catch (NumberParseException $e) { + return false; + } + + return $util->isValidNumber($number); + } +} + diff --git a/src/ValidPhoneRule.php b/src/ValidPhoneRule.php new file mode 100644 index 0000000..25d4da6 --- /dev/null +++ b/src/ValidPhoneRule.php @@ -0,0 +1,43 @@ +<?php + +namespace Zoomyboy\Phone; + +use Illuminate\Contracts\Validation\Rule; + +class ValidPhoneRule implements Rule +{ + private string $attribute; + + /** + * Create a new rule instance. + * + * @return void + */ + public function __construct(string $attribute) + { + $this->attribute = $attribute; + } + + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * + * @return bool + */ + public function passes($attribute, $value) + { + return app(Formatter::class)->validate($value); + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return __('validation.phone_number', ['name' => $this->attribute]); + } +}