feat(profile): enhance national_code uniqueness error message with masked mobile number

This commit is contained in:
hamed
2026-06-24 12:16:16 +03:30
parent 650e36bce2
commit 8b419d0272
5 changed files with 21 additions and 19 deletions
+2 -1
View File
@@ -149,9 +149,10 @@ class PatientController extends BaseController
if ($nationalCode !== '') {
$owner = $this->profileRepo->findOneByNationalCode($nationalCode);
if ($owner !== null && ($patient === null || $owner->getUser()->getId() !== $patient->getId())) {
$masked = \App\Shared\Service\InputValidator::maskMobile($owner->getUser()->getMobileNumber());
return $this->error(
ErrorCodes::ERR_PROFILE_NATIONAL_CODE_TAKEN,
'این کد ملی قبلاً برای کاربر دیگری ثبت شده است',
"این کد ملی قبلاً با شماره {$masked} ثبت شده است",
409,
'national_code'
);
+15 -15
View File
@@ -13,25 +13,25 @@ final class InputValidator
return (bool) preg_match('/^09\d{9}$/', self::toEnglishDigits($mobile));
}
/** کد ملی ایران: ۱۰ رقم + الگوریتم رقم کنترلی؛ ارقام یکسان نامعتبر. */
/** کد ملی: فقط باید دقیقاً ۱۰ رقم باشد (بدون بررسی رقم کنترلی). */
public static function isValidIranNationalCode(string $code): bool
{
$code = self::toEnglishDigits($code);
if (!preg_match('/^\d{10}$/', $code)) {
return false;
}
if (preg_match('/^(\d)\1{9}$/', $code)) {
return false;
}
return (bool) preg_match('/^\d{10}$/', self::toEnglishDigits($code));
}
$check = (int) $code[9];
$sum = 0;
for ($i = 0; $i < 9; $i++) {
$sum += (int) $code[$i] * (10 - $i);
/**
* موبایل را برای نمایش ماسک می‌کند: 09120675433 → 0912****33 (۴ رقم اول + ۲ رقم آخر).
* نتیجه با Unicode LTR-isolate (U+2066…U+2069) محصور می‌شود تا داخلِ متنِ فارسیِ RTL
* جهتِ ارقام/ستاره به‌هم نریزد.
*/
public static function maskMobile(string $mobile): string
{
$mobile = self::toEnglishDigits($mobile);
if (strlen($mobile) < 6) {
return $mobile;
}
$r = $sum % 11;
return $r < 2 ? $check === $r : $check === 11 - $r;
$masked = substr($mobile, 0, 4) . '****' . substr($mobile, -2);
return "\u{2066}{$masked}\u{2069}";
}
public static function toEnglishDigits(string $s): string
@@ -222,9 +222,10 @@ class UserProfileController extends BaseController
$existing = $this->repository->findOneByNationalCode($code);
if ($existing !== null && ($current === null || $existing->getUuid() !== $current->getUuid())) {
$masked = InputValidator::maskMobile($existing->getUser()->getMobileNumber());
return $this->error(
ErrorCodes::ERR_PROFILE_NATIONAL_CODE_TAKEN,
'این کد ملی قبلاً برای کاربر دیگری ثبت شده است',
"این کد ملی قبلاً با شماره {$masked} ثبت شده است",
409,
'national_code'
);