diff --git a/README.md b/README.md index 3bd90aa3..6ae78daf 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Note: The Currency Transformer within this library processes integers; ensure yo | Ukrainian | ua | + | + | | Uzbek | uz | + | + | | Yoruba | yo | + | + | +| Hebrew | he | + | + | ## Contributors diff --git a/src/Concerns/ManagesCurrencyTransformers.php b/src/Concerns/ManagesCurrencyTransformers.php index e950eede..20302387 100644 --- a/src/Concerns/ManagesCurrencyTransformers.php +++ b/src/Concerns/ManagesCurrencyTransformers.php @@ -32,6 +32,7 @@ trait ManagesCurrencyTransformers 'ua' => Transformer\UkrainianCurrencyTransformer::class, 'uz' => Transformer\UzbekCurrencyTransformer::class, 'yo' => Transformer\YorubaCurrencyTransformer::class, + 'he' => Transformer\HebrewCurrencyTransformer::class, ]; /** diff --git a/src/Concerns/ManagesNumberTransformers.php b/src/Concerns/ManagesNumberTransformers.php index d641aeed..c2bd13eb 100644 --- a/src/Concerns/ManagesNumberTransformers.php +++ b/src/Concerns/ManagesNumberTransformers.php @@ -44,6 +44,7 @@ trait ManagesNumberTransformers 'ua' => Transformer\UkrainianNumberTransformer::class, 'uz' => Transformer\UzbekNumberTransformer::class, 'yo' => Transformer\YorubaNumberTransformer::class, + 'he' => Transformer\HebrewNumberTransformer::class, ]; /** diff --git a/src/CurrencyTransformer/HebrewCurrencyTransformer.php b/src/CurrencyTransformer/HebrewCurrencyTransformer.php new file mode 100644 index 00000000..53c61cb5 --- /dev/null +++ b/src/CurrencyTransformer/HebrewCurrencyTransformer.php @@ -0,0 +1,80 @@ +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; + } +} diff --git a/src/Language/Hebrew/HebrewDictionary.php b/src/Language/Hebrew/HebrewDictionary.php new file mode 100644 index 00000000..1019acbd --- /dev/null +++ b/src/Language/Hebrew/HebrewDictionary.php @@ -0,0 +1,138 @@ + [['lek'], ['qindarka']], + '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]; + } +} diff --git a/src/Language/Hebrew/HebrewExponentGetter.php b/src/Language/Hebrew/HebrewExponentGetter.php new file mode 100644 index 00000000..7381a48b --- /dev/null +++ b/src/Language/Hebrew/HebrewExponentGetter.php @@ -0,0 +1,38 @@ +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 : [] + ); + } +} diff --git a/src/Language/Hebrew/HebrewNounGenderInflector.php b/src/Language/Hebrew/HebrewNounGenderInflector.php new file mode 100644 index 00000000..4feb9da2 --- /dev/null +++ b/src/Language/Hebrew/HebrewNounGenderInflector.php @@ -0,0 +1,35 @@ + 1 && $number <= 10) { + 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; + } +} diff --git a/src/Language/Hebrew/HebrewTripletTransformer.php b/src/Language/Hebrew/HebrewTripletTransformer.php new file mode 100644 index 00000000..b0687241 --- /dev/null +++ b/src/Language/Hebrew/HebrewTripletTransformer.php @@ -0,0 +1,75 @@ +dictionary = $dictionary; + } + + /** + * @param int $number + * + * @return string + */ + public function transformToWords($number, $power): string + { + + $units = $number % 10; + $tens = (int) ($number / 10) % 10; + $hundreds = (int) ($number / 100) % 10; + $words = []; + + if ($hundreds > 0) { + $words[] = $this->dictionary->getCorrespondingHundred($hundreds); + } + + if ($tens !== 0 || $units !== 0) { + $words[] = $this->getSubHundred($tens, $units, $power, $number > 100); + } + return implode(' ', $words); + } + + /** + * @param int $tens + * @param int $units + * + * @return string + */ + private function getSubHundred($tens, $units, $power, $exPower) + { + $words = []; + + if ($tens === 1 && (($units > 0 || $exPower))) { + $words[] = $this->dictionary->getCorrespondingTeen($units); + } else { + if ($units > 9 || $tens || $power === 0) { + if ($tens > 0 && !($tens === 1 && $units === 0)) { + $words[] = $this->dictionary->getCorrespondingTen($tens); + } + if ($units > 0) { + $words[] = $this->dictionary->getCorrespondingUnit($units); + } + if ($units === 0 && $tens === 1) { + $words[] = $this->dictionary->getCorrespondingTen($tens); + } + } elseif ((!$power && !$tens) || (!$tens && $units && $exPower)) { + $words[] = $this->dictionary->getCorrespondingUnit($units); + } elseif ($tens) { + $words[] = $this->dictionary->getCorrespondingTen($tens); + } + } + + if (count($words) === 1 && $exPower) { + return 'ו' . $words[0]; + } + + return implode(' ו', $words); + } +} diff --git a/src/NumberTransformer/HebrewNumberTransformer.php b/src/NumberTransformer/HebrewNumberTransformer.php new file mode 100644 index 00000000..06b24d09 --- /dev/null +++ b/src/NumberTransformer/HebrewNumberTransformer.php @@ -0,0 +1,29 @@ +withDictionary($dictionary) + ->withWordsSeparatedBy(' ') + ->transformNumbersBySplittingIntoPowerAwareTriplets($numberToTripletsConverter, $tripletTransformer) + ->inflectExponentByNumbers($exponentInflector) + ->build(); + + return $numberTransformer->toWords($number); + } +}