81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Support\Facades\Blade;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Money\Currencies\ISOCurrencies;
|
|
use Money\Currency;
|
|
use Money\Formatter\DecimalMoneyFormatter;
|
|
use Money\Formatter\IntlMoneyFormatter;
|
|
|
|
class MoneyServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
#[\Override]
|
|
public function register()
|
|
{
|
|
$this->app->singleton(
|
|
DecimalMoneyFormatter::class,
|
|
fn () => new DecimalMoneyFormatter(new ISOCurrencies())
|
|
);
|
|
|
|
$this->app->singleton(
|
|
Currency::class,
|
|
fn () => new Currency(
|
|
// @phpstan-ignore argument.type (Should throw if not provided)
|
|
Config::string(
|
|
'app.currency',
|
|
'EUR'
|
|
)
|
|
)
|
|
);
|
|
|
|
$this->app->singleton(
|
|
\NumberFormatter::class,
|
|
fn () => new \NumberFormatter(
|
|
Config::string('app.locale', 'fr_FR'),
|
|
\NumberFormatter::CURRENCY
|
|
)
|
|
);
|
|
|
|
$this->app->singleton(IntlMoneyFormatter::class, function () {
|
|
$numberFormatter = $this->app->make(\NumberFormatter::class);
|
|
|
|
$currency = $this->app->make(Currency::class);
|
|
$numberFormatter->setTextAttribute(
|
|
\NumberFormatter::CURRENCY_CODE,
|
|
$currency->getCode()
|
|
);
|
|
|
|
return new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->sharetoView();
|
|
}
|
|
|
|
private function sharetoView(): void
|
|
{
|
|
Blade::directive('money', function ($expression): string {
|
|
return "<?php echo app(\Money\Formatter\IntlMoneyFormatter::class)->format($expression); ?>";
|
|
});
|
|
Blade::directive('currency', function ($_): string {
|
|
return "<?php echo app(\Money\Currency::class)->getCode(); ?>";
|
|
});
|
|
}
|
|
}
|