定制 maize-tech/laravel-helpers 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maize-tech/laravel-helpers

Composer 安装命令:

composer require maize-tech/laravel-helpers

包简介

Laravel Helpers

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This repository contains some useful helpers for most applications using Laravel.

Installation

You can install the package via composer:

composer require maize-tech/laravel-helpers

You can publish the config file with:

php artisan vendor:publish --tag="helpers-config"

This is the content of the published config file:

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(),

];

Usage

To use the package, you can simply call the hlp() helper function, followed by one of the Available methods methods listed below. If needed, you could also call the static method directly.

Here's an example using both the helper function and the static method:

hlp()->sanitizeUrl('mywebsite.com'); // using  the helper function

\Maize\Helpers\Helper::sanitizeUrl('mywebsite.com'); // using the static method

Available methods

anonymizeFilename

The anonymizeFilename function returns a randomized name of the given file followed by its extension.

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

The classUsesTrait function returns whether a class object or name uses the given trait or not.

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

The instanceofTypes function returns whether a class object is an instance of at least one of the given types or not.

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

The isUrl function returns whether a string is a valid URL or not.

hlp()->isUrl('https://my-application.test'); // returns true

hlp()->isUrl('not-an-url'); // returns false

modelKeyName

The modelKeyName function returns the key name of a given model object or class name.

use App\Models\User;

$model = User::firstOrFail();

hlp()->modelKeyName($model); // returns 'id'

hlp()->modelKeyName(User::class); // returns 'id'

morphClassOf

The morphClassOf function returns the morph class name of a given model object or class name.

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

The resolveMorphedModel function returns the fully qualified model class name of a given morph class name.

You can either pass the singular or plural name of the morph class name.

hlp()->resolveMorphedModel('users'); // returns 'App\Models\User'

hlp()->resolveMorphedModel('user'); // returns 'App\Models\User'

paginationLimit

The paginationLimit function returns the amount of items per page.

It is useful when working with queries who need a pagination, and allows to define a default pagination limit and the max amount of items per page.

It will also check whether the request's query string contains a limit parameter: if true, the given limit overrides the default 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

The pipe function allows you to integrate Laravel pipelines with ease. The first parameter is the object to be sent through the pipeline, while the second is the list of pipes.

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

The sanitizeArrayOfStrings function sanitizes an array of strings by removing all HTML tags and whitespace from both ends. When passing an associative array, it also filters all keys with an empty value.

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

The sanitizeString function sanitizes a string by removing all HTML tags and whitespace from both ends.

hlp()->sanitizeString('<h1>   test   </h1>'); // returns 'test'

hlp()->sanitizeString('   <h1>   test   '); // returns 'test'

hlp()->sanitizeString('<br />') // returns an empty string

sanitizeUrl

The sanitizeUrl function prepends the specified url with the https protocol if none is set.

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

Adding custom helper methods

If needed, you can easily add your own helper methods.

All you have to do is define your custom helper class and implement the HelperMacro interface:

<?php

namespace App\Helpers\Macros;

use Maize\Helpers\HelperMacro;

class Ping implements HelperMacro
{
    public function __invoke(): \Closure
    {
        return function (): string {
            return 'pong';
        };
    }
}

After that, you can add your method to the macros attribute from config/helpers.php:

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(),

];

Alternatively, if you need to define custom helpers at runtime, you can call the macro static method of the Helper class:

use Maize\Helpers\Helper;

Helper::macro('ping', fn (): string => 'pong');

You can now call your custom helper method with the hlp() helper method:

hlp()->ping(); // returns "pong";

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

maize-tech/laravel-helpers 适用场景与选型建议

maize-tech/laravel-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.19k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2022 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「helpers」 「laravel」 「maize-tech」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maize-tech/laravel-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maize-tech/laravel-helpers 我们能提供哪些服务?
定制开发 / 二次开发

基于 maize-tech/laravel-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-18