maize-tech / laravel-helpers
Laravel 辅助工具
Requires
- php: ^8.1
- illuminate/database: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.7
- larastan/larastan: ^2.0.1
- nunomaduro/collision: ^7.10.0|^8.1.1
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
此仓库包含适用于大多数使用 Laravel 的应用程序的一些有用辅助工具。
安装
您可以通过 composer 安装此包
composer require maize-tech/laravel-helpers
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="helpers-config"
这是已发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Helper macros |-------------------------------------------------------------------------- | | Here you may specify the full list of helper macros which will automatically | be registered on boot. | The key defines the method name, whereas the value should be the | fully qualified name of the invokable class. | */ 'macros' => Helper::defaultMacros()->merge([ // 'methodName' => App\Example\ExampleClass::class, ])->toArray(), ];
使用方法
要使用此包,您可以简单地调用下面的 hlp()
辅助函数,后面跟以下列出的可用方法之一。如果需要,您也可以直接调用静态方法。
以下是一个使用辅助函数和静态方法的示例
hlp()->sanitizeUrl('mywebsite.com'); // using the helper function \Maize\Helpers\Helper::sanitizeUrl('mywebsite.com'); // using the static method
可用方法
anonymizeFilename
classUsesTrait
instanceofTypes
isUrl
modelKeyName
morphClassOf
resolveMorphedModel
paginationLimit
pipe
sanitizeArrayOfStrings
sanitizeString
sanitizeUrl
anonymizeFilename
anonymizeFilename
函数返回给定文件随机名称后跟其扩展名。
string $filename = 'my-custom-file.xml'; // returns a UUID string followed by the file extension // e.g. 'd437fd98-68d1-4874-b0e7-fac06e587083.xml' hlp()->anonymizeFilename($filename);
classUsesTrait
classUsesTrait
函数返回一个类对象或名称是否使用给定的特性。
use App\Models\User; use Exception; use Illuminate\Database\Eloquent\Factories\HasFactory; $model = User::firstOrFail(); hlp()->classUsesTrait(HasFactory::class, $model); // returns true hlp()->classUsesTrait(HasFactory::class, User::class); // returns true hlp()->classUsesTrait(Exception::class, $model); // returns false hlp()->classUsesTrait(Exception::class, User::class); // returns false
instanceofTypes
instanceofTypes
函数返回一个类对象是否是给定类型之一的实例。
use App\Models\User; use Exception; use Illuminate\Database\Eloquent\Model; $model = User::firstOrFail(); hlp()->instanceofTypes($model, Model::class); // returns true hlp()->instanceofTypes($model, Exception); // returns false hlp()->instanceofTypes($model, [ Model::class, Exception::class, ]); // returns true
isUrl
isUrl
函数返回一个字符串是否是有效的 URL。
hlp()->isUrl('https://my-application.test'); // returns true hlp()->isUrl('not-an-url'); // returns false
modelKeyName
modelKeyName
函数返回给定模型对象或类名称的键名称。
use App\Models\User; $model = User::firstOrFail(); hlp()->modelKeyName($model); // returns 'id' hlp()->modelKeyName(User::class); // returns 'id'
morphClassOf
morphClassOf
函数返回给定模型对象或类名称的形态类名称。
use App\Models\User; use Illuminate\Database\Eloquent\Relations\Relation; $model = User::firstOrFail(); hlp()->morphClassOf($model); // returns 'App\Models\User' hlp()->morphClassOf(User::class); // returns 'App\Models\User' Relation::enforceMorphMap([ 'user' => User::class, ]); hlp()->morphClassOf($model); // returns 'user' hlp()->morphClassOf(User::class); // returns 'user'
resolveMorphedModel
resolveMorphedModel
函数返回给定形态类名称的完全限定模型类名称。
您可以传递形态类名称的单数或复数名称。
hlp()->resolveMorphedModel('users'); // returns 'App\Models\User' hlp()->resolveMorphedModel('user'); // returns 'App\Models\User'
paginationLimit
paginationLimit
函数返回每页的项目数量。
当处理需要分页的查询时非常有用,并允许您定义默认的分页限制和每页的最大项目数量。
它还会检查请求的查询字符串中是否包含 limit
参数:如果是,则给定的限制将覆盖默认限制。
use App\Models\Article; // use the default pagination limit (16 items) // GET /api/articles Article::paginate( hlp()->paginationLimit() // returns 16 items ); // use the pagination limit given by the request query string // GET /api/articles?limit=20 Article::paginate( hlp()->paginationLimit() // returns 20 items ); // provide a custom default pagination limit // GET /api/articles Article::paginate( hlp()->paginationLimit(30) // returns 30 items ); // when defined, the request query string limit overrides the default limit // GET /api/articles?limit=20 Article::paginate( hlp()->paginationLimit(30) // returns 20 items ); // provide a max limit of items for each page // GET /api/articles?limit=200 Article::paginate( hlp()->paginationLimit(16, 50) // returns 50 items );
pipe
pipe
函数允许您轻松集成 Laravel 管道。
hlp()->pipe('test', [ \Maize\Helpers\Tests\Support\Actions\Uppercase::class, ]); // returns 'TEST' hlp()->pipe('test', [ \Maize\Helpers\Tests\Support\Actions\Uppercase::class, \Maize\Helpers\Tests\Support\Actions\Reverse::class, ]); // returns 'TSET' hlp()->pipe('', []) // returns an empty string
sanitizeArrayOfStrings
sanitizeArrayOfStrings
函数通过从两端删除所有 HTML 标签和空白字符来清理字符串数组。
hlp()->sanitizeArrayOfStrings([' test ', ' test ']); // returns '['test', 'test']' hlp()->sanitizeArrayOfStrings(['a' => ' test </h1> ', 'b' => ' test </h1> ']) // returns ['a' => 'test', 'b' => 'test'] hlp()->sanitizeArrayOfStrings(['a' => '']); // returns an empty array
sanitizeString
sanitizeString
函数通过从两端删除所有 HTML 标签和空白字符来清理字符串。
hlp()->sanitizeString('<h1> test </h1>'); // returns 'test' hlp()->sanitizeString(' <h1> test '); // returns 'test' hlp()->sanitizeString('<br />') // returns an empty string
sanitizeUrl
sanitizeUrl
函数在未设置协议的情况下将指定的 URL 预先添加 https
协议。
hlp()->sanitizeUrl('http://innovation.h-farm.com'); // returns 'http://innovation.h-farm.com' hlp()->sanitizeUrl('innovation.h-farm.com'); // returns 'https://innovation.h-farm.com' hlp()->sanitizeUrl('') // returns an empty string
添加自定义辅助方法
如果需要,您可以轻松添加自己的辅助方法。
您只需定义自己的自定义辅助类并实现 HelperMacro
接口即可。
<?php namespace App\Helpers\Macros; use Maize\Helpers\HelperMacro; class Ping implements HelperMacro { public function __invoke(): \Closure { return function (): string { return 'pong'; }; } }
之后,您可以将方法添加到 config/helpers.php
的 macros
属性中。
return [ /* |-------------------------------------------------------------------------- | Helper macros |-------------------------------------------------------------------------- | | Here you may specify the full list of helper macros which will automatically | be registered on boot. | The key defines the method name, whereas the value should be the | fully qualified name of the invokable class. | */ 'macros' => Helper::defaultMacros()->merge([ 'ping' => \App\Helpers\Macros\Ping::class, ])->toArray(), ];
或者,如果您需要在运行时定义自定义辅助工具,您可以调用 Helper
类的 macro
静态方法。
use Maize\Helpers\Helper; Helper::macro('ping', fn (): string => 'pong');
现在,您可以使用 hlp()
辅助函数调用您自己的自定义辅助方法。
hlp()->ping(); // returns "pong";
测试
composer test
变更日志
请参阅变更日志获取最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请查阅我们的安全策略了解如何报告安全漏洞。
致谢
许可协议
MIT许可(MIT)。请参阅许可文件获取更多信息。