finagin/laravel-extra-support

Laravel 的额外支持方法

0.1.0 2021-07-15 16:25 UTC

This package is auto-updated.

Last update: 2024-09-15 23:34:10 UTC


README

StyleCI GitHub Actions GitHub Issues Total Downloads Latest Stable Version Software License

安装

composer require finagin/laravel-extra-support

使用方法

Str::randomWithExclude()

返回排除特定字符的随机字符串。

use Illuminate\Support\Str;

Str::randomWithExclude();
Str::randomWithExclude(15);
Str::randomWithExclude(16, ['a', 'b', 'c']);
Str::randomWithExclude(16, 'abc');

Str::randomAlpha()

返回不包含数字的随机字符串。

use Illuminate\Support\Str;

Str::randomAlpha();
Str::randomAlpha(15);

自定义

<?php

namespace App\Services;

use Finagin\ExtraSupport\Services\MacrosRegistrar;
use Illuminate\Support\Str;

class CustomMacrosRegistrar extends MacrosRegistrar
{
    /**
     * @return \Illuminate\Support\Collection|array
     */
    public function additionalRegisters()
    {
        return [
            '\\Illuminate\\Support\\Str@randomExcludeSimilar' => 'registerMacroRandomExcludeSimilar',
        ];
    }

    protected function registerMacroRandomExcludeSimilar()
    {
        Str::macro('randomExcludeSimilar', static function ($length = 16) {
            return Str::randomWithExclude($length, ['1', 'l', '0', 'O']);
        });
    }
}

在配置中,将 registrar 替换为新注册器类

<?php

return [

    'registrar' => \App\Services\CustomMacrosRegistrar::class,
    
    // ...
];

最后,如果您需要,请将依赖项添加到相应的部分

<?php

return [
    // ...
    'dependencies' => [
        // ...
        '\\Illuminate\\Support\\Str@randomExcludeSimilar' => [
            '\\Illuminate\\Support\\Str@randomWithExclude',
        ],
        // ...
    ],
    // ...
];

许可证

MIT 许可证 (MIT)。请参阅许可证文件获取更多信息。