Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 731c781

Browse files
author
Tochukwu Nkemdilim
authored
Fixes #13: Remove Group Value For Empty Money Block (#14)
1 parent f24caa6 commit 731c781

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/Grammar/SentenceGenerator.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,28 @@ private static function _generateSentence(array $moneyValueArray)
5555
$sentence .= "{$hundredth} {$tensAndUnit} ";
5656
}
5757

58-
if ($groupValue != "") {
59-
$sentence .= $groupValue . ", ";
58+
if ($groupValue != "" && ($tensAndUnit != "" || $hundredth != "")) {
59+
$sentence = rtrim($sentence) . " " . $groupValue . ", ";
6060
}
6161
}
6262

63+
return static::_trimGeneratedSentence($sentence);
64+
}
65+
66+
/**
67+
* Trim end of sentence generated by removing unwanted
68+
* characters, symbols, and whitespaces.
69+
*
70+
* @param String $sentence
71+
* @return String
72+
*/
73+
private static function _trimGeneratedSentence($sentence): string
74+
{
75+
$sentence = rtrim($sentence);
76+
if (Str::endsWith($sentence, ",")) {
77+
$sentence = substr($sentence, 0, strlen($sentence) - 1);
78+
}
79+
6380
return trim($sentence);
6481
}
6582
}

src/Helpers/StringProcessing.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44

55
class StringProcessing
66
{
7+
/**
8+
* Checks if a string ends with
9+
*
10+
* @param [type] $haystack
11+
* @param [type] $needle
12+
* @param boolean $case
13+
* @return void
14+
*/
15+
public static function endsWith($haystack, $needle, $case = true)
16+
{
17+
$length = strlen($needle);
18+
if ($length == 0) {
19+
return true;
20+
}
21+
22+
return (substr($haystack, -$length) === $needle);
23+
}
24+
725
/**
826
* Prefix a given string with a fixed amount of zero.
927
*
@@ -68,4 +86,4 @@ public static function splitIntoArrayOfSizeThree(String $stringVal)
6886

6987
return $colletion;
7088
}
71-
}
89+
}

tests/EnglishConversionTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
* Autoload files using Composer autoload
55
*/
66

7+
use PHPUnit\Framework\TestCase;
78
use TNkemdilim\MoneyToWords\Converter;
89
use TNkemdilim\MoneyToWords\Languages as Language;
9-
use TNkemdilim\MoneyToWords\Exception;
10-
use PHPUnit\Framework\TestCase;
1110

1211
class EnglishConversionTest extends TestCase
1312
{
1413
protected $converter;
1514

16-
protected function setUp() : void
15+
protected function setUp(): void
1716
{
1817
$this->converter = new Converter("naira", "kobo", Language::ENGLISH);
1918
}
@@ -42,6 +41,24 @@ public function testWholeNumber()
4241
);
4342
}
4443

44+
public function testLargeNumbers()
45+
{
46+
$this->assertEquals(
47+
$this->converter->convert("50000000"),
48+
"fifty million naira only"
49+
);
50+
51+
$this->assertEquals(
52+
$this->converter->convert("900000000000"),
53+
"nine hundred billion naira only"
54+
);
55+
56+
$this->assertEquals(
57+
$this->converter->convert("900070000000"),
58+
"nine hundred billion, seventy million naira only"
59+
);
60+
}
61+
4562
public function testNumberWithZeroPrefix()
4663
{
4764
$this->assertEquals($this->converter->convert("0000000"), "");
@@ -79,4 +96,3 @@ public function testDecimalNumber()
7996
// $money = "八百七十二万七千八百二十四";
8097

8198
// echo ($this->converter->convert($money) . "\n");
82-
?>

0 commit comments

Comments
 (0)