Skip to content

Commit

Permalink
Add simply static methods for number/currency transformations (#128)
Browse files Browse the repository at this point in the history
* 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
cristiangomeze authored May 2, 2022
1 parent d4f54f7 commit 7aa709e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 149 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Add package to your composer.json by running:
$ composer require kwn/number-to-words
```


## Usage

This library currently has two types of number-to-words transformations: number and currency. In order to use a specific transformer for certain language you need to create an instance of `NumberToWords` class and then call a method which creates a new instance of a transformer;
Expand All @@ -40,6 +39,12 @@ Then it can be used passing in numeric values to the `toWords()` method:
$numberTransformer->toWords(5120); // outputs "five thousand one hundred twenty"
```

You can also use the simpler version:

```php
NumberToWords::transformNumber('en', 5120); // outputs "five thousand one hundred twenty"
```

### Currency Transformer

Creating a currency transformer works just like a number transformer.
Expand All @@ -60,12 +65,18 @@ Then it can be used passing in numeric values for amount and ISO 4217 currency i
$currencyTransformer->toWords(5099, 'USD'); // outputs "fifty dollars ninety nine cents"
```

You can also use the simpler version:

```php
NumberToWords::transformCurrency('en', 5099, 'USD'); // outputs "fifty dollars ninety nine cents"
```

Please bear in mind, the currency transformer accepts integers as the amount to transform. It means that if you store amounts as floats (e.g. 4.99) you need to multiply them by 100 and pass the integer (499) as an argument.

## Available locale

| Language | Identifier | Number | Currency |
|----------------------|------------|--------|----------|
| -------------------- | ---------- | ------ | -------- |
| Albanian | al | + | + |
| Arabic | ar | + | + |
| Azerbaijani | az | + | + |
Expand Down Expand Up @@ -116,4 +127,4 @@ A: Please report an issue on GitHub. Also feel free to fix it and open a pull re

**Q: My language is missing. Could you add it, please?**

A: Unfortunately, there's a high chance I don't know your language. Feel free to implement the missing transformer and open a pull request. You can take a look at the existing transformers, and follow the same pattern as other languages do.
A: Unfortunately, there's a high chance I don't know your language. Feel free to implement the missing transformer and open a pull request. You can take a look at the existing transformers, and follow the same pattern as other languages do.
54 changes: 54 additions & 0 deletions src/Concerns/ManagesCurrencyTransformers.php
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);
}
}
64 changes: 64 additions & 0 deletions src/Concerns/ManagesNumberTransformers.php
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);
}
}
150 changes: 4 additions & 146 deletions src/NumberToWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,153 +2,11 @@

namespace NumberToWords;

use NumberToWords\CurrencyTransformer\AzerbaijaniCurrencyTransformer;
use NumberToWords\CurrencyTransformer\CurrencyTransformer;
use NumberToWords\CurrencyTransformer\GeorgianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\GermanCurrencyTransformer;
use NumberToWords\CurrencyTransformer\HungarianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\DanishCurrencyTransformer;
use NumberToWords\CurrencyTransformer\EnglishCurrencyTransformer;
use NumberToWords\CurrencyTransformer\LatvianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\LithuanianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\PolishCurrencyTransformer;
use NumberToWords\CurrencyTransformer\PortugueseBrazilianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\RomanianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\RussianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\SlovakCurrencyTransformer;
use NumberToWords\CurrencyTransformer\SpanishCurrencyTransformer;
use NumberToWords\CurrencyTransformer\TurkmenCurrencyTransformer;
use NumberToWords\CurrencyTransformer\TurkishCurrencyTransformer;
use NumberToWords\CurrencyTransformer\UkrainianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\FrenchCurrencyTransformer;
use NumberToWords\CurrencyTransformer\YorubaCurrencyTransformer;
use NumberToWords\CurrencyTransformer\AlbanianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\IndonesianCurrencyTransformer;
use NumberToWords\CurrencyTransformer\MalaysianCurrencyTransformer;
use NumberToWords\Exception\InvalidArgumentException;
use NumberToWords\NumberTransformer\AzerbaijaniNumberTransformer;
use NumberToWords\NumberTransformer\BulgarianNumberTransformer;
use NumberToWords\NumberTransformer\CzechNumberTransformer;
use NumberToWords\NumberTransformer\DanishNumberTransformer;
use NumberToWords\NumberTransformer\DutchNumberTransformer;
use NumberToWords\NumberTransformer\EnglishNumberTransformer;
use NumberToWords\NumberTransformer\EstonianNumberTransformer;
use NumberToWords\NumberTransformer\FrenchBelgianNumberTransformer;
use NumberToWords\NumberTransformer\FrenchNumberTransformer;
use NumberToWords\NumberTransformer\GeorgianNumberTransformer;
use NumberToWords\NumberTransformer\GermanNumberTransformer;
use NumberToWords\NumberTransformer\HungarianNumberTransformer;
use NumberToWords\NumberTransformer\IndonesianNumberTransformer;
use NumberToWords\NumberTransformer\ItalianNumberTransformer;
use NumberToWords\NumberTransformer\LatvianNumberTransformer;
use NumberToWords\NumberTransformer\LithuanianNumberTransformer;
use NumberToWords\NumberTransformer\MalayNumberTransformer;
use NumberToWords\NumberTransformer\PersianNumberTransformer;
use NumberToWords\NumberTransformer\PolishNumberTransformer;
use NumberToWords\NumberTransformer\NumberTransformer;
use NumberToWords\NumberTransformer\PortugueseBrazilianNumberTransformer;
use NumberToWords\NumberTransformer\RomanianNumberTransformer;
use NumberToWords\NumberTransformer\RussianNumberTransformer;
use NumberToWords\NumberTransformer\SlovakNumberTransformer;
use NumberToWords\NumberTransformer\SpanishNumberTransformer;
use NumberToWords\NumberTransformer\SwedishNumberTransformer;
use NumberToWords\NumberTransformer\TurkishNumberTransformer;
use NumberToWords\NumberTransformer\TurkmenNumberTransformer;
use NumberToWords\NumberTransformer\UkrainianNumberTransformer;
use NumberToWords\NumberTransformer\YorubaNumberTransformer;
use NumberToWords\NumberTransformer\AlbanianNumberTransformer;
use NumberToWords\NumberTransformer\ArabicNumberTransformer;
use NumberToWords\CurrencyTransformer\ArabicCurrencyTransformer;
use NumberToWords\Concerns\ManagesCurrencyTransformers;
use NumberToWords\Concerns\ManagesNumberTransformers;

class NumberToWords
{
private array $numberTransformers = [
'ar' => ArabicNumberTransformer::class,
'al' => AlbanianNumberTransformer::class,
'az' => AzerbaijaniNumberTransformer::class,
'bg' => BulgarianNumberTransformer::class,
'cs' => CzechNumberTransformer::class,
'de' => GermanNumberTransformer::class,
'dk' => DanishNumberTransformer::class,
'en' => EnglishNumberTransformer::class,
'es' => SpanishNumberTransformer::class,
'et' => EstonianNumberTransformer::class,
'fa' => PersianNumberTransformer::class,
'fr' => FrenchNumberTransformer::class,
'fr_BE' => FrenchBelgianNumberTransformer::class,
'hu' => HungarianNumberTransformer::class,
'id' => IndonesianNumberTransformer::class,
'it' => ItalianNumberTransformer::class,
'ka' => GeorgianNumberTransformer::class,
'lt' => LithuanianNumberTransformer::class,
'lv' => LatvianNumberTransformer::class,
'ms' => MalayNumberTransformer::class,
'nl' => DutchNumberTransformer::class,
'pl' => PolishNumberTransformer::class,
'pt_BR' => PortugueseBrazilianNumberTransformer::class,
'ro' => RomanianNumberTransformer::class,
'ru' => RussianNumberTransformer::class,
'sk' => SlovakNumberTransformer::class,
'sv' => SwedishNumberTransformer::class,
'tk' => TurkmenNumberTransformer::class,
'tr' => TurkishNumberTransformer::class,
'ua' => UkrainianNumberTransformer::class,
'yo' => YorubaNumberTransformer::class,
];

private array $currencyTransformers = [
'ar' => ArabicCurrencyTransformer::class,
'al' => AlbanianCurrencyTransformer::class,
'az' => AzerbaijaniCurrencyTransformer::class,
'de' => GermanCurrencyTransformer::class,
'dk' => DanishCurrencyTransformer::class,
'en' => EnglishCurrencyTransformer::class,
'es' => SpanishCurrencyTransformer::class,
'fr' => FrenchCurrencyTransformer::class,
'hu' => HungarianCurrencyTransformer::class,
'id' => IndonesianCurrencyTransformer::class,
'ka' => GeorgianCurrencyTransformer::class,
'lt' => LithuanianCurrencyTransformer::class,
'lv' => LatvianCurrencyTransformer::class,
'ms' => MalaysianCurrencyTransformer::class,
'pl' => PolishCurrencyTransformer::class,
'pt_BR' => PortugueseBrazilianCurrencyTransformer::class,
'ro' => RomanianCurrencyTransformer::class,
'ru' => RussianCurrencyTransformer::class,
'sk' => SlovakCurrencyTransformer::class,
'tk' => TurkmenCurrencyTransformer::class,
'tr' => TurkishCurrencyTransformer::class,
'ua' => UkrainianCurrencyTransformer::class,
'yo' => YorubaCurrencyTransformer::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]();
}

/**
* @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]();
}
use ManagesCurrencyTransformers;
use ManagesNumberTransformers;
}
9 changes: 9 additions & 0 deletions tests/NumberToWordsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ public function testItThrowsExceptionIfCurrencyTransformerDoesNotExist(): void
$numberToWords = new NumberToWords();
$numberToWords->getCurrencyTransformer('xx');
}

public function testItNumberTransformerAndCurrencyTransformerWithStaticMethods(): void
{
$numberToWords = NumberToWords::transformNumber('en', 5120);
$currencyToWords = NumberToWords::transformCurrency('en', 5099, 'USD');

$this->assertEquals($numberToWords, 'five thousand one hundred twenty');
$this->assertEquals($currencyToWords, 'fifty dollars ninety-nine cents');
}
}

0 comments on commit 7aa709e

Please sign in to comment.