lastdragon-ru/lara-asp-formatter

The Awesome Set of Packages for Laravel - The Formatter.

Downloads

6264

Stars

0

Version

6.2.0

(Laravel) Intl Formatter

This package provides a customizable wrapper around Intl formatters to use it inside Laravel application.

Requirements

Requirement Constraint Supported by
PHP ^8.3 HEAD ⋯ 5.0.0
^8.2 HEAD ⋯ 2.0.0
^8.1 HEAD ⋯ 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 ^11.0.0 HEAD , 6.2.0
^10.34.0 HEAD , 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

Usage

Formatter is very simple to use:

<?php declare(strict_types = 1);

use Illuminate\Container\Container;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\Formatter\Formatter;

$default = Container::getInstance()->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"

Please check source code to see available methods and config example to available settings 🤗

Formats

Some methods like as date()/datetime()/etc have $format argument. The argument specifies not the format but the format name. So you can use the names and do not worry about real formats. It is very important when application big/grow. To specify available names and formats the package config should be published and customized.

php artisan vendor:publish --provider=LastDragon_ru\\LaraASP\\Formatter\\Provider --tag=config
<?php declare(strict_types = 1);

use Illuminate\Container\Container;
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  = Container::getInstance()->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'));

The $default->date($datetime) is:

"30 Dec 2023"

The $default->date($datetime, 'custom') is:

"2023/12/30"

The $locale->date($datetime) is:

"30 дек. 2023"

The $locale->date($datetime, 'custom') is:

"30.12.2023"

Duration

To format duration you can use built-in Intl formatter, but it doesn't support fraction seconds and have 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 difficult to use it in real applications. To make duration() more useful, the alternative syntax was added.

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 Illuminate\Container\Container;
use LastDragon_ru\LaraASP\Dev\App\Example;
use LastDragon_ru\LaraASP\Formatter\Formatter;

$default = Container::getInstance()->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"

Upgrading

Please follow Upgrade Guide.

Contributing

This package is the part of Awesome Set of Packages for Laravel. Please use the main repository to report issues, send pull requests, or ask questions.

LastDragon-ru

Author

LastDragon-ru