marshmallow/helpers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

marshmallow/helpers

Composer 安装命令:

composer require marshmallow/helpers

包简介

A package containing all the helper function we can use throughout all our projects

README 文档

README

alt text

Marshmallow Helpers

A package containing all the helper functions we can use throughout all our projects. We use this package in all our projects, for ourselves and for our customers. If you miss anything, create an issue and we will add it as soon as possible.

Version Issues Code Coverage Licence

Installation

You can install the package via composer:

composer require marshmallow/helpers

Table of contents

  1. CSV Helper
  2. Helper functions
  3. String helper
  4. Migrations
  5. URL
  6. Array
  7. Reviews
  8. Grouper

CSV Helper

This helper will make generating .csv files extremly ease. Please check the examples below. You can use the store method to store it in a location of your choosing. You can use the download method if you just want to download the file. If you wish to store and download the generated .csv file you can use the storeAndDownload method.

Usage example

$headers = ['Column 1', 'Column 2', 'Column 3'];
$data = [
	[1, 2, 3],
	[4, 5, 6],
];

return CSV::headers($headers)
	->data($data)
	->delimiter(';')
	->setFilename('generated-csv-1234')
	->storeAndDownload();

Use a collection as data attribute

You can use a collection as your data attribute. By default it will map all the data in the collection sequence. If you wish to edit the collection data, you can add a callback method to the data method. Please note that this will give you every row as an array value.

use Marshmallow\HelperFunctions\Facades\CSV;

CSV::headers($headers)
        ->data($data, function (array $row) {
            return [
                $row['name'],
                $row['slug'],
                $row['created_at'],
            ];
        })
        ->storeAndDownload();

Store and download calls

The following methods are available for storing and downloading the generated csv file.

$csv->download();
$csv->store();
$csv->storeAndDownload();
$csv->stream();

Helper functions

Percentage helper

This is a public helper function that is available within your Laravel application. This function can be used in all your PHP and Blade files.

percentage(47, App\Post::get()); // 63.829787234043

String Helper

The Str helper extends the helper from Laravel. So you have all the methods available in the Laravel helper available as well. Check the Laravel documentation for all the available methods.

Str::join()

This method is super handy for concatenating strings separated by a comma, but use another value for the last item. Checkout the example below.

Str::join([
  'Marshmallow',
  'Stef van Esch',
  'Mr Mallow'
]);

// Marshmallow, Stef van Esch and Mr Mallow

Str::random()

We have added on the default Str::random() of Laravel. We've added a second parameter which is an array of characters that should be ignored. We also have build in a couple of presets like lowercase which will make sure the random string won't contain any lowercase characters.

Str::random($limit = 16, $ignore = [
    /**
     * Custom items
     */
    'A','B', 'C', 'D',

    /**
     * Presets
     */
    'lowercase', 	// Will ignore all lowercase characters.
    'uppercase',	// Will ignore all uppercase characters.
    'letters',		// Will ignore all letters.
    'numbers',		// Will ignore all numbers.
    'similar',		// Will ignore all numbers and letters that have been marked as similar.
]);

Str::cleanPhoneNumber()

Str::cleanPhoneNumber()

Str::phonenumberWithCountryCode()

Return a cleaned phonenumber with the country code. You can choose to return the phonenumber with a + or 00 at the start of the phonenumber.

Str::phonenumberWithCountryCode('0628998954')
// response: +31628998954

Str::phonenumberWithCountryCode(
    '0031628998954',
    $country_code = '31',
    $use_plus_instead_of_zeros = false
);
// response: 0031628998954

Str::numbersOnly()

Str::numbersOnly()

Str::numbersAndLettersOnly()

Str::numbersAndLettersOnly()

Str::readmore()

Str::readmore(
    $string,
    $lenght_first_part,
    $return_this_part = null
);

Str::paragraphsAsArray()

Str::paragraphsAsArray($string);

Str::getFirstParagraph()

Str::getFirstParagraph(
    $string,
    $number_of_paragraphs = 1,
    $return_array = false
);

Str::getAllButFirstParagraph()

Str::getAllButFirstParagraph(
    $string,
    $number_of_paragraphs_to_skip = 1,
    $return_array = false
);

Migrations

We have a trait available that gives you some extra options when creating migrations. Add the MigrationHelper trait to you migration to make use of these options.

Implementation

use Marshmallow\HelperFunctions\Traits\MigrationHelper;

class CreateProductTable extends Migration
{
    use MigrationHelper;

Create a column only if it doesn't exist.

This method was added because when a database that already has a products table and later will use our Product package, the migrations will through error's.

$this->createColumnIfDoesntExist(
    'products', 'deleted_at', function (Blueprint $table) {
        $table->softDeletes();
    }
);

URL

URL::isInternal()

URL::isInternal($url)

URL::isCurrent()

URL::isCurrent($url)

URL::buildFromArray()

URL::buildFromArray($array)

URL::isNova()

URL::isNova($request)

URL::isNotNova()

URL::isNotNova($request)

Array

Arrayable::storeInFile();

This method will store a pretty array in a file. With this method is possible to generate config files.

Arrayable::storeInFile(array $array, string $file_location);

Builder

Builder::published()

BuilderHelper::published will filter on database columns if something is published.

public function scopePublished (Builder $builder)
{
    BuilderHelper::published(
        $builder,
        $valid_from_column,
        $valid_till_column
    );
}

Reviews

For the review stars you can call ReviewHelper::ratingToStars(4.5). By default the ReviewHelper will think you are using a max rating of 5, support half star rating and return a string of FontAwesome icons. You can overule this behaviour by;

Customise

Create the config file config/review.php and specify your needs:

return [
    'max_rating' => 10,
    'full_star' => '+ ',
    'half_star' => '* ',
    'empty_star' => '- ',
];

Or you can provide the same config array as a second parameter to the ratingToStars method like so;

ReviewHelper::ratingToStars(4.5, [
    'max_rating' => 10,
    'full_star' => '+ ',
    'half_star' => '* ',
    'empty_star' => '- ',
])

Grouper

Grouper is a super handy helper when you need to split your queries results in to groups. We use this with blogs a lot. The first row might have 3 results, the second row will have 1 result, en the third row will have 3 items again. Please see the example below.

use Marshmallow\HelperFunctions\Facades\Collection;

$grouper = Collection::createGrouper(Blog::get(), $structure_array = [
    'first' => 3,
    'second' => 1,
    'third' => 2,
]);

As you can see in the example, the structure_array has a name as the key. This is done so when you are looping through your groups, you are able the test which group you are currently looping.

foreach ($grouper as $group) {
    if ($group->is('first')) {
        // Add your template for 3 items
    }

    if ($group->is('second')) {
        // Add your template for 1 item
    }
}

Looping through the group items works just like you expect when working with collections.

foreach ($grouper as $group) {
    foreach ($group as $blog) {
        //
    }
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Security

If you discover any security related issues, please email stef@marshmallow.dev instead of using the issue tracker.

Credits

License

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

marshmallow/helpers 适用场景与选型建议

marshmallow/helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.98k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 25.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-03