Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hebrew #166

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Note: The Currency Transformer within this library processes integers; ensure yo
| Ukrainian | ua | + | + |
| Uzbek | uz | + | + |
| Yoruba | yo | + | + |
| Hebrew | he | + | + |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please order the list alphabetically?


## Contributors

Expand Down
1 change: 1 addition & 0 deletions src/Concerns/ManagesCurrencyTransformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trait ManagesCurrencyTransformers
'ua' => Transformer\UkrainianCurrencyTransformer::class,
'uz' => Transformer\UzbekCurrencyTransformer::class,
'yo' => Transformer\YorubaCurrencyTransformer::class,
'he' => Transformer\HebrewCurrencyTransformer::class,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add he on the list in the alphabetical order?

];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Concerns/ManagesNumberTransformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ trait ManagesNumberTransformers
'ua' => Transformer\UkrainianNumberTransformer::class,
'uz' => Transformer\UzbekNumberTransformer::class,
'yo' => Transformer\YorubaNumberTransformer::class,
'he' => Transformer\HebrewNumberTransformer::class,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add he on the list in the alphabetical order?

];

/**
Expand Down
80 changes: 80 additions & 0 deletions src/CurrencyTransformer/HebrewCurrencyTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace NumberToWords\CurrencyTransformer;

use NumberToWords\Exception\NumberToWordsException;
use NumberToWords\Language\Hebrew\HebrewDictionary;
use NumberToWords\Language\Hebrew\HebrewExponentInflector;
use NumberToWords\Language\Hebrew\HebrewNounGenderInflector;
use NumberToWords\Language\Hebrew\HebrewTripletTransformer;
use NumberToWords\NumberTransformer\NumberTransformerBuilder;
use NumberToWords\Service\NumberToTripletsConverter;
use NumberToWords\TransformerOptions\CurrencyTransformerOptions;

class HebrewCurrencyTransformer implements CurrencyTransformer
{
public function toWords(int $amount, string $currency, ?CurrencyTransformerOptions $options = null): string
{
$dictionary = new HebrewDictionary();
$numberToTripletsConverter = new NumberToTripletsConverter();
$tripletTransformer = new HebrewTripletTransformer($dictionary);
$exponentInflector = new HebrewExponentInflector(new HebrewNounGenderInflector());

$numberTransformer = (new NumberTransformerBuilder())
->withDictionary($dictionary)
->withWordsSeparatedBy(' ')
->transformNumbersBySplittingIntoPowerAwareTriplets($numberToTripletsConverter, $tripletTransformer)
->inflectExponentByNumbers($exponentInflector)
->build();

$decimal = (int) ($amount / 100);

$fraction = abs($amount % 100);

if ($fraction === 0) {
$fraction = null;
}

$currency = strtoupper($currency);

if (!array_key_exists($currency, HebrewDictionary::$currencyNames)) {
throw new NumberToWordsException(
sprintf('Currency "%s" is not available for "%s" language', $currency, get_class($this))
);
}

$currencyNames = HebrewDictionary::$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] . 's';
}
} 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] . 's';
}
} else {
$return .= ' ' . $currencyNames[1][0];
}
}

return $return;
}
}
138 changes: 138 additions & 0 deletions src/Language/Hebrew/HebrewDictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace NumberToWords\Language\Hebrew;

use NumberToWords\Language\Dictionary;

class HebrewDictionary implements Dictionary
{
public const LOCALE = 'he_IL';
public const LANGUAGE_NAME = 'עברית';
public const LANGUAGE_NAME_NATIVE = 'עברית';

private static array $units = ['', 'אחד', 'שתיים', 'שלוש', 'ארבע', 'חמש', 'שש', 'שבע', 'שמונה', 'תשע'];

private static array $teens = [
'עשר',
'אחד עשרה',
'שתיים עשרה',
'שלוש עשרה',
'ארבע עשרה',
'חמש עשרה',
'שש עשרה',
'שבע עשרה',
'שמונה עשרה',
'תשע עשרה'
];

private static array $tens = [
'',
'עשר',
'עשרים',
'שלושים',
'ארבעים',
'חמישים',
'שישים',
'שבעים',
'שמונים',
'תשעים'
];

private static array $hundred = [
'',
'מאה',
'מאתיים',
'שלוש מאות',
'ארבע מאות',
'חמש מאות',
'שש מאות',
'שבע מאות',
'שמונה מאות',
'תשע מאות'
];

public static array $currencyNames = [
'ALL' => [['lek'], ['qindarka']],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please translate other currencies using Hebrew alphabet?

'AED' => [['Dirham'], ['Fils']],
'AUD' => [['Australian dollar'], ['cent']],
'BAM' => [['convertible marka'], ['fenig']],
'BGN' => [['lev'], ['stotinka']],
'BRL' => [['real'], ['centavos']],
'BYR' => [['Belarussian rouble'], ['kopiejka']],
'CAD' => [['Canadian dollar'], ['cent']],
'CHF' => [['Swiss franc'], ['rapp']],
'CYP' => [['Cypriot pound'], ['cent']],
'CZK' => [['Czech koruna'], ['halerz']],
'DKK' => [['Danish krone'], ['ore']],
'DZD' => [['dinar'], ['cent']],
'EEK' => [['kroon'], ['senti']],
'EGP' => [['Egyptian Pound'], ['piastre']],
'EUR' => [['אירו'], ['אירו סנט']],
'GBP' => [['pound', 'pounds'], ['pence', 'pence']],
'HKD' => [['Hong Kong dollar'], ['cent']],
'HRK' => [['Croatian kuna'], ['lipa']],
'HUF' => [['forint'], ['filler']],
'ILS' => [['ש"ח', 'ש"ח'], ['אגו\'', 'אגו\'']],
'ISK' => [['Icelandic króna'], ['aurar']],
'JPY' => [['yen'], ['sen']],
'LTL' => [['litas'], ['cent']],
'LVL' => [['lat'], ['sentim']],
'LYD' => [['dinar'], ['cent']],
'MAD' => [['dirham'], ['cent']],
'MKD' => [['Macedonian dinar'], ['deni']],
'MRO' => [['ouguiya'], ['khoums']],
'MTL' => [['Maltese lira'], ['centym']],
'NGN' => [['Naira'], ['kobo']],
'NOK' => [['Norwegian krone'], ['oere']],
'PHP' => [['peso'], ['centavo']],
'PLN' => [['zloty', 'zlotys'], ['grosz']],
'ROL' => [['Romanian leu'], ['bani']],
'RUB' => [['Russian Federation rouble'], ['kopiejka']],
'SAR' => [['Riyal'], ['Halalah']],
'SEK' => [['Swedish krona'], ['oere']],
'SIT' => [['Tolar'], ['stotinia']],
'SKK' => [['Slovak koruna'], []],
'TMT' => [['manat'], ['tenge']],
'TND' => [['dinar'], ['millime']],
'TRL' => [['lira'], ['kuruş']],
'TRY' => [['lira'], ['kuruş']],
'UAH' => [['hryvna'], ['cent']],
'USD' => [['דולר', 'דולר'], ['סנט', 'סנט']],
'XAF' => [['CFA franc'], ['cent']],
'XOF' => [['CFA franc'], ['cent']],
'XPF' => [['CFP franc'], ['centime']],
'YUM' => [['dinar'], ['para']],
'ZAR' => [['rand'], ['cent']],
'UZS' => [['sum'], ['tiyin']],
];

public function getZero(): string
{
return 'אפס';
}

public function getMinus(): string
{
return 'מינוס';
}

public function getCorrespondingUnit(int $unit): string
{
return self::$units[$unit];
}

public function getCorrespondingTen(int $ten): string
{
return self::$tens[$ten];
}

public function getCorrespondingTeen(int $teen): string
{
return self::$teens[$teen];
}

public function getCorrespondingHundred(int $hundred): string
{
return self::$hundred[$hundred];
}
}
38 changes: 38 additions & 0 deletions src/Language/Hebrew/HebrewExponentGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace NumberToWords\Language\Hebrew;

use NumberToWords\Language\ExponentGetter;

class HebrewExponentGetter implements ExponentGetter
{
private static array $exponent = [
'',
'אלף',
'מיליון',
'מיליארד',
'טריליון',
'קוודריליון',
'קווינטיליון',
'סקסטיליון',
'ספטיליון',
'אוקטיליון',
'נוניליון',
'דציליון',
'אנדציליון',
'דודציליון',
'טרדציליון',
'קוואטורדציליון',
'קווינדציליון',
'סקסדציליון',
'ספטנדציליון',
'אוקטודציליון',
'נובמדציליון',
'ויגינטיליון',
];

public function getExponent(int $power): string
{
return self::$exponent[$power];
}
}
73 changes: 73 additions & 0 deletions src/Language/Hebrew/HebrewExponentInflector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace NumberToWords\Language\Hebrew;

use NumberToWords\Language\ExponentInflector;

class HebrewExponentInflector implements ExponentInflector
{
private static $exponent = [
['', '', ''],
['אלפים', 'אלף'], // 'אלף' is 'thousand
['מיליון', 'מיליון'], // 'מיליון' is 'million'
['מיליארד', 'מיליארד'], // 'מיליארד' is 'billion'
['טריליון', 'טריליון'], // 'טריליון' is 'trillion'
['קוודריליון', 'קוודריליון'], // 'קוודריליון' is 'quadrillion'
['קווינטיליון', 'קווינטיליון'], // 'קווינטיליון' is 'quintillion'
['סקסטיליון', 'סקסטיליון'], // 'סקסטיליון' is 'sextillion'
['ספטיליון', 'ספטיליון'], // 'ספטיליון' is 'septillion'
['אוקטיליון', 'אוקטיליון'], // 'אוקטיליון' is 'octillion'
['נוניליון', 'נוניליון'], // 'נוניליון' is 'nonillion'
['דציליון', 'דציליון'], // 'דציליון' is 'decillion'
['אנדציליון', 'אנדציליון'], // 'אנדציליון' is 'undecillion'
['דודציליון', 'דודציליון'], // 'דודציליון
];

private static $exponentBetweenTenToTeweny = [
'',
'אלף',
'אלפיים',
'שלושת',
'ארבעת',
'חמשת',
'ששת',
'שבעת',
'שמונת',
'תשעת',
'עשרת',
];

/**
* @var HebrewNounGenderInflector
*/
private $inflector;

