定制 alvarofpp/masks 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alvarofpp/masks

最新稳定版本:1.1.2

Composer 安装命令:

composer require alvarofpp/masks

包简介

A package to mask your variables or model's attributes.

README 文档

README

Packagist Version License

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.

统计信息

  • 总下载量: 15.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-01