axn / tool-kit-for-laravel
包含Laravel框架的一系列实用工具。
Requires
- php: ^8.2
- composer/semver: ^3.0.0
- forxer/generic-term-translations-for-laravel: ^1.8
- laravel/framework: ^10.0 || ^11.0
Requires (Dev)
- driftingly/rector-laravel: ^1.2
- laravel/pint: ^1.16.0
- rector/rector: ^1.1.0
- dev-master
- 10.2.0
- 10.1.0
- 10.0.3
- 10.0.2
- 10.0.1
- 10.0.0
- 9.x-dev
- 9.1.0
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 8.x-dev
- 8.1.0
- 8.0.0
- 7.7.2
- 7.7.1
- 7.7.0
- 7.6.0
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.1
- 7.1.0
- 7.0.1
- 7.0.0
- 6.9.0
- 6.8.0
- 6.7.0
- 6.6.0
- 6.5.0
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.1
- 6.0.0
- v5.x-dev
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0.0
- v4.x-dev
- 4.2.0
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0
- 2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-develop
This package is auto-updated.
Last update: 2024-09-05 17:42:34 UTC
README
包含Laravel框架的一系列实用工具。
安装
使用Composer
composer require axn/tool-kit-for-laravel
要使用这些工具中的某些,您必须已正确安装此包所要求的先决条件包 forxer/generic-term-translations-for-laravel(因此已存在)。
使用 Laravel Lang 的地区发布者来添加/更新/重置或删除翻译
- 如果您从未使用过 Laravel Lang: 添加地区
- 如果您已经使用 Laravel Lang:只需 更新地区
辅助工具
carbon()
从日期字符串、DateTime实例或时间戳创建Carbon实例。
/** * Create a Carbon instance from a date string, a DateTime instance or a timestamp. * * @param \DateTime|int|string|null $date * @param string|null $fromFormat * @param \DateTimeZone|string|null $tz * @return \Illuminate\Support\Carbon * */ function carbon($date = null, $fromFormat = null, $tz = null)
以下是一些示例。
使用Carbon
use Carbon\Carbon; $date = Carbon::now(); $date = Carbon::now('Europe/Paris'); $date = Carbon::createFromFormat('Y-m-d H:i', '2018-06-18 09:30'); $date = Carbon::createFromFormat('Y-m-d H:i', '2018-06-18 09:30', 'Europe/Paris'); $date = new Carbon('Thursday, June 18 2015 9:30:00'); $date = new Carbon('Thursday, June 18 2015 9:30:00', 'Europe/Paris'); $date = Carbon::createFromTimestamp(1434619800)
使用辅助函数的等效操作
$date = carbon(); $date = carbon(tz: 'Europe/Paris'); $date = carbon('2018-06-18 09:30', 'Y-m-d H:i'); $date = carbon('2018-06-18 09:30', 'Y-m-d H:i', 'Europe/Paris'); $date = carbon('Thursday, June 18 2015 9:30:00'); $date = carbon('Thursday, June 18 2015 9:30:00', tz: 'Europe/Paris'); $date = carbon(1434619800) $date = carbon(1434619800, tz: 'Europe/Paris')
collect_models()
创建Eloquent模型的集合。
/** * Create an Eloquent collection of Eloquent models. * * @param array $models * @return EloquentCollection */ function collect_models(array $models)
str_html()
创建一个 Illuminate\Support\HtmlString
实例。
$str = '<a>An HTML string</p>'; $htmlString = str_html($str); // Alias of $htmlString = new Illuminate\Support\HtmlStringHtmlString($str);
linebreaks()
将所有换行符转换为UNIX格式。
将 "\r\n"
和 "\r"
替换为 "\n"
nl_to_p()
将换行符转换为HTML段落 <p>
。
$str = "a text with \n new lines \n\n again \n\n\n and again"; nl_to_p($str); // <p>a text with <br> new lines </p><p> again </p><p> and again</p>
nl_to_br()
PHP原生函数 nl2br()
的别名。
$str = "a text with \n new lines \n\n again \n\n\n and again"; nl_to_br($str) // a text with <br> new lines <br><br> again <br><br><br> and again
number_formated()
以当前语言格式返回一个数字。
$number = '123456789.101112'; $numberFormated = number_formated($number, 2); // fr: 123 456 789,10 // en: 123,456,789.10
number_fr()
以法语格式返回一个数字。
compute_dec_to_time()
十进制到时间计算,返回包含小时、分钟和秒的数组。
$number = '1.75'; $time = compute_dec_to_time($number); // [ // 'hours' => 1.0, // 'minutes' => 45.0, // 'seconds' => 0, // ]
convert_dec_to_time()
十进制到时间转换。输出可以通过 sprintf
格式更改。
$number = '1.75'; $time = convert_dec_to_time($number); // 01:45:00 $time = convert_dec_to_time($number, '%sh%s'); // 01h45 $time = convert_dec_to_time($number, '%2$s:%3$s'); // 45:00
human_readable_bytes_size()
将字节数转换为可读的本地化大小。
$size = human_readable_bytes_size(2048); // fr: 2 ko // en: 2 kB $size = human_readable_bytes_size(2048*1024); // fr: 2 Mo // en: 2 MB $size = human_readable_bytes_size(2048*1024*10000, 2); // fr: 19,53 Go // en: 19.53 GB
mime_type_to_fa5_class()
为特定的MIME类型返回一个Font Awesome文件图标类。
trans_ucfirst()
将给定消息翻译为首字母大写。
is_valid_model()
指示模型类是否可实例化并且是 Illuminate\Database\Eloquent\Model
的实例。
semverToId()
将semver版本号转换为数字标识符。请注意:不考虑“预发布版”(RC、beta等)。
$phpVersion = "8.2.14"; $phpVersionId = semverToId($phpVersion); // 80214 $laravelVersion = " 10.38.2"; $laravelVersionId = semverToId($laravelVersion); // 103802
这对于在数据库中对数字列而不是文本列进行优化比较、搜索和排序非常有用。
Blade指令
@nltop()
将换行符转换为HTML段落 <p>
。
@nltop ("a text with \n new lines \n\n again \n\n\n and again")
显示
<p>a text with <br> new lines </p><p> again </p><p> and again</p>
@nltobr()
将换行符转换为HTML <br>
。
@nltobr ("a text with \n new lines \n\n again \n\n\n and again")
显示
a text with <br> new lines <br><br> again <br><br><br> and again
组件
添加必填字段的指示器
用于显示必填字段标记(例如,在标签标签中)
<x-required-field-marker />
显示
<span class="required-field-marker"> *<span>required</span> </span>
您可以更改默认符号 "*"(一个星号)为您选择的标记符号
<x-required-field-marker :symbol="⚠" />
例如,您可以这样设计它
.required-field-marker { color: #da1313; } .required-field-marker > span { /* Bootstrap styles of .visually-hidden class */ position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important; }
在您的表单中,您可以以这种方式指示必填字段
{!! trans('misc.info_required_fields'); !!} <x-required-field-marker />
枚举
环境应用程序
此包提供了一个名为 AppEnv
的实用枚举。这允许标准化环境名称。
实际上,例如,一些项目统一使用环境 "prod" 和 "production";或者甚至 "preprod" 和 "pre-production",更糟的是:"pre-prod"。
use Axn\ToolKit\Enums\AppEnv; AppEnv::production; AppEnv::preproduction; AppEnv::test; AppEnv::local; AppEnv::unknown;
从字符字符串创建枚举实例
use Axn\ToolKit\Enums\AppEnv; $appEnv = AppEnv::from('pre-prod'); // AppEnv::preprod $appEnv = AppEnv::from(app()->environment()); // enum AppEnv
测试环境类型
use Axn\ToolKit\Enums\AppEnv; AppEnv::isProd('pre-prod'); // false AppEnv::isPreprod('pre-prod'); // true AppEnv::isTest('pre-prod'); // false AppEnv::isLocal('pre-prod'); // false if (AppEnv::isProd(app()->environment())) { // do something in "prod" }
存在反向方法
use Axn\ToolKit\Enums\AppEnv; AppEnv::isNotProd('pre-prod'); // true AppEnv::isNotPreprod('pre-prod'); // false AppEnv::isNotTest('pre-prod'); // true AppEnv::isNotLocal('pre-prod'); // true
检索枚举中定义的环境值
use Axn\ToolKit\Enums\AppEnv; AppEnv::prodNames(); // ['prod', 'production'] AppEnv::preprodNames(); // ['preprod', 'pre-prod', 'preproduction', 'pre-production'] AppEnv::testNames(); // ['test', 'tests', 'testing', 'stage', 'staging'] AppEnv::localNames(); // ['local', 'develop', 'dev']
礼貌
使用 Axn\ToolKit\Enums\Civilities
可用枚举来处理礼貌。
use Axn\ToolKit\Enums\Civilities;
@待办:需要记录此内容