dependencies-upgrade

This commit is contained in:
RafficMohammed
2023-01-08 02:20:59 +05:30
parent 7870479b18
commit 49021a4497
1711 changed files with 74994 additions and 70803 deletions

View 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();
}
}

View 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;
}
}

View 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();
}
}

View File

@@ -1,4 +1,6 @@
<?php namespace Propaganistas\LaravelPhone\Exceptions;
<?php
namespace Propaganistas\LaravelPhone\Exceptions;
class CountryCodeException extends \Exception
{

View File

@@ -1,4 +1,6 @@
<?php namespace Propaganistas\LaravelPhone\Exceptions;
<?php
namespace Propaganistas\LaravelPhone\Exceptions;
use Illuminate\Support\Collection;

View File

@@ -1,4 +1,6 @@
<?php namespace Propaganistas\LaravelPhone\Exceptions;
<?php
namespace Propaganistas\LaravelPhone\Exceptions;
class NumberFormatException extends \Exception
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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)) {

View File

@@ -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(

View File

@@ -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)) {

View File

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