PHP code example of lastdragon-ru / lara-asp-formatter
1. Go to this page and download the library: Download lastdragon-ru/lara-asp-formatter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lastdragon-ru / lara-asp-formatter example snippets
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));
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));
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'));
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
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));
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));