Skip to content

Commit

Permalink
#134 Add skipFractionalPartIfZero param to MoneySpeller to skip fra…
Browse files Browse the repository at this point in the history
…ctional part when it's not required
  • Loading branch information
wapmorgan committed Aug 6, 2023
1 parent d401c6c commit ef87c75
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/MoneySpeller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public static function spell(
$value,
$currency,
$format = self::NORMAL_FORMAT,
$case = null
$case = null,
$skipFractionalPartIfZero = null
) {
throw new RuntimeException('Not implemented');
}
Expand Down
46 changes: 30 additions & 16 deletions src/Russian/MoneySpeller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ class MoneySpeller extends \morphos\MoneySpeller
* @param string $currency
* @param string $format
* @param string|null $case
*
* @param boolean|null $skipFractionalPartIfZero If null, default behaviour: short can skip fractional part if zero, others always have.
* If true, fractional will be skipped if zero.
* If false, fractional will be kept anyway.
* @return string
* @throws \Exception
*/
public static function spell(
$value,
$currency,
$format = self::NORMAL_FORMAT,
$case = null
$case = null,
$skipFractionalPartIfZero = null
) {
$currency = CurrenciesHelper::canonizeCurrency($currency);

Expand All @@ -56,7 +59,7 @@ public static function spell(
case static::SHORT_FORMAT:
return $integer . ' ' . NounPluralization::pluralize(static::$labels[$currency][0], $integer, false,
$case)
. ($fractional > 0
. ($fractional > 0 || $skipFractionalPartIfZero === false
? ' ' . $fractional . ' ' . NounPluralization::pluralize(static::$labels[$currency][2],
$fractional, false, $case)
: null);
Expand All @@ -69,23 +72,34 @@ public static function spell(
$integer,
$case !== null ? $case : Cases::IMENIT,
static::$labels[$currency][1]);
$fractional_spelled = CardinalNumeralGenerator::getCase(
$fractional,
$case !== null ? $case : Cases::IMENIT,
static::$labels[$currency][3]);

if ($fractional > 0 || (in_array($skipFractionalPartIfZero, [false, null], true))) {
$fractional_spelled = CardinalNumeralGenerator::getCase(
$fractional,
$case !== null ? $case : Cases::IMENIT,
static::$labels[$currency][3]
);
}

if ($format == static::CLARIFICATION_FORMAT) {
return $integer . ' (' . $integer_spelled . ') '
. NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case) . ' '
. $fractional . ' (' . $fractional_spelled . ') '
. NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case);
} else {
return $integer_spelled . ($format == static::DUPLICATION_FORMAT ? ' (' . $integer . ')' : null)
. ' ' . NounPluralization::pluralize(static::$labels[$currency][0], $integer, false,
$case) . ' '
. $fractional_spelled . ($format == static::DUPLICATION_FORMAT ? ' (' . $fractional . ')' : null) . ' '
. NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case);
. NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case)
. (isset($fractional_spelled)
? ' ' . $fractional . ' (' . $fractional_spelled . ') '
. NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case)
: null)
;
}

return $integer_spelled . ($format == static::DUPLICATION_FORMAT ? ' (' . $integer . ')' : null)
. ' ' . NounPluralization::pluralize(static::$labels[$currency][0], $integer, false,
$case)
. (isset($fractional_spelled)
? ' '
. $fractional_spelled . ($format == static::DUPLICATION_FORMAT ? ' (' . $fractional . ')' : null) . ' '
. NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case)
: null)
;
}

throw new RuntimeException('Unreachable');
Expand Down

0 comments on commit ef87c75

Please sign in to comment.