feat: E.164 phone validation and storage with libphonenumber
- Add giggsey/libphonenumber-for-php, PhoneNumberNormalizer, ValidPhoneNumber rule - Store subscribers as E.164; mutator normalizes on save; optional phone required from form block - Migration to normalize legacy subscriber phones; Mailwizz/search/UI/tests updated - Add run-deploy-from-local.sh and PREREGISTER_DEFAULT_PHONE_REGION in .env.example Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Subscriber;
|
||||
use App\Services\PhoneNumberNormalizer;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('subscribers')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PhoneNumberNormalizer $normalizer */
|
||||
$normalizer = app(PhoneNumberNormalizer::class);
|
||||
|
||||
Subscriber::query()
|
||||
->whereNotNull('phone')
|
||||
->where('phone', '!=', '')
|
||||
->orderBy('id')
|
||||
->chunkById(100, function ($subscribers) use ($normalizer): void {
|
||||
foreach ($subscribers as $subscriber) {
|
||||
$p = $subscriber->phone;
|
||||
if (! is_string($p) || $p === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str_starts_with($p, '+')) {
|
||||
$normalized = $normalizer->normalizeToE164($p);
|
||||
if ($normalized !== null && $normalized !== $p) {
|
||||
$subscriber->update(['phone' => $normalized]);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (preg_match('/^\d{8,15}$/', $p) !== 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalized = $normalizer->normalizeToE164('+'.$p);
|
||||
if ($normalized !== null) {
|
||||
$subscriber->update(['phone' => $normalized]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Irreversible: we cannot recover original user input formatting.
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user