Files
rkm-conectasus-ci-sandbox/api/app/Utils/MaskUtil.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

187 lines
4.3 KiB
PHP

<?php
namespace App\Utils;
use App\Enums\Geral\DocumentoTipo;
class MaskUtil
{
private const DOCUMENT_TYPES = [
DocumentoTipo::CNPJ => 'Cnpj',
DocumentoTipo::CPF => 'Cpf',
DocumentoTipo::RG => 'Rg',
DocumentoTipo::CNH => 'Cnh',
DocumentoTipo::CNS => 'Cns',
];
public static function getDocumentMasked(int $documentoTipoId, $numero)
{
return self::{'get' . self::DOCUMENT_TYPES[$documentoTipoId] . 'Masked'}($numero);
}
public static function getCpfMasked($cpf)
{
return preg_replace(
'/(\d{3})(\d{3})(\d{3})(\d{2})/',
'$1.$2.$3-$4',
self::getOnlyDigits($cpf)
);
}
public static function getCnpjMasked($cnpj)
{
return preg_replace(
'/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/',
'$1.$2.$3/$4-$5',
self::getOnlyDigits($cnpj)
);
}
public static function getCnhMasked($cnh)
{
return $cnh;
}
public static function getCnsMasked($cns)
{
return preg_replace(
'/(\d{3})(\d{4})(\d{4})(\d{4})/',
'$1 $2 $3 $4',
self::getOnlyDigits($cns)
);
}
public static function getDnvMasked($cns)
{
return preg_replace(
'/(\d{2})(\d{8})(\d{1})/',
'$1-$2-$3',
self::getOnlyDigits($cns)
);
}
public static function getOnlyDigits(&$value)
{
return preg_replace('/[^\d]/', '', (string) $value);
}
public static function getPhoneMasked($number)
{
$number = self::getOnlyDigits($number);
switch (strlen($number)) {
case 10: {
return preg_replace(
'/(\d{2})(\d{4})(\d{4})/',
'($1) $2-$3',
$number
);
}
case 11: {
return preg_replace(
'/(\d{2})(\d{5})(\d{4})/',
'($1) $2-$3',
$number
);
}
default: {
return $number;
}
}
}
public static function getCepMasked($cep)
{
return preg_replace(
'/(\d{2})(\d{3})(\d{3})/',
'$1.$2-$3',
self::getOnlyDigits($cep)
);
}
public static function getNumeroCertidaoMasked($numero)
{
return preg_replace(
'/(\d{6})(\d{2})(\d{2})(\d{4})(\d{1})(\d{5})(\d{3})(\d{7})(\d{2})/',
'$1 $2 $3 $4 $5 $6 $7 $8 $9',
self::getOnlyDigits($numero)
);
}
public static function getProcedimentoMasked($procedimento)
{
return preg_replace(
'/(\d{2})(\d{2})(\d{2})(\d{3})(\d{1})/',
'$1.$2.$3.$4-$5',
$procedimento
);
}
public static function getCidMasked($cid)
{
return preg_replace(
'/(\w{3})(\w{1})/',
'$1.$2',
$cid
);
}
public static function getRgMasked($rg)
{
return $rg;
}
public static function getNumeroAutorizacaoFornecimentoMasked($numero)
{
return preg_replace(
'/(\d{5})(\d{4})/',
'$1/$2',
self::getOnlyDigits($numero)
);
}
public static function getPlacaMasked($placa)
{
return preg_replace(
'/([A-Z]{3})(([0-9]{4})|([0-1]{1}[A-Z]{1}[0-9]{2}))/',
'$1-$2',
self::getDataUnmasked($placa)
);
}
public static function getDataUnmasked($value)
{
return preg_replace('/[^\w]/', '', (string) $value);
}
public static function maskNome($nome)
{
$partes = preg_split('/\s+/', trim($nome));
$primeiro = array_shift($partes);
$iniciais = array_map(fn($p) => mb_substr($p, 0, 1) . '.', $partes);
return (substr($primeiro, 0, 3) . (strlen($primeiro) > 3 ? '.' : '')) . (count($iniciais) ? ' ' . implode(' ', $iniciais) : '');
}
public static function maskDocumento($valor)
{
if (empty($valor)) return null;
$tamanho = strlen($valor);
if ($tamanho <= 5) {
return $valor;
}
$inicio = substr($valor, 0, 3);
$fim = substr($valor, -2);
$oculto = str_repeat('*', $tamanho - 5);
return $inicio . $oculto . $fim;
}
}