alvarofpp / masks
A package to mask your variables or model's attributes.
Installs: 13 008
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.0
Requires (Dev)
- laravel/laravel: >=6.0
This package is auto-updated.
Last update: 2024-02-23 17:08:16 UTC
README
A package to mask your variables or model's attributes.
Contents
Install
Install via composer:
composer require alvarofpp/masks
Usage
This package currently contains:
- Mask helper.
- Applies mask in model's attributes.
Mask helper
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
Applies mask in model's attributes
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.
Contributing
Contributions are more than welcome. Fork, improve and make a pull request. For bugs, ideas for improvement or other, please create an issue.