Skip to content

Commit

Permalink
Add NumberMacro for currency spelling support
Browse files Browse the repository at this point in the history
Introduces `NumberMacro` to spell out currency values in words, supporting multiple locales and currencies. Includes tests and language files for English and Brazilian Portuguese, and adds `ext-intl` to runtime dependencies.
  • Loading branch information
allanmcarvalho committed Dec 12, 2024
1 parent 676cf82 commit 5cb2503
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"require": {
"php": "^8.3",
"ext-dom": "*",
"ext-intl": "*",
"inertiajs/inertia-laravel": "^v1.3.0|^2.x-dev",
"jaybizzle/crawler-detect": "^v1.2.120",
"spatie/laravel-package-tools": "^1.16.5",
"illuminate/contracts": "^11.24"
},
"require-dev": {
"ext-intl": "*",
"laravel/pint": "^v1.18.1",
"nunomaduro/collision": "^v8.4.0",
"orchestra/testbench": "^9.5.0",
Expand Down
43 changes: 43 additions & 0 deletions resources/lang/en/number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| LaravelToolkit Language Lines
|--------------------------------------------------------------------------
|
*/

'and' => 'and',
'cent' => 'cent',
'cents' => 'cents',

'currencies' => [
'AUD' => [
'singular' => 'australian dollar',
'plural' => 'australian dollars',
],
'BRL' => [
'singular' => 'real',
'plural' => 'reais',
],
'CAD' => [
'singular' => 'canadian dollar',
'plural' => 'canadian dollars',
],
'EUR' => [
'singular' => 'euro',
'plural' => 'euros',
],
'JPY' => [
'singular' => 'yen',
'plural' => 'yens',
],
'USD' => [
'singular' => 'dollar',
'plural' => 'dollars',
],
],

];
43 changes: 43 additions & 0 deletions resources/lang/pt_BR/number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| LaravelToolkit Language Lines
|--------------------------------------------------------------------------
|
*/

'and' => 'e',
'cent' => 'centavo',
'cents' => 'centavos',

'currencies' => [
'AUD' => [
'singular' => 'dólar australiano',
'plural' => 'dólares australiano',
],
'BRL' => [
'singular' => 'real',
'plural' => 'reais',
],
'CAD' => [
'singular' => 'dólar canadense',
'plural' => 'dólares canadense',
],
'EUR' => [
'singular' => 'euro',
'plural' => 'euros',
],
'JPY' => [
'singular' => 'yen',
'plural' => 'yens',
],
'USD' => [
'singular' => 'dólar',
'plural' => 'dólares',
],
],

];
2 changes: 2 additions & 0 deletions src/LaravelToolkitServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use LaravelToolkit\Macros\BlueprintMacro;
use LaravelToolkit\Macros\BuilderMacro;
use LaravelToolkit\Macros\CollectionMacro;
use LaravelToolkit\Macros\NumberMacro;
use LaravelToolkit\Macros\RequestMacro;
use LaravelToolkit\Macros\RouterMacro;
use LaravelToolkit\Macros\StrMacro;
Expand Down Expand Up @@ -91,6 +92,7 @@ protected function bootMacros(): void
(new BlueprintMacro)();
(new BuilderMacro)();
(new CollectionMacro)();
(new NumberMacro)();
(new RequestMacro)();
(new RouterMacro)();
(new StrMacro)();
Expand Down
46 changes: 46 additions & 0 deletions src/Macros/NumberMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace LaravelToolkit\Macros;

use Illuminate\Support\Number;

class NumberMacro
{
public function __invoke(): void
{
$this->spellCurrency();
}

public function spellCurrency(): void
{
Number::macro('spellCurrency', function (
int|float $number,
?string $in = null,
?string $locale = null
): string {
$int = intval(floor($number));
$decimals = intval(floor((($number - $int) * 100)));

$locale = $locale ?? app()->getLocale();

$in = $in ?? Number::defaultCurrency();

$singularKey = "laraveltoolkit::number.currencies.{$in}.singular";
$pluralKey = "laraveltoolkit::number.currencies.{$in}.plural";

$singularName = str(__($singularKey))->replace($singularKey, $in)->toString();
$pluralName = str(__($pluralKey))->replace($pluralKey, $in)->toString();

$spelled = [];
$spelled[] = Number::spell($int, $locale);
$spelled[] = ($int !== 1 ? $pluralName : $singularName);
if ($decimals !== 0) {
$spelled[] = __('laraveltoolkit::number.and');
$spelled[] = Number::spell($decimals, $locale);
$spelled[] = ($decimals !== 1 ? __('laraveltoolkit::number.cents') : __('laraveltoolkit::number.cent'));
}

return implode(' ', $spelled);
});
}
}
14 changes: 14 additions & 0 deletions tests/Macros/NumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

it('can spell', function () {
expect(Number::spellCurrency(1))
->toEqual('um dólar')
->and(Number::spellCurrency(2))
->toEqual('dois dólares')
->and(Number::spellCurrency(2))
->toEqual('dois dólares')
->and(Number::spellCurrency(0.43))
->toEqual('zero dólares e quarenta e três centavos')
->and(Number::spellCurrency(1_432_321.52))
->toEqual('um milhão quatrocentos e trinta e dois mil trezentos e vinte e um dólares e cinquenta e dois centavos');
});

0 comments on commit 5cb2503

Please sign in to comment.