Skip to content

Commit c25ccf6

Browse files
authored
Merge pull request #12 from jurchiks/upgrade-to-php7
Upgrade to php 7
2 parents 86c01bd + 21d1ec6 commit c25ccf6

File tree

10 files changed

+53
-66
lines changed

10 files changed

+53
-66
lines changed

.idea/php.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"email": "[email protected]"
1515
}
1616
],
17+
"require": {
18+
"php": ">=7.1"
19+
},
1720
"autoload": {
1821
"psr-4": {
1922
"js\\tools\\numbers2words\\": "src/"

src/Speller.php

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,10 @@ private final function __construct()
5757
* @param string $language A two-letter, ISO 639-1 code of the language.
5858
* @return Speller
5959
*/
60-
private static function get($language)
60+
private static function get(string $language): Speller
6161
{
6262
static $spellers = [];
6363

64-
$language = strtolower(trim($language));
65-
66-
if (strlen($language) != 2)
67-
{
68-
throw new InvalidArgumentException('Invalid language code specified, must follow ISO 639-1 format.');
69-
}
70-
7164
if (!isset(self::$languages[$language]))
7265
{
7366
throw new InvalidArgumentException('That language is not implemented yet.');
@@ -81,12 +74,12 @@ private static function get($language)
8174
return $spellers[$language];
8275
}
8376

84-
public static function getAcceptedLanguages()
77+
public static function getAcceptedLanguages(): array
8578
{
8679
return array_keys(self::$languages);
8780
}
8881

89-
public static function getAcceptedCurrencies()
82+
public static function getAcceptedCurrencies(): array
9083
{
9184
return self::$currencies;
9285
}
@@ -99,12 +92,10 @@ public static function getAcceptedCurrencies()
9992
* @return string The number as written in words in the specified language.
10093
* @throws InvalidArgumentException If any parameter is invalid.
10194
*/
102-
public static function spellNumber($number, $language)
95+
public static function spellNumber(int $number, string $language): string
10396
{
104-
self::assertNumber($number);
105-
10697
return self::get($language)
107-
->parseInt(intval($number), false);
98+
->parseInt($number, false);
10899
}
109100

110101
/**
@@ -117,7 +108,7 @@ public static function spellNumber($number, $language)
117108
* @return string The currency as written in words in the specified language.
118109
* @throws InvalidArgumentException If any parameter is invalid.
119110
*/
120-
public static function spellCurrencyShort($amount, $language, $currency)
111+
public static function spellCurrencyShort($amount, string $language, string $currency): string
121112
{
122113
self::assertNumber($amount);
123114
self::validateCurrency($currency);
@@ -148,7 +139,7 @@ public static function spellCurrencyShort($amount, $language, $currency)
148139
* @return string The currency as written in words in the specified language.
149140
* @throws InvalidArgumentException If any parameter is invalid.
150141
*/
151-
public static function spellCurrency($amount, $language, $currency, $requireDecimal = true, $spellDecimal = false)
142+
public static function spellCurrency($amount, string $language, string $currency, bool $requireDecimal = true, bool $spellDecimal = false): string
152143
{
153144
self::assertNumber($amount);
154145
self::validateCurrency($currency);
@@ -176,7 +167,7 @@ public static function spellCurrency($amount, $language, $currency, $requireDeci
176167
return $text;
177168
}
178169

179-
private function parseInt($number, $isDecimalPart, $currency = '')
170+
private function parseInt(int $number, bool $isDecimalPart, string $currency = ''): string
180171
{
181172
$text = '';
182173

@@ -234,29 +225,22 @@ private function parseInt($number, $isDecimalPart, $currency = '')
234225
return $text;
235226
}
236227

237-
protected abstract function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency);
228+
protected abstract function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string;
238229

239-
protected abstract function spellExponent($type, $number, $currency);
230+
protected abstract function spellExponent(string $type, int $number, string $currency): string;
240231

241-
protected abstract function getCurrencyName($type, $number, $currency);
232+
protected abstract function getCurrencyName(string $type, int $number, string $currency): string;
242233

243-
private static function assertNumber($number)
234+
private static function assertNumber($number): void
244235
{
245236
if (!is_numeric($number))
246237
{
247238
throw new InvalidArgumentException('Invalid number specified.');
248239
}
249240
}
250241

251-
private static function validateCurrency(&$currency)
242+
private static function validateCurrency(string $currency): void
252243
{
253-
if (!is_string($currency))
254-
{
255-
throw new InvalidArgumentException('Invalid currency code specified.');
256-
}
257-
258-
$currency = strtoupper(trim($currency));
259-
260244
if (!in_array($currency, self::$currencies))
261245
{
262246
throw new InvalidArgumentException('That currency is not implemented yet.');

src/languages/English.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class English extends Speller
99
protected $minus = 'minus';
1010
protected $decimalSeparator = ' and ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $tens = [
1515
1 => 'ten',
@@ -63,17 +63,17 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
6363

6464
if ($number < 10)
6565
{
66-
$text .= $singles[intval($number)];
66+
$text .= $singles[$number];
6767
}
6868
else if (($number > 10) && ($number < 20))
6969
{
70-
$text .= $teens[intval($number)];
70+
$text .= $teens[$number];
7171
}
7272
else
7373
{
7474
$text .= $tens[intval(substr("$number", 0, 1))];
7575

76-
if ($number % 10 > 0) // twenty five
76+
if ($number % 10 > 0)
7777
{
7878
$text .= ' ' . $singles[$number % 10];
7979
}
@@ -82,7 +82,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
8282
return $text;
8383
}
8484

85-
protected function spellExponent($type, $number, $currency)
85+
protected function spellExponent(string $type, int $number, string $currency): string
8686
{
8787
if ($type === 'million')
8888
{
@@ -97,7 +97,7 @@ protected function spellExponent($type, $number, $currency)
9797
return '';
9898
}
9999

100-
protected function getCurrencyName($type, $number, $currency)
100+
protected function getCurrencyName(string $type, int $number, string $currency): string
101101
{
102102
static $names = [
103103
self::CURRENCY_EURO => [

src/languages/Estonian.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Estonian extends Speller
99
protected $minus = 'miinus';
1010
protected $decimalSeparator = ' ja ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $tens = [
1515
1 => 'kümme',
@@ -82,7 +82,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
8282
return $text;
8383
}
8484

85-
protected function spellExponent($type, $number, $currency)
85+
protected function spellExponent(string $type, int $number, string $currency): string
8686
{
8787
if ($type === 'million')
8888
{
@@ -102,7 +102,7 @@ protected function spellExponent($type, $number, $currency)
102102
return '';
103103
}
104104

105-
protected function getCurrencyName($type, $number, $currency)
105+
protected function getCurrencyName(string $type, int $number, string $currency): string
106106
{
107107
static $names = [
108108
self::CURRENCY_EURO => [

src/languages/Latvian.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Latvian extends Speller
99
protected $minus = 'mīnus';
1010
protected $decimalSeparator = ' un ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $hundreds = [
1515
1 => 'viens simts',
@@ -81,7 +81,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
8181
return $text;
8282
}
8383

84-
private function spellSingle($digit, $isDecimalPart, $currency)
84+
private function spellSingle(int $digit, bool $isDecimalPart, string $currency): string
8585
{
8686
static $singlesMasculine = [
8787
0 => 'nulle',
@@ -111,13 +111,13 @@ private function spellSingle($digit, $isDecimalPart, $currency)
111111
if ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE))
112112
{
113113
// russian kopek nouns are feminine gender in Latvian
114-
return $singlesFeminine[intval($digit)];
114+
return $singlesFeminine[$digit];
115115
}
116116

117-
return $singlesMasculine[intval($digit)];
117+
return $singlesMasculine[$digit];
118118
}
119119

120-
protected function spellExponent($type, $number, $currency)
120+
protected function spellExponent(string $type, int $number, string $currency): string
121121
{
122122
$tens = $number % 100;
123123
$singles = $number % 10;
@@ -145,7 +145,7 @@ protected function spellExponent($type, $number, $currency)
145145
return '';
146146
}
147147

148-
protected function getCurrencyName($type, $number, $currency)
148+
protected function getCurrencyName(string $type, int $number, string $currency): string
149149
{
150150
static $names = [
151151
self::CURRENCY_EURO => [

src/languages/Lithuanian.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Lithuanian extends Speller
99
protected $minus = 'minus';
1010
protected $decimalSeparator = ' ir ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $hundreds = [
1515
1 => 'vienas šimtas',
@@ -93,7 +93,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
9393
return $text;
9494
}
9595

96-
protected function spellExponent($type, $number, $currency)
96+
protected function spellExponent(string $type, int $number, string $currency): string
9797
{
9898
$tens = $number % 100;
9999
$singles = $number % 10;
@@ -121,7 +121,7 @@ protected function spellExponent($type, $number, $currency)
121121
return '';
122122
}
123123

124-
protected function getCurrencyName($type, $number, $currency)
124+
protected function getCurrencyName(string $type, int $number, string $currency): string
125125
{
126126
static $names = [
127127
self::CURRENCY_EURO => [

src/languages/Polish.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Polish extends Speller
99
protected $minus = 'minus';
1010
protected $decimalSeparator = ' i ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $hundreds = [
1515
1 => 'sto',
@@ -62,7 +62,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
6262

6363
if ($number < 10)
6464
{
65-
$text .= $this->spellSingle($number, $groupOfThrees, $isDecimalPart, $currency);
65+
$text .= $this->spellSingle($number, $isDecimalPart, $currency);
6666
}
6767
else if (($number > 10) && ($number < 20))
6868
{
@@ -74,14 +74,14 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
7474

7575
if ($number % 10 > 0)
7676
{
77-
$text .= ' ' . $this->spellSingle($number % 10, $groupOfThrees, $isDecimalPart, $currency);
77+
$text .= ' ' . $this->spellSingle($number % 10, $isDecimalPart, $currency);
7878
}
7979
}
8080

8181
return $text;
8282
}
8383

84-
private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
84+
private function spellSingle(int $digit, bool $isDecimalPart, string $currency): string
8585
{
8686
static $singlesMasculine = [
8787
0 => 'zero',
@@ -110,13 +110,13 @@ private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
110110

111111
if ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE)) // russian kopeks
112112
{
113-
return $singlesFeminine[intval($digit)];
113+
return $singlesFeminine[$digit];
114114
}
115115

116-
return $singlesMasculine[intval($digit)];
116+
return $singlesMasculine[$digit];
117117
}
118118

119-
protected function spellExponent($type, $number, $currency)
119+
protected function spellExponent(string $type, int $number, string $currency): string
120120
{
121121
$tens = $number % 100;
122122
$singles = $number % 10;
@@ -156,7 +156,7 @@ protected function spellExponent($type, $number, $currency)
156156
return '';
157157
}
158158

159-
protected function getCurrencyName($type, $number, $currency)
159+
protected function getCurrencyName(string $type, int $number, string $currency): string
160160
{
161161
static $names = [
162162
self::CURRENCY_EURO => [

src/languages/Russian.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Russian extends Speller
99
protected $minus = 'минус';
1010
protected $decimalSeparator = ' и ';
1111

12-
protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
12+
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
1313
{
1414
static $hundreds = [
1515
1 => 'сто',
@@ -81,7 +81,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
8181
return $text;
8282
}
8383

84-
private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
84+
private function spellSingle(int $digit, int $groupOfThrees, bool $isDecimalPart, string $currency): string
8585
{
8686
static $singlesMasculine = [
8787
0 => 'ноль',
@@ -111,13 +111,13 @@ private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
111111
if (($groupOfThrees === 2) // thousands
112112
|| ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE))) // russian kopeks
113113
{
114-
return $singlesFeminine[intval($digit)];
114+
return $singlesFeminine[$digit];
115115
}
116116

117-
return $singlesMasculine[intval($digit)];
117+
return $singlesMasculine[$digit];
118118
}
119119

120-
protected function spellExponent($type, $number, $currency)
120+
protected function spellExponent(string $type, int $number, string $currency): string
121121
{
122122
$tens = $number % 100;
123123
$singles = $number % 10;
@@ -157,7 +157,7 @@ protected function spellExponent($type, $number, $currency)
157157
return '';
158158
}
159159

160-
protected function getCurrencyName($type, $number, $currency)
160+
protected function getCurrencyName(string $type, int $number, string $currency): string
161161
{
162162
static $names = [
163163
self::CURRENCY_EURO => [

0 commit comments

Comments
 (0)