dependencies-upgrade
This commit is contained in:
216
vendor/propaganistas/laravel-phone/README.md
vendored
216
vendor/propaganistas/laravel-phone/README.md
vendored
@@ -5,19 +5,20 @@
|
||||
[](https://packagist.org/packages/propaganistas/laravel-phone)
|
||||
[](https://packagist.org/packages/propaganistas/laravel-phone)
|
||||
|
||||
Adds phone number functionality to Laravel and Lumen based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://github.com/googlei18n/libphonenumber) by [giggsey](https://github.com/giggsey).
|
||||
Adds phone number functionality to Laravel based on the [PHP port](https://github.com/giggsey/libphonenumber-for-php) of [Google's libphonenumber API](https://github.com/googlei18n/libphonenumber) by [giggsey](https://github.com/giggsey).
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Demo](#demo)
|
||||
- [Installation](#installation)
|
||||
- [Laravel](#laravel)
|
||||
- [Lumen](#lumen)
|
||||
- [Validation](#validation)
|
||||
- [Attribute casting](#attribute-casting)
|
||||
- [Utility class](#utility-phonenumber-class)
|
||||
- [Formatting](#formatting)
|
||||
- [Number information](#number-information)
|
||||
- [Equality comparison](#equality-comparison)
|
||||
- [Helper function](#helper-function)
|
||||
- [Database considerations](#database-considerations)
|
||||
|
||||
## Demo
|
||||
|
||||
@@ -31,24 +32,14 @@ Run the following command to install the latest applicable version of the packag
|
||||
composer require propaganistas/laravel-phone
|
||||
```
|
||||
|
||||
### Laravel
|
||||
The Service Provider gets discovered automatically by Laravel.
|
||||
|
||||
The Service Provider gets discovered automatically.
|
||||
|
||||
In your languages directory, add an extra line for each language file:
|
||||
In your languages directory, add an extra translation in every `validation.php` language file:
|
||||
|
||||
```php
|
||||
'phone' => 'The :attribute field contains an invalid number.',
|
||||
```
|
||||
|
||||
### Lumen
|
||||
|
||||
In `bootstrap/app.php`, register the Service Provider
|
||||
|
||||
```php
|
||||
$app->register(Propaganistas\LaravelPhone\PhoneServiceProvider::class);
|
||||
```
|
||||
|
||||
## Validation
|
||||
|
||||
To validate a phone number, use the `phone` keyword in your validation rules array or use the `Phone` rule class to define the rule in an expressive way. The phone validator is able to operate in **three** ways.
|
||||
@@ -101,9 +92,77 @@ You can also enable more lenient validation (for example, fixed lines without ar
|
||||
// 'phonefield' => Rule::phone()->lenient()->country('US')
|
||||
```
|
||||
|
||||
## Attribute casting
|
||||
|
||||
Two cast classes are provided for automatic casting of Eloquent model attributes:
|
||||
|
||||
```php
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Propaganistas\LaravelPhone\Casts\RawPhoneNumberCast;
|
||||
use Propaganistas\LaravelPhone\Casts\E164PhoneNumberCast;
|
||||
|
||||
class User extends Model
|
||||
{
|
||||
public $casts = [
|
||||
'phone' => RawPhoneNumberCast::class.':BE',
|
||||
'another_phone' => E164PhoneNumberCast::class.':BE',
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
Both classes automatically cast the database value to a PhoneNumber object for further use in your application.
|
||||
|
||||
```php
|
||||
$user->phone // PhoneNumber object or null
|
||||
```
|
||||
When setting a value, they both accept a string value or a PhoneNumber object.
|
||||
The `RawPhoneNumberCast` mutates the database value to the raw input number, while the `E164PhoneNumberCast` writes a formatted E.164 phone number to the database.
|
||||
|
||||
In case of `RawPhoneNumberCast`, the cast needs to be hinted about the phone country in order to properly parse the raw number into a phone object.
|
||||
In case of `E164PhoneNumberCast` and the value to be set is not already in some international format, the cast needs to be hinted about the phone country in order to properly mutate the value.
|
||||
|
||||
Both classes accept cast parameters in the same way:
|
||||
1. When a similar named attribute exists, but suffixed with `_country` (e.g. phone_country), the cast will detect and use it automatically.
|
||||
2. Provide another attribute's name as a cast parameter
|
||||
3. Provide one or several country codes as cast parameters
|
||||
|
||||
```php
|
||||
public $casts = [
|
||||
'phone' => RawPhoneNumberCast::class.':country_field',
|
||||
'another_phone' => E164PhoneNumberCast::class.':BE',
|
||||
];
|
||||
```
|
||||
|
||||
In order to not encounter any unexpected issues when using these casts, please always validate any input using the [validation](#validation) rules previously described.
|
||||
|
||||
#### ⚠️ Attribute assignment and `E164PhoneNumberCast`
|
||||
Due to the nature of `E164PhoneNumberCast` a valid country attribute is expected if the number is not passed in international format. Since casts are applied in the order of the given values, be sure to set the country attribute _before_ setting the phone number attribute. Otherwise `E164PhoneNumberCast` will encounter an empty country value and throw an unexpected exception.
|
||||
|
||||
```php
|
||||
// Wrong
|
||||
$model->fill([
|
||||
'phone' => '012 34 56 78',
|
||||
'phone_country' => 'BE',
|
||||
]);
|
||||
|
||||
// Correct
|
||||
$model->fill([
|
||||
'phone_country' => 'BE',
|
||||
'phone' => '012 34 56 78',
|
||||
]);
|
||||
|
||||
// Wrong
|
||||
$model->phone = '012 34 56 78';
|
||||
$model->phone_country = 'BE';
|
||||
|
||||
// Correct
|
||||
$model->phone_country = 'BE';
|
||||
$model->phone = '012 34 56 78';
|
||||
```
|
||||
|
||||
## Utility PhoneNumber class
|
||||
|
||||
A phone number can be wrapped in the `Propaganistas\LaravelPhone\PhoneNumber` class to enhance it with useful utility methods. It's safe to directly reference these objects in views or when saving to the database as they will degrade gracefully to the E164 format.
|
||||
A phone number can be wrapped in the `Propaganistas\LaravelPhone\PhoneNumber` class to enhance it with useful utility methods. It's safe to directly reference these objects in views or when saving to the database as they will degrade gracefully to the E.164 format.
|
||||
|
||||
```php
|
||||
use Propaganistas\LaravelPhone\PhoneNumber;
|
||||
@@ -117,32 +176,50 @@ use Propaganistas\LaravelPhone\PhoneNumber;
|
||||
A PhoneNumber can be formatted in various ways:
|
||||
|
||||
```php
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->format($format); // See libphonenumber\PhoneNumberFormat
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatE164(); // +3212345678
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatInternational(); // +32 12 34 56 78
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatRFC3966(); // +32-12-34-56-78
|
||||
PhoneNumber::make('012/34.56.78', 'BE')->formatNational(); // 012 34 56 78
|
||||
$phone = PhoneNumber::make('012/34.56.78', 'BE');
|
||||
|
||||
$phone->format($format); // See libphonenumber\PhoneNumberFormat
|
||||
$phone->formatE164(); // +3212345678
|
||||
$phone->formatInternational(); // +32 12 34 56 78
|
||||
$phone->formatRFC3966(); // +32-12-34-56-78
|
||||
$phone->formatNational(); // 012 34 56 78
|
||||
|
||||
// Formats so the number can be called straight from the provided country.
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('BE'); // 012 34 56 78
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('NL'); // 00 32 12 34 56 78
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForCountry('US'); // 011 32 12 34 56 78
|
||||
$phone->formatForCountry('BE'); // 012 34 56 78
|
||||
$phone->formatForCountry('NL'); // 00 32 12 34 56 78
|
||||
$phone->formatForCountry('US'); // 011 32 12 34 56 78
|
||||
|
||||
// Formats so the number can be clicked on and called straight from the provided country using a cellphone.
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('BE'); // 012345678
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('NL'); // +3212345678
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->formatForMobileDialingInCountry('US'); // +3212345678
|
||||
$phone->formatForMobileDialingInCountry('BE'); // 012345678
|
||||
$phone->formatForMobileDialingInCountry('NL'); // +3212345678
|
||||
$phone->formatForMobileDialingInCountry('US'); // +3212345678
|
||||
```
|
||||
|
||||
### Number information
|
||||
Get some information about the phone number:
|
||||
|
||||
```php
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->getType(); // 'fixed_line'
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->isOfType('fixed_line'); // true
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->getCountry(); // 'BE'
|
||||
PhoneNumber::make('012 34 56 78', 'BE')->isOfCountry('BE'); // true
|
||||
PhoneNumber::make('+32 12 34 56 78')->isOfCountry('BE'); // true
|
||||
$phone = PhoneNumber::make('012 34 56 78', 'BE');
|
||||
|
||||
$phone->getType(); // 'fixed_line'
|
||||
$phone->isOfType('fixed_line'); // true
|
||||
$phone->getCountry(); // 'BE'
|
||||
$phone->isOfCountry('BE'); // true
|
||||
```
|
||||
|
||||
### Equality comparison
|
||||
Check if a given phone number is (not) equal to another one:
|
||||
|
||||
```php
|
||||
$phone = PhoneNumber::make('012 34 56 78', 'BE');
|
||||
|
||||
$phone->equals('012/34.56.76', 'BE') // true
|
||||
$phone->equals('+32 12 34 56 78') // true
|
||||
$phone->equals( $anotherPhoneObject ) // true/false
|
||||
|
||||
$phone->notEquals('045 67 89 10', 'BE') // false
|
||||
$phone->notEquals('+32 45 67 89 10') // true
|
||||
$phone->notEquals( $anotherPhoneObject ) // true/false
|
||||
```
|
||||
|
||||
### Helper function
|
||||
@@ -152,3 +229,78 @@ The package exposes the `phone()` helper function that returns a `Propaganistas\
|
||||
```php
|
||||
phone($number, $country = [], $format = null)
|
||||
```
|
||||
|
||||
## Database considerations
|
||||
|
||||
> Disclaimer: Phone number handling is quite different in each application. The topics mentioned below are therefore meant as a set of thought starters; support will **not** be provided.
|
||||
|
||||
Storing phone numbers in a database has always been a speculative topic and there's simply no silver bullet. It all depends on your application's requirements. Here are some things to take into account, along with an implementation suggestion. Your ideal database setup will probably be a combination of some of the pointers detailed below.
|
||||
|
||||
### Uniqueness
|
||||
|
||||
The E.164 format globally and uniquely identifies a phone number across the world. It also inherently implies a specific country and can be supplied as-is to the `phone()` helper.
|
||||
|
||||
You'll need:
|
||||
|
||||
* One column to store the phone number
|
||||
* To format the phone number to E.164 before persisting it
|
||||
|
||||
Example:
|
||||
|
||||
* User input = `012/45.65.78`
|
||||
* Database column
|
||||
* `phone` (varchar) = `+3212456578`
|
||||
|
||||
### Presenting the phone number the way it was inputted
|
||||
|
||||
If you store formatted phone numbers the raw user input will unretrievably get lost. It may be beneficial to present your users with their very own inputted phone number, for example in terms of improved user experience.
|
||||
|
||||
You'll need:
|
||||
* Two columns to store the raw input and the correlated country
|
||||
|
||||
Example:
|
||||
|
||||
* User input = `012/34.56.78`
|
||||
* Database columns
|
||||
* `phone` (varchar) = `012/34.56.78`
|
||||
* `phone_country` (varchar) = `BE`
|
||||
|
||||
### Supporting searches
|
||||
|
||||
Searching through phone numbers can quickly become ridiculously complex and will always require deep understanding of the context and extent of your application. Here's _a_ possible approach covering quite a lot of "natural" use cases.
|
||||
|
||||
You'll need:
|
||||
* Three additional columns to store searchable variants of the phone number:
|
||||
* Normalized input (raw input with all non-alpha characters stripped)
|
||||
* National formatted phone number (with all non-alpha characters stripped)
|
||||
* E.164 formatted phone number
|
||||
* Probably a `saving()` observer (or equivalent) to prefill the variants before persistence
|
||||
* An extensive search query utilizing the searchable variants
|
||||
|
||||
Example:
|
||||
|
||||
* User input = `12/34.56.78`
|
||||
* Observer method:
|
||||
```php
|
||||
public function saving(User $user)
|
||||
{
|
||||
if ($user->isDirty('phone') && $user->phone) {
|
||||
$user->phone_normalized = preg_replace('[^0-9]', '', $user->phone);
|
||||
$user->phone_national = preg_replace('[^0-9]', '', phone($user->phone, $user->phone_country)->formatNational());
|
||||
$user->phone_e164 = phone($user->phone, $user->phone_country)->formatE164();
|
||||
}
|
||||
}
|
||||
```
|
||||
* Database columns
|
||||
* `phone_normalized` (varchar) = `12345678`
|
||||
* `phone_national` (varchar) = `012345678`
|
||||
* `phone_e164` (varchar) = `+3212345678`
|
||||
* Search query:
|
||||
```php
|
||||
// $search holds the search term
|
||||
User::where(function($query) use ($search) {
|
||||
$query->where('phone_normalized', 'LIKE', preg_replace('[^0-9]', '', $search) . '%')
|
||||
->orWhere('phone_national', 'LIKE', preg_replace('[^0-9]', '', $search) . '%')
|
||||
->orWhere('phone_e164', 'LIKE', preg_replace('[^+0-9]', '', $search) . '%')
|
||||
});
|
||||
```
|
||||
|
||||
19
vendor/propaganistas/laravel-phone/composer.json
vendored
19
vendor/propaganistas/laravel-phone/composer.json
vendored
@@ -1,9 +1,8 @@
|
||||
{
|
||||
"name": "propaganistas/laravel-phone",
|
||||
"description": "Adds phone number functionality to Laravel and Lumen based on Google's libphonenumber API.",
|
||||
"description": "Adds phone number functionality to Laravel based on Google's libphonenumber API.",
|
||||
"keywords": [
|
||||
"laravel",
|
||||
"lumen",
|
||||
"libphonenumber",
|
||||
"validation",
|
||||
"phone"
|
||||
@@ -16,15 +15,17 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.1|^8.0",
|
||||
"illuminate/support": "^6.0|^7.0|^8.0",
|
||||
"illuminate/validation": "^6.0|^7.0|^8.0",
|
||||
"php": "^7.3|^8.0",
|
||||
"illuminate/contracts": "^8.9|^9.0",
|
||||
"illuminate/support": "^8.0|^9.0",
|
||||
"illuminate/validation": "^8.0|^9.0",
|
||||
"giggsey/libphonenumber-for-php": "^7.0|^8.0",
|
||||
"league/iso3166": "^2.0|^3.0"
|
||||
"league/iso3166": "^2.0|^3.0|^4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"orchestra/testbench": "*",
|
||||
"phpunit/phpunit": "*"
|
||||
"phpunit/phpunit": "^9.5.10",
|
||||
"nunomaduro/larastan": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -45,5 +46,7 @@
|
||||
"Propaganistas\\LaravelPhone\\PhoneServiceProvider"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
}
|
||||
|
||||
78
vendor/propaganistas/laravel-phone/src/Casts/E164PhoneNumberCast.php
vendored
Normal file
78
vendor/propaganistas/laravel-phone/src/Casts/E164PhoneNumberCast.php
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Casts;
|
||||
|
||||
use Propaganistas\LaravelPhone\PhoneNumber;
|
||||
use UnexpectedValueException;
|
||||
|
||||
class E164PhoneNumberCast extends PhoneNumberCast
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return \Propaganistas\LaravelPhone\PhoneNumber|null
|
||||
*/
|
||||
public function get($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$phone = new PhoneNumber($value);
|
||||
|
||||
if (! $phone->numberLooksInternational()) {
|
||||
throw new UnexpectedValueException(
|
||||
'Queried value for '.$key.' is not in international format'
|
||||
);
|
||||
}
|
||||
|
||||
return $phone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $value instanceof PhoneNumber) {
|
||||
$value = (new PhoneNumber($value))->ofCountry(
|
||||
$this->getPossibleCountries($key, $attributes)
|
||||
);
|
||||
}
|
||||
|
||||
return $value->formatE164();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the attribute when converting the model to an array.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function serialize($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var $value PhoneNumber */
|
||||
return $value->formatE164();
|
||||
}
|
||||
}
|
||||
46
vendor/propaganistas/laravel-phone/src/Casts/PhoneNumberCast.php
vendored
Normal file
46
vendor/propaganistas/laravel-phone/src/Casts/PhoneNumberCast.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
use Illuminate\Contracts\Database\Eloquent\SerializesCastableAttributes;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
abstract class PhoneNumberCast implements CastsAttributes, SerializesCastableAttributes
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $parameters = [];
|
||||
|
||||
/**
|
||||
* @param mixed $parameters
|
||||
*/
|
||||
public function __construct($parameters = [])
|
||||
{
|
||||
$this->parameters = is_array($parameters) ? $parameters : func_get_args();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param array $attributes
|
||||
* @return array
|
||||
*/
|
||||
protected function getPossibleCountries($key, array $attributes)
|
||||
{
|
||||
$parameters = $this->parameters;
|
||||
|
||||
// Discover if an attribute was provided. If not, default to _country.
|
||||
$inputField = Collection::make($parameters)
|
||||
->intersect(array_keys(Arr::dot($attributes)))
|
||||
->first() ?: "{$key}_country";
|
||||
|
||||
// Attempt to retrieve the field's value.
|
||||
if ($inputCountry = Arr::get($attributes, $inputField)) {
|
||||
$parameters[] = $inputCountry;
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
}
|
||||
73
vendor/propaganistas/laravel-phone/src/Casts/RawPhoneNumberCast.php
vendored
Normal file
73
vendor/propaganistas/laravel-phone/src/Casts/RawPhoneNumberCast.php
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Casts;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Propaganistas\LaravelPhone\PhoneNumber;
|
||||
|
||||
class RawPhoneNumberCast extends PhoneNumberCast
|
||||
{
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return \Propaganistas\LaravelPhone\PhoneNumber|null
|
||||
*/
|
||||
public function get($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$phone = new PhoneNumber($value);
|
||||
$countries = $this->getPossibleCountries($key, $attributes);
|
||||
|
||||
if (empty($countries) && ! $phone->numberLooksInternational()) {
|
||||
throw new InvalidArgumentException(
|
||||
'Missing country specification for '.$key.' attribute cast'
|
||||
);
|
||||
}
|
||||
|
||||
return $phone->ofCountry($countries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if ($value instanceof PhoneNumber) {
|
||||
return $value->getRawNumber();
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the attribute when converting the model to an array.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function serialize($model, string $key, $value, array $attributes)
|
||||
{
|
||||
if (! $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var $value PhoneNumber */
|
||||
return $value->getRawNumber();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class CountryCodeException extends \Exception
|
||||
{
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
class NumberFormatException extends \Exception
|
||||
{
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Exceptions;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use libphonenumber\NumberParseException as libNumberParseException;
|
||||
@@ -37,7 +39,7 @@ class NumberParseException extends libNumberParseException
|
||||
* Country mismatch static constructor.
|
||||
*
|
||||
* @param string $number
|
||||
* @param string|array $country
|
||||
* @param string|array $countries
|
||||
* @return static
|
||||
*/
|
||||
public static function countryMismatch($number, $countries)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
@@ -11,6 +13,7 @@ use libphonenumber\NumberParseException as libNumberParseException;
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
use libphonenumber\PhoneNumberType;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
|
||||
use Propaganistas\LaravelPhone\Exceptions\NumberFormatException;
|
||||
use Propaganistas\LaravelPhone\Exceptions\CountryCodeException;
|
||||
use Propaganistas\LaravelPhone\Exceptions\NumberParseException;
|
||||
@@ -145,7 +148,7 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
/**
|
||||
* Format the phone number in a given format.
|
||||
*
|
||||
* @param string $format
|
||||
* @param string|int $format
|
||||
* @return string
|
||||
* @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException
|
||||
*/
|
||||
@@ -265,7 +268,7 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
foreach ($countries as $country) {
|
||||
$instance = $this->lib->parse($this->number, $country);
|
||||
|
||||
if ($this->lib->isValidNumber($instance)) {
|
||||
if (($this->lenient && $this->lib->isPossibleNumber($instance)) || $this->lib->isValidNumber($instance)) {
|
||||
return $this->lib->getRegionCodeForNumber($instance);
|
||||
}
|
||||
}
|
||||
@@ -314,6 +317,38 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
return in_array($this->getType(true), $types, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if two phone numbers are the same.
|
||||
*
|
||||
* @param string|static $number
|
||||
* @param string|array|null $country
|
||||
* @return bool
|
||||
*/
|
||||
public function equals($number, $country = null)
|
||||
{
|
||||
try {
|
||||
if (! $number instanceof static) {
|
||||
$number = static::make($number, $country);
|
||||
}
|
||||
|
||||
return $this->formatE164() === $number->formatE164();
|
||||
} catch (NumberParseException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if two phone numbers are not the same.
|
||||
*
|
||||
* @param string|static $number
|
||||
* @param string|array|null $country
|
||||
* @return bool
|
||||
*/
|
||||
public function notEquals($number, $country = null)
|
||||
{
|
||||
return ! $this->equals($number, $country);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw provided number.
|
||||
*
|
||||
@@ -339,7 +374,7 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function numberLooksInternational()
|
||||
public function numberLooksInternational()
|
||||
{
|
||||
return Str::startsWith($this->number, '+');
|
||||
}
|
||||
@@ -372,6 +407,7 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->formatE164();
|
||||
@@ -381,21 +417,45 @@ class PhoneNumber implements Jsonable, JsonSerializable, Serializable
|
||||
* Convert the phone instance into a string representation.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @deprecated PHP 8.1
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return $this->formatE164();
|
||||
return $this->__serialize()['number'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstructs the phone instance from a string representation.
|
||||
*
|
||||
* @param string $serialized
|
||||
* @param string|array $serialized
|
||||
*
|
||||
* @deprecated PHP 8.1
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$this->__unserialize(is_array($serialized) ? $serialized : ['number' => $serialized]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the phone instance into a string representation.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __serialize()
|
||||
{
|
||||
return ['number' => $this->formatE164()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstructs the phone instance from a string representation.
|
||||
*
|
||||
* @param array $serialized
|
||||
*/
|
||||
public function __unserialize(array $serialized)
|
||||
{
|
||||
$this->lib = PhoneNumberUtil::getInstance();
|
||||
$this->number = $serialized;
|
||||
$this->number = $serialized['number'];
|
||||
$this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php namespace Propaganistas\LaravelPhone;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Validation\Factory;
|
||||
use Illuminate\Validation\Rule;
|
||||
use libphonenumber\PhoneNumberUtil;
|
||||
use Propaganistas\LaravelPhone\Rules;
|
||||
@@ -20,16 +23,10 @@ class PhoneServiceProvider extends ServiceProvider
|
||||
});
|
||||
|
||||
$this->app->alias('libphonenumber', PhoneNumberUtil::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->app['validator']->extendDependent('phone', Validation\Phone::class . '@validate');
|
||||
|
||||
$this->app->afterResolving('validator', static function (Factory $validator) {
|
||||
$validator->extendDependent('phone', Validation\Phone::class . '@validate');
|
||||
});
|
||||
|
||||
Rule::macro('phone', function () {
|
||||
return new Rules\Phone;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Rules;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Rules;
|
||||
|
||||
use libphonenumber\PhoneNumberType;
|
||||
use Propaganistas\LaravelPhone\Traits\ParsesCountries;
|
||||
@@ -74,7 +76,7 @@ class Phone
|
||||
/**
|
||||
* Set the phone types.
|
||||
*
|
||||
* @param string|array $type
|
||||
* @param int|string|array $type
|
||||
* @return $this
|
||||
*/
|
||||
public function type($type)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Traits;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Traits;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use League\ISO3166\ISO3166;
|
||||
@@ -33,6 +35,10 @@ trait ParsesCountries
|
||||
protected function parseCountries($countries)
|
||||
{
|
||||
return Collection::make(is_array($countries) ? $countries : func_get_args())
|
||||
->reject(function ($value) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
return is_null($value);
|
||||
})
|
||||
->map(function ($country) {
|
||||
return strtoupper($country);
|
||||
})
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Traits;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Traits;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use libphonenumber\PhoneNumberFormat;
|
||||
@@ -27,12 +29,12 @@ trait ParsesFormats
|
||||
/**
|
||||
* Parse a phone format.
|
||||
*
|
||||
* @param string $format
|
||||
* @return string
|
||||
* @param int|string $format
|
||||
* @return int|null
|
||||
*/
|
||||
protected static function parseFormat($format)
|
||||
{
|
||||
static::loadFormats();
|
||||
self::loadFormats();
|
||||
|
||||
// If the format equals a constant's value, just return it.
|
||||
if (in_array($format, static::$formats, true)) {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Traits;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Traits;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -17,7 +19,7 @@ trait ParsesTypes
|
||||
/**
|
||||
* Determine whether the given type is valid.
|
||||
*
|
||||
* @param string $type
|
||||
* @param int|string $type
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValidType($type)
|
||||
@@ -28,14 +30,18 @@ trait ParsesTypes
|
||||
/**
|
||||
* Parse a phone type into constant's value.
|
||||
*
|
||||
* @param string|array $types
|
||||
* @param int|string|array $types
|
||||
* @return array
|
||||
*/
|
||||
protected static function parseTypes($types)
|
||||
{
|
||||
static::loadTypes();
|
||||
self::loadTypes();
|
||||
|
||||
return Collection::make(is_array($types) ? $types : func_get_args())
|
||||
->reject(function ($value) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
return is_null($value);
|
||||
})
|
||||
->map(function ($type) {
|
||||
// If the type equals a constant's value, just return it.
|
||||
if (is_numeric($type) && in_array($type, static::$resolvedTypes)) {
|
||||
@@ -45,20 +51,20 @@ trait ParsesTypes
|
||||
// Otherwise we'll assume the type is the constant's name.
|
||||
return Arr::get(static::$resolvedTypes, strtoupper($type));
|
||||
})
|
||||
->reject(function ($value) {
|
||||
return is_null($value) || $value === false;
|
||||
->filter(function ($value) {
|
||||
return is_numeric($value);
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a phone type into its string representation.
|
||||
*
|
||||
* @param string|array $types
|
||||
* @param int|string|array $types
|
||||
* @return array
|
||||
*/
|
||||
protected static function parseTypesAsStrings($types)
|
||||
{
|
||||
static::loadTypes();
|
||||
self::loadTypes();
|
||||
|
||||
return array_keys(
|
||||
array_intersect(
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<?php namespace Propaganistas\LaravelPhone\Validation;
|
||||
<?php
|
||||
|
||||
namespace Propaganistas\LaravelPhone\Validation;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -74,11 +76,12 @@ class Phone
|
||||
$phoneNumberInstance = $phoneNumber->getPhoneNumberInstance();
|
||||
|
||||
// Country detection.
|
||||
if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) {
|
||||
return true;
|
||||
// Will throw a NumberParseException if country could not be detected.
|
||||
if ($detect && $country === null) {
|
||||
$country = $phoneNumber->getCountry();
|
||||
}
|
||||
|
||||
// Default number+country validation.
|
||||
// Number+country validation.
|
||||
if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) {
|
||||
return true;
|
||||
}
|
||||
@@ -104,7 +107,7 @@ class Phone
|
||||
// Discover if an input field was provided. If not, guess the field's name.
|
||||
$inputField = Collection::make($parameters)
|
||||
->intersect(array_keys(Arr::dot($data)))
|
||||
->first() ?: "${attribute}_country";
|
||||
->first() ?: "{$attribute}_country";
|
||||
|
||||
// Attempt to retrieve the field's value.
|
||||
if ($inputCountry = Arr::get($data, $inputField)) {
|
||||
|
||||
@@ -8,7 +8,7 @@ if (! function_exists('phone')) {
|
||||
*
|
||||
* @param string $number
|
||||
* @param string|array $country
|
||||
* @param string $format
|
||||
* @param string|int $format
|
||||
* @return string|Propaganistas\LaravelPhone\PhoneNumber
|
||||
*/
|
||||
function phone($number, $country = [], $format = null)
|
||||
|
||||
Reference in New Issue
Block a user