A package to mask your variables or model's attributes.
Install via composer:
composer require alvarofpp/masks
This package currently contains:
- Mask helper.
- Applies mask in model's attributes.
mask(string $mask, string $value): string
You can use this helper to apply mask in any value that you want. Example:
<?php
$cpf = '12345678901';
$mask = '###.###.###-##';
echo mask($mask, $cpf);
// Result: 123.456.789-01
You can use the trait MaskAttributes
in your model to apply masks in the attributes.
Taking the example in the previous section, we can automate the masks such as:
<?php
namespace App;
use Alvarofpp\Masks\Traits\MaskAttributes;
// ...
class User extends Authenticatable
{
use MaskAttributes;
/**
* The attributes that should be masked.
*
* @var array
*/
protected $masks = [
'cpf' => '###.###.###-##',
'phone' => [
10 => '(##) ####-####',
11 => '(##) ###-###-###',
],
];
// ...
}
So if the value of $user->cpf
is 12345678901
, you can use $user->cpf_masked
to take the value masked.
In the $user->phone
case, the mask will depend on the length of the value, for example:
if phone has 1234567890
, the value masked will be (12) 3456-7890
,
but if phone has 12345678901
, the value masked will be (12) 345-678-901
.
By default, MaskAttributes
use the suffix _masked
, you can change the suffix declaring $maskSuffix
:
<?php
namespace App;
use Alvarofpp\Masks\Traits\MaskAttributes;
// ...
class User extends Authenticatable
{
use MaskAttributes;
/**
* Indicates the suffix of masked attributes.
*
* @var string
*/
protected $maskSuffix = '_formatted';
/**
* The attributes that should be masked.
*
* @var array
*/
protected $masks = [
'cpf' => '###.###.###-##',
];
// ...
}
That way, you use $user->cpf_formatted
to grab the masked value.
Contributions are more than welcome. Fork, improve and make a pull request. For bugs, ideas for improvement or other, please create an issue.