add types

This commit is contained in:
2024-11-03 22:37:38 +05:00
parent 62ea6af082
commit 0a860b3259
6 changed files with 74 additions and 26 deletions

View File

@@ -4,14 +4,33 @@ namespace App\Modules\TurkmenNumberFormatter\Repositories;
class TurkmenNumberFormatter
{
// Define number mappings
private static $units = ['', 'bir', 'iki', 'üç', 'dört', 'bäş', 'alty', 'ýedi', 'sekiz', 'dokuz'];
/**
* Units
*
* @var array<int, string>
*/
private static array $units = ['', 'bir', 'iki', 'üç', 'dört', 'bäş', 'alty', 'ýedi', 'sekiz', 'dokuz'];
private static $tens = ['', 'on', 'ýigrimi', 'otuz', 'kyrk', 'elli', 'altmyş', 'ýetmiş', 'segsen', 'togsan'];
/**
* Tens
*
* @var array<int, string>
*/
private static array $tens = ['', 'on', 'ýigrimi', 'otuz', 'kyrk', 'elli', 'altmyş', 'ýetmiş', 'segsen', 'togsan'];
private static $hundreds = ['', 'ýüz', 'iki ýüz', 'üç ýüz', 'dört ýüz', 'bäş ýüz', 'alty ýüz', 'ýedi ýüz', 'sekiz ýüz', 'dokuz ýüz'];
/**
* Hundreds
*
* @var array<int, string>
*/
private static array $hundreds = ['', 'ýüz', 'iki ýüz', 'üç ýüz', 'dört ýüz', 'bäş ýüz', 'alty ýüz', 'ýedi ýüz', 'sekiz ýüz', 'dokuz ýüz'];
private static $largeNumbers = ['', 'müň', 'million', 'milliard', 'trillion'];
/**
* Large mumbers
*
* @var array<int, string>
*/
private static array $largeNumbers = ['', 'müň', 'million', 'milliard', 'trillion'];
/**
* Main method to format a given amount in Turkmen.
@@ -19,16 +38,16 @@ class TurkmenNumberFormatter
* @param float $amount The amount to format.
* @return string The amount in written Turkmen format.
*/
public static function format($amount)
public static function format(float $amount): string
{
// Split the amount into whole and fractional parts
[$whole, $fraction] = explode('.', number_format($amount, 2, '.', ''));
// Convert the whole part
$wholeWords = self::convertWholePart($whole);
$wholeWords = self::convertWholePart(intval($whole));
// Convert the fractional part
$fractionWords = self::convertFractionalPart($fraction);
$fractionWords = self::convertFractionalPart(intval($fraction));
return trim($wholeWords.' '.$fractionWords);
}
@@ -39,7 +58,7 @@ class TurkmenNumberFormatter
* @param int $whole The whole part of the number.
* @return string The converted whole part in Turkmen.
*/
private static function convertWholePart($whole)
private static function convertWholePart(int $whole): string
{
$words = '';
$level = 0;
@@ -63,7 +82,7 @@ class TurkmenNumberFormatter
* @param int $fraction The fractional part of the number.
* @return string The converted fractional part in Turkmen.
*/
private static function convertFractionalPart($fraction)
private static function convertFractionalPart(int $fraction): string
{
if ($fraction <= 0) {
return '';
@@ -85,7 +104,7 @@ class TurkmenNumberFormatter
* @param int $num The number to convert.
* @return string The converted number in Turkmen.
*/
private static function convertToWords($num)
private static function convertToWords(int $num): string
{
$words = '';