From 127ebe79ae3ccd76bb8f7efa4614e8616a3406d0 Mon Sep 17 00:00:00 2001 From: Kamil Niemczycki Date: Sun, 30 Jul 2023 11:42:07 +0200 Subject: [PATCH] - fix phone number format --- app/Http/Requests/CVRequest.php | 2 +- app/Http/Resources/PhoneResource.php | 2 +- app/Models/CV.php | 13 ++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/Http/Requests/CVRequest.php b/app/Http/Requests/CVRequest.php index 252e0cb..5e2ab03 100644 --- a/app/Http/Requests/CVRequest.php +++ b/app/Http/Requests/CVRequest.php @@ -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', diff --git a/app/Http/Resources/PhoneResource.php b/app/Http/Resources/PhoneResource.php index 99dd346..c0550bb 100644 --- a/app/Http/Resources/PhoneResource.php +++ b/app/Http/Resources/PhoneResource.php @@ -13,7 +13,7 @@ class PhoneResource extends JsonResource public function toArray($request): array { return [ - 'number' => $this->phoneNumber, + 'number' => $this->phone_number, 'formattedNumber' => $this->formattedPhoneNumber, ]; } diff --git a/app/Models/CV.php b/app/Models/CV.php index 79941ec..ff995d3 100644 --- a/app/Models/CV.php +++ b/app/Models/CV.php @@ -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; + }, ); }