Initial Commit

This commit is contained in:
philipp lang 2023-03-02 23:36:11 +01:00
commit e6aa98c558
5 changed files with 335 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vendor/

20
composer.json Normal file
View File

@ -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"
}
]
}

228
composer.lock generated Normal file
View File

@ -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"
}

43
src/Formatter.php Normal file
View File

@ -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);
}
}

43
src/ValidPhoneRule.php Normal file
View File

@ -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]);
}
}