Skip to content

Commit

Permalink
DRY and PHPDoc fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jurchiks authored Oct 1, 2019
1 parent 46d508b commit 0cc2093
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions src/Speller.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function getAcceptedCurrencies()
}

/**
* Convert a number into {whole part spelled} CODE {decimal part}/100 format.
* Convert a number into its linguistic representation.
*
* @param int $number : the number to spell in the specified language
* @param string $language : a two-letter, ISO 639-1 code of the language to spell the number in
Expand All @@ -102,17 +102,15 @@ public static function getAcceptedCurrencies()
*/
public static function spellNumber($number, $language)
{
if (!is_numeric($number))
{
throw new InvalidArgumentException('Invalid number specified.');
}
self::assertNumber($number);

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

/**
* Convert currency to its linguistic short representation.
* Convert currency to its linguistic representation.
* The format is {whole part spelled} CODE {decimal part}/100.
*
* @param int|float $amount : the amount to spell in the specified language
* @param string $language : a two-letter, ISO 639-1 code of the language to spell the amount in
Expand All @@ -122,22 +120,8 @@ public static function spellNumber($number, $language)
*/
public static function spellCurrencyShort($amount, $language, $currency)
{
if (!is_numeric($amount))
{
throw new InvalidArgumentException('Invalid number specified.');
}

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.');
}
self::assertNumber($amount);
self::validateCurrency($currency);

$amount = number_format($amount, 2, '.', ''); // ensure decimal is always 2 digits
$parts = explode('.', $amount);
Expand Down Expand Up @@ -167,22 +151,8 @@ public static function spellCurrencyShort($amount, $language, $currency)
*/
public static function spellCurrency($amount, $language, $currency, $requireDecimal = true, $spellDecimal = false)
{
if (!is_numeric($amount))
{
throw new InvalidArgumentException('Invalid number specified.');
}

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.');
}
self::assertNumber($amount);
self::validateCurrency($currency);

$amount = number_format($amount, 2, '.', ''); // ensure decimal is always 2 digits
$parts = explode('.', $amount);
Expand Down Expand Up @@ -268,4 +238,27 @@ private function parseInt($number, $isDecimalPart, $currency = '')
protected abstract function spellHundred($number, $groupOfThrees, $isDecimalPart, $currency);
protected abstract function spellExponent($type, $number, $currency);
protected abstract function getCurrencyName($type, $number, $currency);

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

private static function validateCurrency(&$currency)
{
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.');
}
}
}

0 comments on commit 0cc2093

Please sign in to comment.