macmotp / codegen-laravel
生成易于理解代码 - Laravel 扩展
v0.3.0
2023-07-08 07:42 UTC
Requires
- php: >=8.1
- macmotp/codegen: ^0.2.0
- spatie/laravel-package-tools: ^1.15
Requires (Dev)
- orchestra/testbench: ^v8.5
- phpunit/phpunit: ^10.2
This package is auto-updated.
Last update: 2024-09-08 10:12:45 UTC
README
生成易于理解代码
适用于基于名称、收据号码、唯一参考生成推荐代码。
这是用于生成易于理解代码的 Laravel 扩展包 Codegen - Generate Human Friendly Codes
要求
- PHP >=8.1
- Laravel >= 5
如果你使用 PHP 7.4,请确保使用 v0.1.2
如果你使用 PHP 8.0,请确保使用 v0.2.0
安装
你可以通过 composer 安装此包
composer require macmotp/codegen-laravel
用法
通过应用特质,从任何模型创建语义化和清洗过的参考代码
use Illuminate\Database\Eloquent\Model; use Macmotp\HasCodegen; class User extends Model { use HasCodegen; protected $fillable = [ 'name', 'code', ]; /** * Attribute of the model used to generate the code * * @return string */ protected function buildCodeFrom(): string { return $this->name; } }
在 creating
事件中,它将为指定的列生成可读性强的代码
$user = User::create([ 'name' => 'Bob McLovin', ]); dump($user->code); // (string) 'BBMCLV';
配置
发布默认配置
创建 config/codegen.php
文件,在此文件中可以调整一些设置
<?php // config for Macmotp/HasCodegen return [ /* |-------------------------------------------------------------------------- | The attribute of the model to build the code from. | For example, if your model has a column 'name' you can build the code from this attribute. | If empty, will generate random codes. |-------------------------------------------------------------------------- */ 'build-from' => '', /* |-------------------------------------------------------------------------- | The column use to save the code into the model. |-------------------------------------------------------------------------- */ 'code-column' => 'code', /* |-------------------------------------------------------------------------- | The length of the code to generate. |-------------------------------------------------------------------------- */ 'code-length' => 6, /* |-------------------------------------------------------------------------- | Sanitize level. | 1. Low/Default: will filter out anything is not a letter or a digit; | 2. Medium: will filter out (O - 0 - Q - I - 1) characters; | 3. High: will filter out (2 - Z - 4 - A - 5 - S - 8 - B - U - V - Y) characters; | Levels are inclusive, e.g. the highest level will apply also regex of level low and medium. |-------------------------------------------------------------------------- */ 'sanitize-level' => 1, /* |-------------------------------------------------------------------------- | Prepend a string. |-------------------------------------------------------------------------- */ 'prepend' => '', /* |-------------------------------------------------------------------------- | Append a string. |-------------------------------------------------------------------------- */ 'append' => '', /* |-------------------------------------------------------------------------- | Maximum accepted number of attempts for the generation. |-------------------------------------------------------------------------- */ 'max-attempts' => 10000, ];
按模型自定义配置
覆盖单个模型的自定义配置
use Illuminate\Database\Eloquent\Model; use Macmotp\HasCodegen; class Foo extends Model { use HasCodegen; protected $fillable = [ 'title', 'reference', ]; /** * Attribute of the model used to generate the code * * @return string */ protected function buildCodeFrom(): string { return $this->title; } /** * Column used to save the unique code * * @return string */ protected function getCodeColumn(): string { return 'reference'; } /** * Get char length of the code * * @return int */ protected function getCodeLength(): int { return 12; } /** * Force to prepend this portion of string in the code * * @return string */ protected function prependToCode(): string { return 'PR'; } /** * Force to append this portion of string in the code * * @return string */ protected function appendToCode(): string { return 'AP'; } /** * Get the sanitize level to apply * * @return int */ protected function getCodeSanitizeLevel(): int { return 3; // Level High } }
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请查阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。