Skip to content

Commit a81fa52

Browse files
authored
Merge pull request #3 from mamyraoby/develop
Add billions support and helper function
2 parents 155ca48 + 62018f1 commit a81fa52

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ echo $converter->toWords(7); // fito
2929
echo $converter->toWords(69); // sivy amby enimpolo
3030
echo $converter->toWords(2025); // dimy amby roapolo sy roa arivo
3131
echo $converter->toWords(3429267); // fito amby enimpolo sy roanjato sy sivy arivo sy roa alina sy efatra hetsy sy telo tapitrisa
32-
```
32+
33+
// You can also use the global helper function if you've configured autoloading in Composer:
34+
echo phanisana_convert_number(7); // fito
35+
echo phanisana_convert_number(69); // sivy amby enimpolo
36+
echo phanisana_convert_number(2025); // dimy amby roapolo sy roa arivo
37+
echo phanisana_convert_number(3429267); // fito amby enimpolo sy roanjato sy sivy arivo sy roa alina sy efatra hetsy sy telo tapitrisa
38+
```
3339

3440
This demonstrates how to convert various number ranges into Malagasy words using the `NumberConverter`.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"psr-4": {
88
"MamyRaoby\\Phanisana\\": "src",
99
"MamyRaoby\\Phanisana\\Test\\": "test"
10-
}
10+
},
11+
"files": [
12+
"helpers/converter.php"
13+
]
1114
},
1215
"authors": [
1316
{

helpers/converter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use MamyRaoby\Phanisana\Converter\NumberConverter;
4+
5+
if (!function_exists('phanisana_convert_number')) {
6+
function phanisana_convert_number(int $number): string {
7+
$converter = new NumberConverter();
8+
return $converter->toWords($number);
9+
}
10+
}

src/Converter/NumberConverter.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ class NumberConverter
88
{
99
public function toWords(int $number): string
1010
{
11+
if ($number >= 1000000000) {
12+
$millions = $number % 1000000000;
13+
if ($millions === 0) {
14+
return $this->convertBillions($number);
15+
}
16+
17+
return $this->toWords($millions) . Dictionary::GLUE_SY->value . $this->convertBillions($number - $millions);
18+
}
19+
1120
if ($number >= 1000000) {
1221
$hundredThousands = $number % 1000000;
1322
if ($hundredThousands === 0) {
@@ -182,17 +191,49 @@ protected function convertHundredThousands(int $number): string
182191
}
183192

184193
protected function convertMillions(int $number): string
194+
{
195+
$beforeMillions = $number / 1000000;
196+
197+
if($beforeMillions >= 1 && $beforeMillions <= 9){
198+
return match ($number) {
199+
1000000 => Dictionary::MILLION->value,
200+
2000000 => Dictionary::TWO_MILLION->value,
201+
3000000 => Dictionary::THREE_MILLION->value,
202+
4000000 => Dictionary::FOUR_MILLION->value,
203+
5000000 => Dictionary::FIVE_MILLION->value,
204+
6000000 => Dictionary::SIX_MILLION->value,
205+
7000000 => Dictionary::SEVEN_MILLION->value,
206+
8000000 => Dictionary::EIGHT_MILLION->value,
207+
9000000 => Dictionary::NINE_MILLION->value,
208+
default => '',
209+
};
210+
}
211+
212+
if($beforeMillions >= 10 && $beforeMillions <= 99)
213+
{
214+
return $this->toWords($beforeMillions).' tapitrisa';
215+
}
216+
217+
if($beforeMillions >= 100 && $beforeMillions <= 999)
218+
{
219+
return $this->toWords($beforeMillions).' tapitrisa';
220+
}
221+
222+
return '';
223+
}
224+
225+
protected function convertBillions(int $number): string
185226
{
186227
return match ($number) {
187-
1000000 => Dictionary::MILLION->value,
188-
2000000 => Dictionary::TWO_MILLION->value,
189-
3000000 => Dictionary::THREE_MILLION->value,
190-
4000000 => Dictionary::FOUR_MILLION->value,
191-
5000000 => Dictionary::FIVE_MILLION->value,
192-
6000000 => Dictionary::SIX_MILLION->value,
193-
7000000 => Dictionary::SEVEN_MILLION->value,
194-
8000000 => Dictionary::EIGHT_MILLION->value,
195-
9000000 => Dictionary::NINE_MILLION->value,
228+
1000000000 => Dictionary::BILLION->value,
229+
2000000000 => Dictionary::TWO_BILLION->value,
230+
3000000000 => Dictionary::THREE_BILLION->value,
231+
4000000000 => Dictionary::FOUR_BILLION->value,
232+
5000000000 => Dictionary::FIVE_BILLION->value,
233+
6000000000 => Dictionary::SIX_BILLION->value,
234+
7000000000 => Dictionary::SEVEN_BILLION->value,
235+
8000000000 => Dictionary::EIGHT_BILLION->value,
236+
9000000000 => Dictionary::NINE_BILLION->value,
196237
default => '',
197238
};
198239
}

src/Enum/Dictionary.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ enum Dictionary: string
7979
case SEVEN_MILLION = 'fito tapitrisa';
8080
case EIGHT_MILLION = 'valo tapitrisa';
8181
case NINE_MILLION = 'sivy tapitrisa';
82+
83+
case BILLION = 'iray lavitrisa';
84+
case TWO_BILLION = 'roa lavitrisa';
85+
case THREE_BILLION = 'telo lavitrisa';
86+
case FOUR_BILLION = 'efatra lavitrisa';
87+
case FIVE_BILLION = 'dimy lavitrisa';
88+
case SIX_BILLION = 'enina lavitrisa';
89+
case SEVEN_BILLION = 'fito lavitrisa';
90+
case EIGHT_BILLION = 'valo lavitrisa';
91+
case NINE_BILLION = 'sivy lavitrisa';
8292
}

test/Converter/NumberConverterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testHundreds()
3737
$this->assertEquals('efajato', $converter->toWords(400));
3838
$this->assertEquals('efatra amby efajato', $converter->toWords(404));
3939
$this->assertEquals('iraika amby folo amby zato', $converter->toWords(111));
40+
$this->assertEquals('sivy amby roapolo sy efajato', $converter->toWords(429));
4041
$this->assertEquals('sivy amby sivifolo sy sivinjato', $converter->toWords(999));
4142
}
4243

@@ -76,4 +77,17 @@ public function testMillions()
7677
$this->assertEquals('dimanjato sy dimy arivo sy valo alina sy efatra hetsy sy sivy tapitrisa', $converter->toWords(9485500));
7778
$this->assertEquals('fito amby enimpolo sy roanjato sy sivy arivo sy roa alina sy efatra hetsy sy telo tapitrisa', $converter->toWords(3429267));
7879
}
80+
81+
public function testBillions()
82+
{
83+
$converter = new NumberConverter();
84+
$this->assertEquals('iray lavitrisa', $converter->toWords(1000000000));
85+
$this->assertEquals('dimy arivo sy efatra alina sy telopolo tapitrisa sy sivy lavitrisa', $converter->toWords(9030045000));
86+
$this->assertEquals('fito arivo sy enina alina sy roa hetsy sy sivy amby roapolo sy efajato tapitrisa sy telo lavitrisa', $converter->toWords(3429267000));
87+
}
88+
89+
public function testHelper()
90+
{
91+
$this->assertEquals('iraika amby folo', phanisana_convert_number(11));
92+
}
7993
}

0 commit comments

Comments
 (0)