Skip to content

Commit b358b50

Browse files
committed
refactor: improve phone number validation and database storage
1 parent 797d814 commit b358b50

14 files changed

+196
-7
lines changed

app/Http/Requests/CourseRegistration/GuestCourseRegistrationRequest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function rules(): array
2727
'name' => 'required|string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
2828
'last_name' => 'required|string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
2929
'email' => 'required|email|unique:users,email',
30-
'phone' => 'required|regex:/^\+?\d{9,15}$/',
30+
'phone' => 'phone:PL,INTERNATIONAL',
31+
'phone_country' => 'required_with:phone_number|string|size:2',
3132
];
3233
}
3334

@@ -39,6 +40,7 @@ public function getGuestCourseRegistration(Course $course): StoreGuestCourseRegi
3940
$this->get('last_name'),
4041
$this->get('email'),
4142
$this->get('phone'),
43+
$this->get('phone_country')
4244
);
4345
}
4446
}

app/Http/Requests/UpdateUserRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function rules(): array
2525
'name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
2626
'last_name' => 'string|min:2|max:30|regex:/^[a-zA-ZÀ-ž\s\'-]+$/',
2727
'email' => 'email|max:255|unique:users,email,'.$this->user->id,
28-
'phone_number' => 'string|regex:/^\+?\d{9,15}$/',
28+
'phone_number' => 'phone:PL,INTERNATIONAL',
29+
'phone_country' => 'required_with:phone_number|string|size:2',
2930
'voivodship' => 'string|min:1|max:30',
3031
'city' => 'string|min:1|max:30',
3132
'zip_code' => 'string|regex:/^\d{2}-\d{3}$/',

app/Http/Resources/CourseRegistration/CourseRegistrationResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function toArray(Request $request): array
2121
'last_name' => $this->last_name,
2222
'email' => $this->email,
2323
'phone' => $this->phone,
24+
'phone_country' => $this->phone_country,
2425
'status' => $this->status,
2526
'created_at' => $this->created_at?->toDateTimeString(),
2627
'updated_at' => $this->updated_at?->toDateTimeString(),

app/Http/Resources/UserResource.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Illuminate\Http\Resources\Json\JsonResource;
7+
use Propaganistas\LaravelPhone\PhoneNumber;
78

89
class UserResource extends JsonResource
910
{
@@ -19,7 +20,8 @@ public function toArray(Request $request): array
1920
'name' => $this->name,
2021
'last_name' => $this->last_name,
2122
'email' => $this->email,
22-
'phone_number' => $this->phone_number,
23+
'phone_number' => $this->formatPhoneNumber(),
24+
'phone_country' => $this->phone_country,
2325
'voivodship' => $this->voivodship,
2426
'city' => $this->city,
2527
'zip_code' => $this->zip_code,
@@ -30,4 +32,18 @@ public function toArray(Request $request): array
3032
'updated_at' => $this->updated_at,
3133
];
3234
}
35+
36+
private function formatPhoneNumber(): ?string
37+
{
38+
if (empty($this->phone_number)) {
39+
return null;
40+
}
41+
try {
42+
$phone = new PhoneNumber($this->phone_number, $this->phone_country);
43+
return $phone->formatNational();
44+
} catch (\Throwable $e) {
45+
return $this->phone_number;
46+
}
47+
}
48+
3349
}

app/Models/CourseRegistration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CourseRegistration extends Model
1818
'last_name',
1919
'email',
2020
'phone',
21+
'phone_country',
2122
'status',
2223
];
2324

app/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class User extends Authenticatable
2828
'email',
2929
'password',
3030
'phone_number',
31+
'phone_country',
3132
'voivodship',
3233
'city',
3334
'zip_code',

app/Services/CourseRegistrationService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function accept(CourseRegistration $courseRegistration): CourseRegistrati
3939
'last_name' => $courseRegistration->last_name,
4040
'email' => $courseRegistration->email,
4141
'phone_number' => $courseRegistration->phone,
42+
'phone_country' => $courseRegistration->phone_country,
4243
'password' => Hash::make(Str::random(12)),
4344
]);
4445

app/Services/GuestCourseRegistrationService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use App\Models\CourseRegistration;
66
use App\ValueObjects\StoreGuestCourseRegistration;
7+
use Propaganistas\LaravelPhone\PhoneNumber;
78

89
class GuestCourseRegistrationService
910
{
1011
public function store(StoreGuestCourseRegistration $data): CourseRegistration
1112
{
12-
return CourseRegistration::create($data->toArray());
13+
$phone = new PhoneNumber($data->getPhone(), $data->getPhoneCountry());
14+
$dataArray = $data->toArray();
15+
$dataArray['phone'] = $phone->formatE164();
16+
return CourseRegistration::create($dataArray);
1317
}
1418
}

app/Services/UserService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Services;
44

55
use App\Models\User;
6+
use Propaganistas\LaravelPhone\PhoneNumber;
67

78
class UserService
89
{
@@ -13,6 +14,8 @@ public function show(User $user): User
1314

1415
public function update(array $data, User $user): User
1516
{
17+
$phone = new PhoneNumber($data['phone_number'], $data['phone_country']);
18+
$data['phone_number'] = $phone->formatE164();
1619
$user->update($data);
1720

1821
return $user;

app/ValueObjects/StoreGuestCourseRegistration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function __construct(
1313
private string $last_name,
1414
private string $email,
1515
private string $phone,
16+
private string $phone_country
1617
) {}
1718

1819
public function getCourseId(): int
@@ -40,6 +41,11 @@ public function getPhone(): string
4041
return $this->phone;
4142
}
4243

44+
public function getPhoneCountry(): string
45+
{
46+
return $this->phone_country;
47+
}
48+
4349
public function toArray(): array
4450
{
4551
return [
@@ -48,6 +54,7 @@ public function toArray(): array
4854
'last_name' => $this->getLast_Name(),
4955
'email' => $this->getEmail(),
5056
'phone' => $this->getPhone(),
57+
'phone_country' => $this->getPhoneCountry(),
5158
'status' => RegistrationStatus::PENDING->value,
5259
];
5360
}

0 commit comments

Comments
 (0)