alvarofpp / masks
该软件包已被弃用且不再维护。未建议替代包。
一个用于屏蔽变量或模型属性的软件包。
1.1.2
2020-08-23 09:30 UTC
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
一个用于屏蔽变量或模型属性的软件包。
内容
安装
通过composer安装
composer require alvarofpp/masks
用法
此软件包当前包含
- 屏蔽助手。
- 在模型属性中应用屏蔽。
屏蔽助手
mask(string $mask, string $value): string
您可以使用此助手应用屏蔽到任何您想要的价值。示例
<?php $cpf = '12345678901'; $mask = '###.###.###-##'; echo mask($mask, $cpf); // Result: 123.456.789-01
在模型属性中应用屏蔽
您可以在模型中使用MaskAttributes
特性来在属性中应用屏蔽。以上一节中的示例为例,我们可以自动化屏蔽,如下所示
<?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 => '(##) ###-###-###', ], ]; // ... }
因此,如果$user->cpf
的值为12345678901
,您可以使用$user->cpf_masked
来获取屏蔽后的值。
在$user->phone
的情况下,屏蔽将取决于值的长度,例如:如果电话号码为1234567890
,则屏蔽后的值将为(12) 3456-7890
,但如果电话号码为12345678901
,则屏蔽后的值将为(12) 345-678-901
。
默认情况下,MaskAttributes
使用后缀_masked
,您可以通过声明$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' => '###.###.###-##', ]; // ... }
这样,您可以使用$user->cpf_formatted
来获取屏蔽后的值。
贡献
欢迎贡献。Fork,改进,并提交pull request。对于错误、改进建议或其他问题,请创建问题。