Files
prn2026/app/Support/SensitiveData.php
2026-06-03 08:51:22 +08:00

55 lines
1.2 KiB
PHP

<?php
namespace App\Support;
class SensitiveData
{
public static function maskIc(?string $value): string
{
return self::mask($value, 4, 4);
}
public static function maskAccount(?string $value): string
{
return self::mask($value, 3, 4);
}
public static function maskPhone(?string $value): string
{
return self::mask($value, 3, 3);
}
public static function maskName(?string $value): string
{
$normalized = trim((string) $value);
if ($normalized === '') {
return '-';
}
$length = mb_strlen($normalized);
$visible = min(3, $length);
return mb_substr($normalized, 0, $visible).str_repeat('*', max(0, $length - $visible));
}
private static function mask(?string $value, int $prefix, int $suffix): string
{
$normalized = trim((string) $value);
if ($normalized === '') {
return '-';
}
$length = mb_strlen($normalized);
if ($length <= ($prefix + $suffix)) {
return str_repeat('*', $length);
}
return mb_substr($normalized, 0, $prefix)
.str_repeat('*', $length - $prefix - $suffix)
.mb_substr($normalized, -$suffix);
}
}