dependencies-upgrade
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user