generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NumberMacro for currency spelling support
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
1 parent
676cf82
commit 5cb2503
Showing
6 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |