nedwors/pluralize 问题修复 & 功能扩展

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

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

nedwors/pluralize

Composer 安装命令:

composer require nedwors/pluralize

包简介

Easily and fluently pluralize content within your Laravel apps

README 文档

README

Latest Version on Packagist Tests

A Laravel package that provides null-safe, meaningful pluralization of strings.

Go from this...

@if($pizzas)

{{ $pizzas->count() }} {{ Str::plural('Pizza', $pizzas->count()) }}

@else

-

@endif

// 2 Pizzas
// -

To this...

{{ pluralize('Pizza', $pizzas) }}

// 2 Pizzas
// -

Nice eh?

No more count($me), $me->count() or $me->total()... Just pass in your variable and have it counted for you.

No need to repeat {{ $lemons->count() }} {{ Str::plural('Lemon', $lemons->count()) }} over and over everywhere in your views. Just a unified format across your app.

No need to think anymore about whether the variable is null and what to do if so, just a clean -, or whatever you want.

The stuff of nightmares...

Call to a member function count() on null

Docs

Installation

You can install the package via composer

composer require nedwors/pluralize

Setup

Minimal, if any, setup is required. The package is ready to work out of the box with sensible defaults.

The defaults are shown below. The 1st comment represents the returned string when $pizzas has a count of 10; the 2nd represents when $pizzas is null:

pluralize('Pizza', $pizzas)

// 10 Pizzas
// -

However you are free to configure these defaults, and fine tune the system further.

Introduction

Helper vs Class

This documentation's examples are based on the helper function pluralize(). However, the underlying class can be used just the same for every feature. For instance, the following are equivalent:

pluralize('Dog', $dogs, '...')

pluralize('Dog')->from($dogs)->or('...')

Pluralize::this('Dog')->from($dogs)->or('...')

Usage

The underlying class in the package implements the Stringable interface, so in your Blade views it works just by calling the helper function or class.

pluralize('Dog', $dogs)

Outside your views, do any of the following

pluralize('Dog', $dogs)() // Invoke

pluralize('Dog', $dogs)->go() // Call the go() method

(string) pluralize('Dog', $dogs) // Cast the type

Features/API

Pluralize

Pluralize has 4 main functions:

Pluralize::this(...)->from(...)->as(...)->or(...)

The pluralize() helper function has 4 parameters that map to these functions:

pluralize('this', 'from', 'or', 'as')

You'll notice that or is listed before as in the parameter list. This helps with usability for most use cases.

It can be accessed fluently too

pluralize(...)->from(...)->as(...)->or(...)

This

The singular form of the string you wish to be pluralized.

// The first argument to the helper function

pluralize('Rocket')

From

The count/total/sum you wish to pluralize the string from.

The variable you pass can be an int, an array, a Collection, a LengthAwarePaginator or a Paginator.

// The second argument to the helper function

pluralize('Rocket', $rockets)

// Or, as a method

pluralize('Rocket')->from($rockets)

By this point, it's worth noting that you are good to go. Nothing more is required for most uses.

Pluralize does provide extra features for more flexibility. These are detailed below, as well as configuration.

Or

The string to display if the count is null.

This is not required. If not provided, it will simply defer to the default -.

// The third argument to the helper function

pluralize('Rocket', $rockets, '...')

// Or, as a method

pluralize('Rocket', $rockets)->or('...')

In addition to providing a string, you can pass a Closure that will receive the pluralized form of the word.

pluralize('Rocket', $rockets)
    ->or(fn($plural) => "Oops, $plural is not defined")

// Oops, Rockets is not defined
Why would I ever write a Closure when I can just type Oops, Rockets is not defined?

It's true, it's probably unlikely. But at least the power is there if needed. For instance, perhaps you want to grab the Auth::user(), or give context with the current time.

However, the power of using a Closure really comes when configuring the package.

As

The format to display the pluralization.

This is not required. If not provided, it will simply defer to the default n items.

The most useful means is declaring this as a Closure, which is passed the plural form of the string and the count.

// The fourth argument to the helper function

pluralize('Rocket', $rockets, '...', fn($plural, $count) => "$plural: $count")

// Or, probably more usefully, as a method

pluralize('Rocket', $rockets)->as(fn($plural, $count) => "$plural: $count")

// Rockets: 10

How about if you wanted something along the lines of there are 10 Rockets? When there's 1 Rocket, you'll end up with There are 1 Rocket... Hmm. Well, the pipe operator is your friend! Simply declare the singular output to the left, the plural to the right.

pluralize('Rocket', $rockets)
    ->as(fn($plural, $count) => "There is|are $count $plural")

// There is 1 Rocket
// There are 2 Rockets

Now, you can pass a string if you really, really want to...

pluralize('Rocket', $rockets)
    ->as('Not sure how many, but you have some Rockets')

// Now sure how many, but you have some Rockets

...but this is most useful when used with your configuration.

Configuration

You can easily configure different aspects of the package. This is all done via the Pluralize class in your service provider.

Default Bindings

You can declare the default formats for use in your app by calling Pluralize::bind() with no arguments

// In your service provider's boot() method
Pluralize::bind()
    ->output(fn($plural, $count) => "$plural: $count")
    ->fallback('...')

// In your view
pluralize('Car', $cars)

// When $cars = null, ...
// When $cars = 10, Cars: 10

The default Output is n items. The default Fallback is -.

Specific Bindings

You can bind to the word you want to pluralize for specific formatting

// In your service provider's boot() method
Pluralize::bind('Hotdog')
    ->output(fn($plural, $count) => "Yum, $count hotdogs!")

// In your view
pluralize('Car', $cars) // 10 Cars
pluralize('Hotdog', $hotdogs) // Yum, 10 hotdogs!

You can set an arbitrary key to be referred to at time of render

// In your service provider's boot() method
Pluralize::bind('ye-olde')
    ->fallback(fn($plural) => "Thou hath not declared $plural")

// In your view
pluralize('Robot', null) // -
pluralize('DeLorean', null)->or('ye-olde') // Thou hath not declared DeLoreans

Driver

The package uses the Laravel Str::plural() method for pluralizing the strings passed in. You are free to use your own driver if desired. This would be especially useful for non-english languages.

Write your custom driver implementing the Pluralization interface. This defines one method, run(), which is passed the singular string and the current count. Here's the current implementation:

public function run(string $string, $count): string
{
    return Str::plural($string, $count);
}

Then set this as the desired driver in the boot() method of your service provider

Pluralize::driver(NewDriver::class)

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email nedwors@gmail.com instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

nedwors/pluralize 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-28