diff --git a/.gitignore b/.gitignore index 6e12e2d..afb5e35 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ *.ntvs* *.njsproj *.sln -*.sw? \ No newline at end of file +*.sw? + +vendor \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6c25dda --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Numbers to Persian/Dari words converter +Convert any number from 1 to 13 digits to Persian words + +## Installation +You can install this package using composer. +``` +$ composer require arifi/numbers-to-perisan +``` + +## Usage +Genrate random password. +```php +convert($number); // یک‌صد و بيست و سه +``` \ No newline at end of file diff --git a/composer.json b/composer.json index f7e458f..6505011 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,11 @@ ], "autoload": { "psr-4": { - "Arifi\\": "src/Arifi/" - } - }, - "extra": { - "laravel": { - "providers": [ - "Arifi\\NumbersToPersianConverter" - ] - } + "Arifi\\": "src/" + }, + "files": [ + "src/NumbersToPersianConverter.php" + ] }, "minimum-stability": "dev" } \ No newline at end of file diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..bfa0a0e --- /dev/null +++ b/composer.lock @@ -0,0 +1,20 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "c6dd1789af7a181bde7feae436b37da4", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3.0" + }, + "platform-dev": [], + "plugin-api-version": "2.1.0" +} diff --git a/src/NumbersToPersianConverter.php b/src/NumbersToPersianConverter.php new file mode 100644 index 0000000..41b760d --- /dev/null +++ b/src/NumbersToPersianConverter.php @@ -0,0 +1,206 @@ + 'و' , + + '1' => 'يك' , + '2' => 'دو' , + '3' => 'سه' , + '4' => 'چهار' , + '5' => 'پنج' , + '6' => 'شش' , + '7' => 'هفت' , + '8' => 'هشت' , + '9' => 'نه' , + + '11' => 'يازده' , + '12' => 'دوازده' , + '13' => 'سيزده' , + '14' => 'چهارده' , + '15' => 'پانزده' , + '16' => 'شانزده' , + '17' => 'هفده' , + '18' => 'هجده' , + '19' => 'نوزده' , + + '10' => 'ده' , + '20' => 'بيست' , + '30' => 'سی' , + '40' => 'چهل' , + '50' => 'پنجاه' , + '60' => 'شصت' , + '70' => 'هفتاد' , + '80' => 'هشتاد' , + '90' => 'نود' , + + '100' => 'يك‌صد' , + '200' => 'دو‌صد' , + '300' => 'سه‌صد' , + '400' => 'چهار‌صد' , + '500' => 'پنج‌صد' , + '600' => 'شش‌صد' , + '700' => 'هفت‌صد' , + '800' => 'هشت‌صد' , + '900' => 'نه‌صد' , + + 't' => 'هزار' , + 'm' => 'ميليون' , + 'b' => 'ميليارد' , + 'tr' => 'تريليون' + ); + + private function oneDigit($number) + { + return $this->numbersToWord[$number]; + } + + private function twoDigits($number) + { + if (fmod($number, 10) == 0 || ($number >= 11 && $number <= 19)) { + $words = $this->numbersToWord[$number]; + } else { + $first = fmod($number, 10); + $second = floor(($number / 10)); + $second *= 10 ; + $words = $this->numbersToWord[$second] . ' ' . $this->numbersToWord[0] . ' ' . $this->numbersToWord[$first]; + } + return $words; + } + + private function threeDigits($number) + { + if (fmod($number, 100) == 0) { + $words = $this->numbersToWord[$number]; + } else { + $first = fmod($number, 100); + $second = floor(($number / 100)); + $second *= 100 ; + if ($first >= 1 && $first <= 9) { + $firstWord = $this->oneDigit($first); + } else { + $firstWord = $this->twoDigits($first); + } + $words = $this->numbersToWord[$second] . ' ' . $this->numbersToWord[0] . ' ' . $firstWord; + } + return $words; + } + + private function oneToThreeDigits($number) + { + if ($number >= 1 && $number <= 9) { + $words = $this->oneDigit($number); + } elseif ($number >= 10 && $number <= 99) { + $words = $this->twoDigits($number); + } elseif ($number >= 100 && $number <= 999) { + $words = $this->threeDigits($number); + } + return $words; + } + + private function fourDigits($number) + { + if (fmod($number, 1000) == 0) { + $number2 = $number/1000; + $number3 = $this->oneToThreeDigits($number2); + $words = $number3 . ' ' . $this->numbersToWord['t']; + } else { + $first = fmod($number, 1000); + $firstWord = $this->oneToThreeDigits($first); + $second = floor(($number / 1000)); + $secondWord = $this->oneToThreeDigits($second); + $words = $secondWord . ' ' . $this->numbersToWord['t'] . ' ' . $this->numbersToWord[0] . ' ' . $firstWord; + } + return $words; + } + + private function sevenDigits($number) + { + if (fmod($number, 1000000) == 0) { + $number2 = $number/1000000; + $number3 = $this->oneToThreeDigits($number2); + $words = $number3 . ' ' . $this->numbersToWord['m']; + } else { + $first = fmod($number, 1000000); + if ($first >= 1 && $first <= 999) { + $firstWord = $this->oneToThreeDigits($first); + } else { + $firstWord = $this->fourDigits($first); + } + $second = floor(($number / 1000000)); + $secondWord = $this->oneToThreeDigits($second); + $words = $secondWord . ' ' . $this->numbersToWord['m'] . ' ' . $this->numbersToWord[0] . ' ' . $firstWord; + } + return $words; + } + + private function tenDigits($number) + { + if (fmod($number, 1000000000) == 0) { + $number2 = $number/1000000000; + $number3 = $this->oneToThreeDigits($number2); + $words = $number3 . ' ' . $this->numbersToWord['b']; + } else { + $first = fmod($number, 1000000000); + if ($first >= 1 && $first <= 999) { + $firstWord = $this->oneToThreeDigits($first); + } elseif($first >= 1000 && $first <= 999999) { + $firstWord = $this->fourDigits($first); + } else { + $firstWord = $this->sevenDigits($first); + } + $second = floor(($number / 1000000000)); + $secondWord = $this->oneToThreeDigits($second); + $words = $secondWord . ' ' . $this->numbersToWord['b'] . ' ' . $this->numbersToWord[0] . ' ' . $firstWord; + } + return $words; + } + + private function thirteenDigits($number) + { + if (fmod($number, 1000000000000) == 0) { + $number2 = $number/1000000000000; + $number3 = $this->oneToThreeDigits($number2); + $words = $number3 . ' ' . $this->numbersToWord['tr']; + } else { + $first = fmod($number, 1000000000000); + if ($first >= 1 && $first <= 999) { + $firstWord = $this->oneToThreeDigits($first); + } elseif($first >= 1000 && $first <= 999999) { + $firstWord = $this->fourDigits($first); + } elseif($first >= 1000000 && $first <= 999999999) { + $firstWord = $this->sevenDigits($first); + } else { + $firstWord = $this->tenDigits($first); + } + $second = floor(($number / 1000000000000)); + $secondWord = $this->oneToThreeDigits($second); + $words = $secondWord . ' ' . $this->numbersToWord['tr'] . ' ' . $this->numbersToWord[0] . ' ' . $firstWord; + } + return $words; + } + + public function convert($number) + { + if ($number >= 1 && $number <= 9) { + $words = $this->oneDigit($number); + } elseif ($number >= 10 && $number <= 99) { + $words = $this->twoDigits($number); + } elseif ($number >= 100 && $number <= 999) { + $words = $this->threeDigits($number); + } elseif ($number >= 1000 && $number <= 999999) { + $words = $this->fourDigits($number); + } elseif ($number >= 1000000 && $number <= 999999999) { + $words = $this->sevenDigits($number); + } elseif ($number >= 1000000000 && $number <= 999999999999) { + $words = $this->tenDigits($number); + } elseif ($number >= 1000000000000 && $number <= 999999999999999) { + $words = $this->thirteenDigits($number); + } else { + $words = 'Out of range'; + } + return $words; + } +} \ No newline at end of file