- fix phone number format

This commit is contained in:
Kamil Niemczycki 2023-07-30 11:42:07 +02:00
parent 010f8cf278
commit 127ebe79ae
Signed by: kamilniemczycki
GPG Key ID: 04D4E2012F969213
3 changed files with 12 additions and 5 deletions

View File

@ -13,7 +13,7 @@ class CVRequest extends FormRequest
return [
'recipient' => 'required|string',
'email' => 'required|email',
'phone_number' => 'required|string',
'phone_number' => 'required|regex:/^([0-9\s\+]*)$/i|min:12|max:15',
'locations' => 'required|array',
'mission' => 'nullable|string',
'rodo' => 'nullable|string',

View File

@ -13,7 +13,7 @@ class PhoneResource extends JsonResource
public function toArray($request): array
{
return [
'number' => $this->phoneNumber,
'number' => $this->phone_number,
'formattedNumber' => $this->formattedPhoneNumber,
];
}

View File

@ -14,7 +14,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property string $token
* @property string $recipient
* @property string $email
* @property string $phoneNumber
* @property string|null $phoneNumber
* @property array $locations
* @property string|null $mission
* @property string|null $rodo
@ -35,14 +35,21 @@ class CV extends Model
protected function phoneNumber(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): ?string => str_replace(' ', '', $attributes['phone_number'] ?? ''),
get: fn (mixed $value): string => str_replace(' ', '', $value ?? ''),
set: fn (mixed $value): string => str_replace(' ', '', $value ?? ''),
);
}
protected function formattedPhoneNumber(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): ?string => $attributes['phone_number'] ?? '',
get: function (mixed $value, array $attributes): ?string {
$number = str_replace(' ', '', $attributes['phone_number'] ?? '');
for ($i = 3; $i < 12; $i+=4) {
$number = substr_replace($number, ' ', $i, 0);
}
return $number;
},
);
}