kbs1/laravel-abbreviations

为您的应用程序提供缩写/首字母缩略词支持。

v1.0 2017-07-16 16:15 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:31:14 UTC


README

简单地为任何模型或通用缩写/首字母缩略词创建可读的缩写。

安装

此包支持自动发现。对于laravel版本 < 5.5,在config/app.php的providers部分添加以下行

Kbs1\Abbreviations\AbbreviationsServiceProvider::class,

config/app.php的aliases部分添加以下行

'Abbreviation' => Kbs1\Abbreviations\Abbreviation::class,

用法

在laravel模型中,使用以下特质

use \Kbs1\Abbreviations\HasAbbreviation;

现在您可以通过获取$model->abbreviation属性来简单地获取模型的缩写。

自定义缩写属性

特质默认使用模型的name属性。您可以通过在模型代码中重写abbreviationAttribute方法来覆盖此行为。

通用缩写/首字母缩略词

在任何应用程序位置,您都可以调用Abbreviation::make($string)abbreviate($string)辅助函数来动态生成缩写。

配置

使用php artisan vendor:publish --tag=abbreviations发布配置后,您可以定义生成的缩写最大长度(默认3),生成的缩写大小写(uppercaselowercaseoriginal),是否希望将数字包含在生成的缩写中(默认false),以及是否在处理之前从源字符串中删除现有的缩写(默认true)。

有关更多详细信息,请参阅已发布的配置文件中的注释。

示例

使用默认配置

  • abbreviate('somestring') == 'SOM'
  • abbreviate('somestring w/ meaning') == 'SM'
  • abbreviate('somestring w/o meaning') == 'SM'
  • abbreviate('somestring incl. others') == 'SO'
  • abbreviate('Ing. Name Surname, PhD.') == 'NS'
  • abbreviate('[7] => somestring') == 'SOM'
  • abbreviate('K-9 mail widget') == 'KMW'
  • abbreviate('GPSLoc') == 'GL'
  • abbreviate('14Logistics internal system') == 'LIS'
  • abbreviate('#1411: fix fooBar status') == 'FFB'
  • abbreviate('#1234: deploy') == 'DEP'
  • abbreviate('#1234:777: 500 words 7 characters long') == 'WCL'
  • abbreviate('#1234:777: 500 words 700b') == 'WB'
  • abbreviate('#1234:777: 500') == '#12'
  • abbreviate('123456abcde') == 'ABC'

允许数字

  • abbreviate('K-9 mail widget') == 'KMW' (非数字匹配始终优先)
  • abbreviate('[7] => somestring') == '7S'
  • abbreviate('#1234: deploy') == '1D'
  • abbreviate('#1234:777: 500 words 700b') == '175'
  • abbreviate('#1234:777: 500') == '175'
  • abbreviate('123456abcde') == '123'