Feature Description
Implement a mask method that applies a custom mask pattern to a string, allowing flexible formatting of IDs, document numbers, credit cards, phone numbers, and other structured data. The method should accept a string and a pattern where # represents a character from the input that should be preserved, and other characters are formatting separators.
Use Case
This feature would solve formatting challenges when:
- Formatting document numbers (CPF, CNPJ, RG, CNH) with custom masks beyond the built-in Brazilian maskers
- Applying regional-specific ID formats from different countries
- Formatting credit card numbers, account numbers, or any numeric sequences with specific patterns
- Creating custom phone number formats for different regions or business requirements
- Applying product codes, serial numbers, or inventory IDs with specific formatting rules
- Formatting any structured data that requires consistent visual presentation
Currently, the library provides specific Brazilian maskers (maskBrCpf, maskBrCnpj, maskBrCep, maskBrPhone), but there's no generic method for applying custom patterns. Developers need to implement their own formatting logic or use complex regex replacements for different ID formats.
This method would provide a flexible, reusable API for any custom masking pattern without creating individual transformers for each format.
Example scenarios:
// Custom CPF format (different from built-in)
$cpf = SM::mask('12345678901', '###.###.###-##');
// Output: '123.456.789-01'
// CNPJ format
$cnpj = SM:: mask('11223344556677', '##.###.###/####-##');
// Output: '11.223.344/5566-77'
// Credit card format
$card = SM::mask('1234567890123456', '#### #### #### ####');
// Output: '1234 5678 9012 3456'
// Custom phone format (international)
$phone = SM::mask('5511987654321', '+## (##) #####-####');
// Output: '+55 (11) 98765-4321'
// Product code
$product = SM::mask('ABCD1234XY', '####-####-##');
// Output: 'ABCD-1234-XY'
// Bank account
$account = SM::mask('12345678', '####-#');
// Output: '1234-5678'
Static Usage
use SSolWEB\StringMorpher\StringMorpher as SM;
// Apply CPF mask
$cpf = SM::mask('12345678901', '###.###.###-##');
echo $cpf; // '123.456.789-01'
// Apply CNPJ mask
$cnpj = SM::mask('11223344556677', '##.###.###/####-##');
echo $cnpj; // '11.223.344/5566-77'
// Apply credit card mask
$card = SM::mask('1234567890123456', '#### #### #### ####');
echo $card; // '1234 5678 9012 3456'
Fluent Usage
use SSolWEB\StringMorpher\StringMorpher as SM;
// Chain with data cleaning
$formatted = SM::make(' 12345678901 ')
->trim()
->onlyNumbers()
->mask('###.###.###-##');
echo $formatted; // '123.456.789-01'
// Format user input
$userInput = 'ABCD-1234-XY-99';
$clean = SM::make($userInput)
->onlyAlpha()
->limit(10)
->mask('####-####-##');
echo $clean; // 'ABCD-1234-XY'
// International phone number
$phone = SM::make('+55 (11) 98765-4321')
->onlyNumbers()
->mask('+## (##) #####-####');
echo $phone; // '+55 (11) 98765-4321'
Checklist
Feature Description
Implement a
maskmethod that applies a custom mask pattern to a string, allowing flexible formatting of IDs, document numbers, credit cards, phone numbers, and other structured data. The method should accept a string and a pattern where#represents a character from the input that should be preserved, and other characters are formatting separators.Use Case
This feature would solve formatting challenges when:
Currently, the library provides specific Brazilian maskers (
maskBrCpf,maskBrCnpj,maskBrCep,maskBrPhone), but there's no generic method for applying custom patterns. Developers need to implement their own formatting logic or use complex regex replacements for different ID formats.This method would provide a flexible, reusable API for any custom masking pattern without creating individual transformers for each format.
Example scenarios:
Static Usage
Fluent Usage
Checklist