finagin / laravel-extra-support
Laravel 的额外支持方法
0.1.0
2021-07-15 16:25 UTC
Requires
- php: ^7.2.5|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/php-code-coverage: *
This package is auto-updated.
Last update: 2024-09-15 23:34:10 UTC
README
安装
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', ], // ... ], // ... ];