using new style of attribute casting
This commit is contained in:
@ -2,19 +2,24 @@
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
trait FormattedPhoneTrait
|
||||
{
|
||||
/**
|
||||
* Format a phone number to be human readable.
|
||||
*
|
||||
* @package Namespace\App\Contact
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string
|
||||
* @return void
|
||||
*/
|
||||
public function getPhoneNumberAttribute(): string
|
||||
protected function phoneNumber(): Attribute
|
||||
{
|
||||
$phoneLength = strlen($this->phone);
|
||||
$phoneNumber = preg_replace('//', '', $this->phone);
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
$phoneLength = strlen($attributes['phone_number']);
|
||||
$phoneNumber = preg_replace('/[^0-9]/', '', $attributes['phone_number']);
|
||||
|
||||
if ($phoneLength > 10) {
|
||||
$countryCode = substr($phoneNumber, 0, $phoneLength - 10);
|
||||
@ -37,19 +42,10 @@ trait FormattedPhoneTrait
|
||||
}
|
||||
|
||||
return $phoneNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all non-numeric characters from the phone number.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPhoneNumberAttribute($value): void
|
||||
{
|
||||
$this->attributes['phone'] = preg_replace('/[^0-9]/', '', $value);
|
||||
},
|
||||
set: function (mixed $value) {
|
||||
return preg_replace('/[^0-9]/', '', $value);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user