lastdragon-ru / lara-asp-formatter
为Laravel提供的出色包集合 - 格式化器。
Requires
- php: ^8.1|^8.2|^8.3
- ext-bcmath: *
- ext-intl: *
- ext-mbstring: *
- illuminate/container: ^10.34.0|^11.0.0
- illuminate/contracts: ^10.34.0|^11.0.0
- illuminate/macroable: ^10.34.0|^11.0.0
- illuminate/support: ^10.34.0|^11.0.0
- lastdragon-ru/lara-asp-core: 6.4.2
- symfony/polyfill-php83: ^1.28
Requires (Dev)
- lastdragon-ru/lara-asp-testing: 6.4.2
- orchestra/testbench: ^8.0.0|^9.0.0
- phpunit/phpunit: ^10.1.0|^11.0.0
- dev-main
- 6.x-dev
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-beta.1
- 5.0.0-beta.0
- 4.x-dev
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.0.0
- 2.x-dev
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-gitattributes
This package is auto-updated.
Last update: 2024-09-26 07:31:09 UTC
README
此包提供了一组可定制的Intl格式化器包装,以便在Laravel应用程序中使用。
需求
安装
composer require lastdragon-ru/lara-asp-formatter
使用
格式化器非常简单易用
<?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));
$default->integer(123.454321)
是
"123"
$default->decimal(123.454321)
是
"123.45"
$locale->decimal(123.454321)
是
"123,45"
格式
一些方法如date()
/datetime()
/等有$format
参数。该参数指定的是格式名称,而不是格式本身。因此,您可以使用名称,而无需担心真实的格式。这对于大型/增长的应用程序非常重要。要指定可用的名称和格式,应发布和自定义包的配置。
php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\Formatter\\Provider --tag=config
<?php declare(strict_types = 1); use Illuminate\Support\Facades\Date; use LastDragon_ru\LaraASP\Dev\App\Example; use LastDragon_ru\LaraASP\Formatter\Formatter; use LastDragon_ru\LaraASP\Formatter\Package; Example::config(Package::Name, [ 'options' => [ Formatter::Date => 'default', ], 'all' => [ Formatter::Date => [ 'default' => 'd MMM yyyy', 'custom' => 'yyyy/MM/dd', ], ], 'locales' => [ 'ru_RU' => [ Formatter::Date => [ 'custom' => '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($default->date($datetime, 'custom')); Example::dump($locale->date($datetime)); Example::dump($locale->date($datetime, 'custom'));
$default->date($datetime)
是
"30 Dec 2023"
$default->date($datetime, 'custom')
是
"2023/12/30"
$locale->date($datetime)
是
"30 дек. 2023"
$locale->date($datetime, 'custom')
是
"30.12.2023"
持续时间
要格式化持续时间,可以使用内置的Intl格式化器,但它不支持分数秒,并且在不同的区域设置中格式不同(例如,在en_US
区域设置中,12345秒是3:25:45
,而在ru_RU
区域设置中是12 345
)。这些原因使得在实际应用中使用它变得困难。为了使duration()
更有用,添加了替代语法。
该语法与ICU日期/时间格式语法相同。
<?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));
$default->duration(123.454321)
是
"00:02:03.454"
$locale->duration(123.4543)
是
"00:02:03.454"
$locale->duration(1234543)
是
"342:55:43.000"
升级
请遵循升级指南。