src/CurrencyTransformer/AzerbaijaniCurrencyTransformer.php
<?php
namespace NumberToWords\CurrencyTransformer;
use NumberToWords\Exception\NumberToWordsException;
use NumberToWords\Language\Azerbaijani\AzerbaijaniDictionary;
use NumberToWords\Language\Azerbaijani\AzerbaijaniExponentGetter;
use NumberToWords\Language\Azerbaijani\AzerbaijaniTripletTransformer;
use NumberToWords\NumberTransformer\NumberTransformerBuilder;
use NumberToWords\Service\NumberToTripletsConverter;
use NumberToWords\TransformerOptions\CurrencyTransformerOptions;
class AzerbaijaniCurrencyTransformer implements CurrencyTransformer
{
public function toWords(int $amount, string $currency, ?CurrencyTransformerOptions $options = null): string
{
$dictionary = new AzerbaijaniDictionary();
$numberToTripletsConverter = new NumberToTripletsConverter();
$tripletTransformer = new AzerbaijaniTripletTransformer($dictionary);
$exponentInflector = new AzerbaijaniExponentGetter();
$numberTransformer = (new NumberTransformerBuilder())
->withDictionary($dictionary)
->withWordsSeparatedBy(' ')
->transformNumbersBySplittingIntoTriplets($numberToTripletsConverter, $tripletTransformer)
->useRegularExponents($exponentInflector)
->build();
$decimal = (int) ($amount / 100);
$fraction = abs($amount % 100);
if ($fraction === 0) {
$fraction = null;
}
$currency = strtoupper($currency);
if (!array_key_exists($currency, AzerbaijaniDictionary::$currencyNames)) {
throw new NumberToWordsException(
sprintf('Currency "%s" is not available for "%s" language', $currency, get_class($this))
);
}
$currencyNames = AzerbaijaniDictionary::$currencyNames[$currency];
$return = trim($numberTransformer->toWords($decimal));
$level = ($decimal === 1) ? 0 : 1;
if ($level > 0) {
if (count($currencyNames[0]) > 1) {
$return .= ' ' . $currencyNames[0][$level];
} else {
$return .= ' ' . $currencyNames[0][0];
}
} else {
$return .= ' ' . $currencyNames[0][0];
}
if (null !== $fraction) {
$return .= ' ' . trim($numberTransformer->toWords($fraction));
$level = $fraction === 1 ? 0 : 1;
if ($level > 0) {
if (count($currencyNames[1]) > 1) {
$return .= ' ' . $currencyNames[1][$level];
} else {
$return .= ' ' . $currencyNames[1][0];
}
} else {
$return .= ' ' . $currencyNames[1][0];
}
}
return $return;
}
}