-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simply static methods for number/currency transformations (#128)
* Make the NumberToWords class fluent by allowing method chaining * fixed * added: newline at end of manages file * refactoring * simpler static methods * fixed readme file
- Loading branch information
1 parent
d4f54f7
commit 7aa709e
Showing
5 changed files
with
145 additions
and
149 deletions.
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,54 @@ | ||
<?php | ||
|
||
namespace NumberToWords\Concerns; | ||
|
||
use NumberToWords\CurrencyTransformer as Transformer; | ||
use NumberToWords\Exception\InvalidArgumentException; | ||
use NumberToWords\CurrencyTransformer\CurrencyTransformer; | ||
|
||
trait ManagesCurrencyTransformers | ||
{ | ||
private array $currencyTransformers = [ | ||
'ar' => Transformer\ArabicCurrencyTransformer::class, | ||
'al' => Transformer\AlbanianCurrencyTransformer::class, | ||
'az' => Transformer\AzerbaijaniCurrencyTransformer::class, | ||
'de' => Transformer\GermanCurrencyTransformer::class, | ||
'dk' => Transformer\DanishCurrencyTransformer::class, | ||
'en' => Transformer\EnglishCurrencyTransformer::class, | ||
'es' => Transformer\SpanishCurrencyTransformer::class, | ||
'fr' => Transformer\FrenchCurrencyTransformer::class, | ||
'hu' => Transformer\HungarianCurrencyTransformer::class, | ||
'ka' => Transformer\GeorgianCurrencyTransformer::class, | ||
'lt' => Transformer\LithuanianCurrencyTransformer::class, | ||
'lv' => Transformer\LatvianCurrencyTransformer::class, | ||
'pl' => Transformer\PolishCurrencyTransformer::class, | ||
'pt_BR' => Transformer\PortugueseBrazilianCurrencyTransformer::class, | ||
'ro' => Transformer\RomanianCurrencyTransformer::class, | ||
'ru' => Transformer\RussianCurrencyTransformer::class, | ||
'sk' => Transformer\SlovakCurrencyTransformer::class, | ||
'tk' => Transformer\TurkmenCurrencyTransformer::class, | ||
'tr' => Transformer\TurkishCurrencyTransformer::class, | ||
'ua' => Transformer\UkrainianCurrencyTransformer::class, | ||
'yo' => Transformer\YorubaCurrencyTransformer::class | ||
]; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function getCurrencyTransformer(string $language): CurrencyTransformer | ||
{ | ||
if (!array_key_exists($language, $this->currencyTransformers)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Currency transformer for "%s" language is not implemented.', | ||
$language | ||
)); | ||
} | ||
|
||
return new $this->currencyTransformers[$language](); | ||
} | ||
|
||
public static function transformCurrency(string $language, int $number, string $currency): string | ||
{ | ||
return (new static())->getCurrencyTransformer($language)->toWords($number, $currency); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace NumberToWords\Concerns; | ||
|
||
use NumberToWords\NumberTransformer as Transformer; | ||
use NumberToWords\Exception\InvalidArgumentException; | ||
use NumberToWords\NumberTransformer\NumberTransformer; | ||
|
||
trait ManagesNumberTransformers | ||
{ | ||
private array $numberTransformers = [ | ||
'ar' => Transformer\ArabicNumberTransformer::class, | ||
'al' => Transformer\AlbanianNumberTransformer::class, | ||
'az' => Transformer\AzerbaijaniNumberTransformer::class, | ||
'bg' => Transformer\BulgarianNumberTransformer::class, | ||
'cs' => Transformer\CzechNumberTransformer::class, | ||
'de' => Transformer\GermanNumberTransformer::class, | ||
'dk' => Transformer\DanishNumberTransformer::class, | ||
'en' => Transformer\EnglishNumberTransformer::class, | ||
'es' => Transformer\SpanishNumberTransformer::class, | ||
'et' => Transformer\EstonianNumberTransformer::class, | ||
'fa' => Transformer\PersianNumberTransformer::class, | ||
'fr' => Transformer\FrenchNumberTransformer::class, | ||
'fr_BE' => Transformer\FrenchBelgianNumberTransformer::class, | ||
'hu' => Transformer\HungarianNumberTransformer::class, | ||
'id' => Transformer\IndonesianNumberTransformer::class, | ||
'it' => Transformer\ItalianNumberTransformer::class, | ||
'ka' => Transformer\GeorgianNumberTransformer::class, | ||
'lt' => Transformer\LithuanianNumberTransformer::class, | ||
'lv' => Transformer\LatvianNumberTransformer::class, | ||
'ms' => Transformer\MalayNumberTransformer::class, | ||
'nl' => Transformer\DutchNumberTransformer::class, | ||
'pl' => Transformer\PolishNumberTransformer::class, | ||
'pt_BR' => Transformer\PortugueseBrazilianNumberTransformer::class, | ||
'ro' => Transformer\RomanianNumberTransformer::class, | ||
'ru' => Transformer\RussianNumberTransformer::class, | ||
'sk' => Transformer\SlovakNumberTransformer::class, | ||
'sv' => Transformer\SwedishNumberTransformer::class, | ||
'tk' => Transformer\TurkmenNumberTransformer::class, | ||
'tr' => Transformer\TurkishNumberTransformer::class, | ||
'ua' => Transformer\UkrainianNumberTransformer::class, | ||
'yo' => Transformer\YorubaNumberTransformer::class, | ||
]; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function getNumberTransformer(string $language): NumberTransformer | ||
{ | ||
if (!array_key_exists($language, $this->numberTransformers)) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Number transformer for "%s" language is not implemented.', | ||
$language | ||
)); | ||
} | ||
|
||
return new $this->numberTransformers[$language](); | ||
} | ||
|
||
public static function transformNumber(string $language, int $number): string | ||
{ | ||
return (new static())->getNumberTransformer($language)->toWords($number); | ||
} | ||
} |
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