Skip to content

Commit

Permalink
Merge pull request #12 from jurchiks/upgrade-to-php7
Browse files Browse the repository at this point in the history
Upgrade to php 7
  • Loading branch information
jurchiks authored Nov 23, 2020
2 parents 86c01bd + 21d1ec6 commit c25ccf6
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"email": "jurchiks101@gmail.com"
}
],
"require": {
"php": ">=7.1"
},
"autoload": {
"psr-4": {
"js\\tools\\numbers2words\\": "src/"
Expand Down
42 changes: 13 additions & 29 deletions src/Speller.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,10 @@ private final function __construct()
* @param string $language A two-letter, ISO 639-1 code of the language.
* @return Speller
*/
private static function get($language)
private static function get(string $language): Speller
{
static $spellers = [];

$language = strtolower(trim($language));

if (strlen($language) != 2)
{
throw new InvalidArgumentException('Invalid language code specified, must follow ISO 639-1 format.');
}

if (!isset(self::$languages[$language]))
{
throw new InvalidArgumentException('That language is not implemented yet.');
Expand All @@ -81,12 +74,12 @@ private static function get($language)
return $spellers[$language];
}

public static function getAcceptedLanguages()
public static function getAcceptedLanguages(): array
{
return array_keys(self::$languages);
}

public static function getAcceptedCurrencies()
public static function getAcceptedCurrencies(): array
{
return self::$currencies;
}
Expand All @@ -99,12 +92,10 @@ public static function getAcceptedCurrencies()
* @return string The number as written in words in the specified language.
* @throws InvalidArgumentException If any parameter is invalid.
*/
public static function spellNumber($number, $language)
public static function spellNumber(int $number, string $language): string
{
self::assertNumber($number);

return self::get($language)
->parseInt(intval($number), false);
->parseInt($number, false);
}

/**
Expand All @@ -117,7 +108,7 @@ public static function spellNumber($number, $language)
* @return string The currency as written in words in the specified language.
* @throws InvalidArgumentException If any parameter is invalid.
*/
public static function spellCurrencyShort($amount, $language, $currency)
public static function spellCurrencyShort($amount, string $language, string $currency): string
{
self::assertNumber($amount);
self::validateCurrency($currency);
Expand Down Expand Up @@ -148,7 +139,7 @@ public static function spellCurrencyShort($amount, $language, $currency)
* @return string The currency as written in words in the specified language.
* @throws InvalidArgumentException If any parameter is invalid.
*/
public static function spellCurrency($amount, $language, $currency, $requireDecimal = true, $spellDecimal = false)
public static function spellCurrency($amount, string $language, string $currency, bool $requireDecimal = true, bool $spellDecimal = false): string
{
self::assertNumber($amount);
self::validateCurrency($currency);
Expand Down Expand Up @@ -176,7 +167,7 @@ public static function spellCurrency($amount, $language, $currency, $requireDeci
return $text;
}

private function parseInt($number, $isDecimalPart, $currency = '')
private function parseInt(int $number, bool $isDecimalPart, string $currency = ''): string
{
$text = '';

Expand Down Expand Up @@ -234,29 +225,22 @@ private function parseInt($number, $isDecimalPart, $currency = '')
return $text;
}

protected abstract function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency);
protected abstract function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string;

protected abstract function spellExponent($type, $number, $currency);
protected abstract function spellExponent(string $type, int $number, string $currency): string;

protected abstract function getCurrencyName($type, $number, $currency);
protected abstract function getCurrencyName(string $type, int $number, string $currency): string;

private static function assertNumber($number)
private static function assertNumber($number): void
{
if (!is_numeric($number))
{
throw new InvalidArgumentException('Invalid number specified.');
}
}

private static function validateCurrency(&$currency)
private static function validateCurrency(string $currency): void
{
if (!is_string($currency))
{
throw new InvalidArgumentException('Invalid currency code specified.');
}

$currency = strtoupper(trim($currency));

if (!in_array($currency, self::$currencies))
{
throw new InvalidArgumentException('That currency is not implemented yet.');
Expand Down
12 changes: 6 additions & 6 deletions src/languages/English.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class English extends Speller
protected $minus = 'minus';
protected $decimalSeparator = ' and ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $tens = [
1 => 'ten',
Expand Down Expand Up @@ -63,17 +63,17 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren

if ($number < 10)
{
$text .= $singles[intval($number)];
$text .= $singles[$number];
}
else if (($number > 10) && ($number < 20))
{
$text .= $teens[intval($number)];
$text .= $teens[$number];
}
else
{
$text .= $tens[intval(substr("$number", 0, 1))];

if ($number % 10 > 0) // twenty five
if ($number % 10 > 0)
{
$text .= ' ' . $singles[$number % 10];
}
Expand All @@ -82,7 +82,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
return $text;
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
if ($type === 'million')
{
Expand All @@ -97,7 +97,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
6 changes: 3 additions & 3 deletions src/languages/Estonian.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Estonian extends Speller
protected $minus = 'miinus';
protected $decimalSeparator = ' ja ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $tens = [
1 => 'kümme',
Expand Down Expand Up @@ -82,7 +82,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
return $text;
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
if ($type === 'million')
{
Expand All @@ -102,7 +102,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
12 changes: 6 additions & 6 deletions src/languages/Latvian.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Latvian extends Speller
protected $minus = 'mīnus';
protected $decimalSeparator = ' un ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $hundreds = [
1 => 'viens simts',
Expand Down Expand Up @@ -81,7 +81,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
return $text;
}

private function spellSingle($digit, $isDecimalPart, $currency)
private function spellSingle(int $digit, bool $isDecimalPart, string $currency): string
{
static $singlesMasculine = [
0 => 'nulle',
Expand Down Expand Up @@ -111,13 +111,13 @@ private function spellSingle($digit, $isDecimalPart, $currency)
if ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE))
{
// russian kopek nouns are feminine gender in Latvian
return $singlesFeminine[intval($digit)];
return $singlesFeminine[$digit];
}

return $singlesMasculine[intval($digit)];
return $singlesMasculine[$digit];
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
$tens = $number % 100;
$singles = $number % 10;
Expand Down Expand Up @@ -145,7 +145,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
6 changes: 3 additions & 3 deletions src/languages/Lithuanian.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Lithuanian extends Speller
protected $minus = 'minus';
protected $decimalSeparator = ' ir ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $hundreds = [
1 => 'vienas šimtas',
Expand Down Expand Up @@ -93,7 +93,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
return $text;
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
$tens = $number % 100;
$singles = $number % 10;
Expand Down Expand Up @@ -121,7 +121,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
16 changes: 8 additions & 8 deletions src/languages/Polish.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Polish extends Speller
protected $minus = 'minus';
protected $decimalSeparator = ' i ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $hundreds = [
1 => 'sto',
Expand Down Expand Up @@ -62,7 +62,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren

if ($number < 10)
{
$text .= $this->spellSingle($number, $groupOfThrees, $isDecimalPart, $currency);
$text .= $this->spellSingle($number, $isDecimalPart, $currency);
}
else if (($number > 10) && ($number < 20))
{
Expand All @@ -74,14 +74,14 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren

if ($number % 10 > 0)
{
$text .= ' ' . $this->spellSingle($number % 10, $groupOfThrees, $isDecimalPart, $currency);
$text .= ' ' . $this->spellSingle($number % 10, $isDecimalPart, $currency);
}
}

return $text;
}

private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
private function spellSingle(int $digit, bool $isDecimalPart, string $currency): string
{
static $singlesMasculine = [
0 => 'zero',
Expand Down Expand Up @@ -110,13 +110,13 @@ private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)

if ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE)) // russian kopeks
{
return $singlesFeminine[intval($digit)];
return $singlesFeminine[$digit];
}

return $singlesMasculine[intval($digit)];
return $singlesMasculine[$digit];
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
$tens = $number % 100;
$singles = $number % 10;
Expand Down Expand Up @@ -156,7 +156,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
12 changes: 6 additions & 6 deletions src/languages/Russian.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class Russian extends Speller
protected $minus = 'минус';
protected $decimalSeparator = ' и ';

protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency)
protected function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $hundreds = [
1 => 'сто',
Expand Down Expand Up @@ -81,7 +81,7 @@ protected function spellHundred($number, $groupOfThrees, $isDecimalPart, $curren
return $text;
}

private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
private function spellSingle(int $digit, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $singlesMasculine = [
0 => 'ноль',
Expand Down Expand Up @@ -111,13 +111,13 @@ private function spellSingle($digit, $groupOfThrees, $isDecimalPart, $currency)
if (($groupOfThrees === 2) // thousands
|| ($isDecimalPart && ($currency === self::CURRENCY_RUSSIAN_ROUBLE))) // russian kopeks
{
return $singlesFeminine[intval($digit)];
return $singlesFeminine[$digit];
}

return $singlesMasculine[intval($digit)];
return $singlesMasculine[$digit];
}

protected function spellExponent($type, $number, $currency)
protected function spellExponent(string $type, int $number, string $currency): string
{
$tens = $number % 100;
$singles = $number % 10;
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function spellExponent($type, $number, $currency)
return '';
}

protected function getCurrencyName($type, $number, $currency)
protected function getCurrencyName(string $type, int $number, string $currency): string
{
static $names = [
self::CURRENCY_EURO => [
Expand Down
Loading

0 comments on commit c25ccf6

Please sign in to comment.