lastdragon-ru/lara-asp-formatter
Composer 安装命令:
composer require lastdragon-ru/lara-asp-formatter
包简介
Provides a customizable wrapper around Intl formatters to use it inside Laravel application. And also allows defining your own formats.
README 文档
README
This package provides a customizable wrapper around Intl formatters to use it inside Laravel application. And also allows defining your own formats.
Requirements
| Requirement | Constraint | Supported by |
|---|---|---|
| PHP | ^8.5 |
HEAD ⋯ 11.0.0 |
^8.4 |
HEAD ⋯ 8.0.0 |
|
^8.3 |
10.3.0 ⋯ 5.0.0 |
|
^8.2 |
7.2.0 ⋯ 2.0.0 |
|
^8.1 |
6.4.2 ⋯ 2.0.0 |
|
^8.0 |
4.6.0 ⋯ 2.0.0 |
|
^8.0.0 |
1.1.2 ⋯ 0.12.0 |
|
>=8.0.0 |
0.11.0 ⋯ 0.4.0 |
|
>=7.4.0 |
0.3.0 ⋯ 0.1.0 |
|
| Laravel | ^13.10.0 |
HEAD , 11.1.0 |
^13.0.0 |
11.0.0 |
|
^12.0.1 |
10.3.0 ⋯ 9.0.0 |
|
^11.0.8 |
8.1.1 ⋯ 8.0.0 |
|
^11.0.0 |
7.2.0 ⋯ 6.2.0 |
|
^10.34.0 |
7.2.0 ⋯ 6.2.0 |
|
^10.0.0 |
6.1.0 ⋯ 2.1.0 |
|
^9.21.0 |
5.6.0 ⋯ 5.0.0-beta.1 |
|
^9.0.0 |
5.0.0-beta.0 ⋯ 0.12.0 |
|
^8.22.1 |
3.0.0 ⋯ 0.2.0 |
|
^8.0 |
0.1.0 |
Installation
composer require lastdragon-ru/lara-asp-formatter
Configuration
Config can be used to customize formats. Before this, you need to publish it via the following command, and then you can edit config/lara-asp-formatter.php.
php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\Formatter\\PackageProvider --tag=config
Usage
Note
The resolved formats are cached, thus run-time changes in the configuration will not be applied. You can clone the formatter instance to reset the internal cache.
The Formatter is very simple to use. Please also check Formatter to see built-in formats 🤗
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Formatter; $default = app()->make(Formatter::class); // For default app locale $locale = $default->forLocale('ru_RU'); // For ru_RU locale Example::dump($default->integer(123.454321)); Example::dump($default->decimal(123.454321)); Example::dump($locale->decimal(123.454321));
The $default->integer(123.454321) is:
"123"
The $default->decimal(123.454321) is:
"123.45"
The $locale->decimal(123.454321) is:
"123,45"
You can also define separate setting for each locale:
<?php declare(strict_types = 1); use Illuminate\Support\Facades\Date; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Config\Config; use LastDragon_ru\LaraASP\Formatter\Config\Format; use LastDragon_ru\LaraASP\Formatter\Formats\IntlDateTime\IntlDateTimeFormat; use LastDragon_ru\LaraASP\Formatter\Formats\IntlDateTime\IntlDateTimeOptions; use LastDragon_ru\LaraASP\Formatter\Formatter; use LastDragon_ru\LaraASP\Formatter\PackageConfig; Example::config(PackageConfig::class, static function (Config $config): void { $config->formats[Formatter::Date] = new Format( IntlDateTimeFormat::class, new IntlDateTimeOptions( dateType: IntlDateFormatter::SHORT, timeType: IntlDateFormatter::NONE, pattern : 'd MMM yyyy', ), [ 'ru_RU' => new IntlDateTimeOptions( pattern: 'dd.MM.yyyy', ), ], ); }); $datetime = Date::make('2023-12-30T20:41:40.000018+04:00'); $default = app()->make(Formatter::class); $locale = $default->forLocale('ru_RU'); Example::dump($default->date($datetime)); Example::dump($locale->date($datetime));
The $default->date($datetime) is:
"30 Dec 2023"
The $locale->date($datetime) is:
"30.12.2023"
Adding new formats
You just need to create a class that implements Format, add into the package config, and add macros to the Formatter class.
Note
The instance will be created through container with the following additional arguments:
$formatter:Formatter- the current formatter instance (can be used to get locale/timezone).$options(array) - formatter options defined inside app config (may containnulls).
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Formatter\Docs\Examples\Uppercase; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Config\Config; use LastDragon_ru\LaraASP\Formatter\Config\Format; use LastDragon_ru\LaraASP\Formatter\Contracts\Format as FormatContract; use LastDragon_ru\LaraASP\Formatter\Formatter; use LastDragon_ru\LaraASP\Formatter\PackageConfig; use Override; use Stringable; use function mb_strtoupper; /** * @implements FormatContract<null, Stringable|string|null> */ class UppercaseFormat implements FormatContract { public function __construct() { // empty } #[Override] public function __invoke(mixed $value): string { return mb_strtoupper((string) $value); } } Formatter::macro('uppercase', function (Stringable|string|null $value): string { return $this->format('uppercase', $value); }); Example::config(PackageConfig::class, static function (Config $config): void { $config->formats['uppercase'] = new Format( UppercaseFormat::class, ); }); // @phpstan-ignore method.notFound Example::dump(app()->make(Formatter::class)->uppercase('string'));
The app()->make(Formatter::class)->uppercase('string') is:
"STRING"
Notes about built-in formats
Currency
By default, the Formatter use locale currency. You can redefine it globally through config, specify for the call, and/or add a macros for another currency.
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Config\Config; use LastDragon_ru\LaraASP\Formatter\Config\Format; use LastDragon_ru\LaraASP\Formatter\Formats\IntlNumber\IntlCurrencyFormat; use LastDragon_ru\LaraASP\Formatter\Formats\IntlNumber\IntlCurrencyOptions; use LastDragon_ru\LaraASP\Formatter\Formatter; use LastDragon_ru\LaraASP\Formatter\PackageConfig; Example::config(PackageConfig::class, static function (Config $config): void { $config->formats[Formatter::Currency] = new Format( IntlCurrencyFormat::class, new IntlCurrencyOptions( currency: 'USD', ), ); }); Formatter::macro('eur', function (float|int|null $value): string { return $this->format(Formatter::Currency, [$value, 'EUR']); }); $formatter = app()->make(Formatter::class); $value = 123.45; // @phpstan-ignore method.notFound Example::dump($formatter->eur($value)); // macro Example::dump($formatter->currency($value)); // locale default Example::dump($formatter->currency($value, 'EUR')); // as defined
The $formatter->eur($value) is:
"€123.45"
The $formatter->currency($value) is:
"$123.45"
The $formatter->currency($value, 'EUR') is:
"€123.45"
Duration
To format duration you can use built-in Intl formatter, but it doesn't support fraction seconds and have a different format between locales (for example, 12345 seconds is 3:25:45 in en_US locale, and 12 345 in ru_RU). These reasons make it difficult to use it in real applications. To make duration() more useful, the alternative syntax was added and used by default.
The syntax is the same as ICU Date/Time format syntax.
| Symbol | Meaning |
|---|---|
y |
years |
M |
months |
d |
days |
H |
hours |
m |
minutes |
s |
seconds |
S |
fractional seconds |
z |
negative sign (default -) |
' |
escape for text |
'' |
two single quotes produce one |
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Formatter; $default = app()->make(Formatter::class); // For default app locale $locale = $default->forLocale('ru_RU'); // For ru_RU locale Example::dump($default->duration(123.454321)); Example::dump($locale->duration(123.4543)); Example::dump($locale->duration(1_234_543));
The $default->duration(123.454321) is:
"00:02:03.454"
The $locale->duration(123.4543) is:
"00:02:03.454"
The $locale->duration(1234543) is:
"342:55:43.000"
To use Intl Formatter, you need to change the duration format in the config:
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Config\Config; use LastDragon_ru\LaraASP\Formatter\Config\Format; use LastDragon_ru\LaraASP\Formatter\Formats\IntlNumber\IntlDurationFormat; use LastDragon_ru\LaraASP\Formatter\Formatter; use LastDragon_ru\LaraASP\Formatter\PackageConfig; Example::config(PackageConfig::class, static function (Config $config): void { $config->formats[Formatter::Duration] = new Format( IntlDurationFormat::class, ); }); $default = app()->make(Formatter::class); // For default app locale $locale = $default->forLocale('ru_RU'); // For ru_RU locale $value = 123.4543; Example::dump($default->duration($value)); Example::dump($locale->duration($value));
The $default->duration($value) is:
"2:03"
The $locale->duration($value) is:
"123"
Upgrading
Please follow Upgrade Guide.
Contributing
Please use the main repository to report issues, send pull requests, or ask questions.
lastdragon-ru/lara-asp-formatter 适用场景与选型建议
lastdragon-ru/lara-asp-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.96k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「formatter」 「intl」 「laravel」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lastdragon-ru/lara-asp-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lastdragon-ru/lara-asp-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lastdragon-ru/lara-asp-formatter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 phone formatter and validator.
Easy to use internationalization functions for Laravel
Custom laravel monolog logger for datadog logs management, both api and agent ways
Easy to use internationalization functions for Laravel
An less opinionated version of Laravel Pint.
Symfony bundle for multilingual entity localization and database-backed form field translations with XLIFF export.
统计信息
- 总下载量: 7.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-19