/**
* @param HebrewNounGenderInflector $inflector
*/
public function __construct(HebrewNounGenderInflector $inflector)
{
$this->inflector = $inflector;
}


/**
* @param int $number
* @param int $power
*
* @return string
*/
public function inflectExponent($number, $power): string
{
if ($power === 0) {
return '';
}

return $this->inflector->inflectNounByNumber(
$number,
self::$exponent[$power][0],
self::$exponent[$power][1],
$power === 1 ? self::$exponentBetweenTenToTeweny : []
);
}
}
35 changes: 35 additions & 0 deletions src/Language/Hebrew/HebrewNounGenderInflector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace NumberToWords\Language\Hebrew;

class HebrewNounGenderInflector
{
/**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use type hinting instead of docblocks? This library requires min PHP7

* @param int $number
* @param string $singular
* @param string $plural
* @param string $genitivePlural
*
* @return string
*/
public function inflectNounByNumber($number, $plural, $genitivePlural, array $exponentBetweenTenToTeweny)
{
$units = $number % 10;
$tens = ((int) ($number / 10)) % 10;

if ($number > 1 && $number <= 10) {
if($number === 2) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSR12 error

Suggested change
if($number === 2) {
if ($number === 2) {

return $exponentBetweenTenToTeweny[$number] ?? $plural;
}
return ($exponentBetweenTenToTeweny[$number] ?? false)
? $exponentBetweenTenToTeweny[$number] . ' ' . $plural
: $plural;
}

if (($units > 2 && $tens === 0) || ($units === 0 && $tens === 1)) {
return $genitivePlural;
}

return $genitivePlural;
}
}
Loading