Files
RelaisSMS/app/Models/ContactPhoneNumber.php

63 lines
1.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable([
'contact_id',
'label',
'raw_number',
'normalized_number',
'country',
'is_valid',
'is_mobile',
'is_primary',
'validation_error',
])]
class ContactPhoneNumber extends Model
{
public function hasDetectedMobileNumber(): bool
{
if ($this->is_mobile) {
return true;
}
$normalizedNumber = (string) $this->normalized_number;
if (preg_match('/^\+33[67]\d{8}$/', $normalizedNumber) === 1) {
return true;
}
$rawDigits = preg_replace('/\D+/', '', (string) $this->raw_number) ?? '';
return preg_match('/^0[67]\d{8}$/', $rawDigits) === 1
|| preg_match('/^33[67]\d{8}$/', $rawDigits) === 1
|| preg_match('/^0033[67]\d{8}$/', $rawDigits) === 1;
}
/**
* @return BelongsTo<Contact, $this>
*/
public function contact(): BelongsTo
{
return $this->belongsTo(Contact::class);
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_valid' => 'boolean',
'is_mobile' => 'boolean',
'is_primary' => 'boolean',
];
}
}