diff --git a/README-ru.md b/README-ru.md index a870f6b..e3ad9b1 100644 --- a/README-ru.md +++ b/README-ru.md @@ -245,6 +245,14 @@ MoneySpeller::spell(123.45, MoneySpeller::RUBLE, MoneySpeller::CLARIFICATION_FOR При указании валюты используйте знак валюты (например, ₽) или трехзначный код валюты (`\morphos\Currency::RUBLE`). +Также можно указать падеж для склонения четвёртым параметром: + +```php +use morphos\Russian\MoneySpeller; + +MoneySpeller::spell(123.45, MoneySpeller::RUBLE, MoneySpeller::NORMAL_FORMAT, 'родительный') => 'ста двадцати трех рублей сорока пяти копеек' +``` + Доступные валюты: | Знак | Идентификатор | Валюта | diff --git a/src/MoneySpeller.php b/src/MoneySpeller.php index bc892bb..724b5b2 100644 --- a/src/MoneySpeller.php +++ b/src/MoneySpeller.php @@ -10,9 +10,16 @@ abstract class MoneySpeller implements Currency /** * @abstract - * @param float $value + * + * @param float $value * @param string $currency * @param string $format + * @param null $case */ - public static function spell($value, $currency, $format = self::NORMAL_FORMAT) {} + public static function spell( + $value, + $currency, + $format = self::NORMAL_FORMAT, + $case = null + ) {} } diff --git a/src/Russian/MoneySpeller.php b/src/Russian/MoneySpeller.php index f4d529a..3795c25 100644 --- a/src/Russian/MoneySpeller.php +++ b/src/Russian/MoneySpeller.php @@ -28,13 +28,19 @@ class MoneySpeller extends \morphos\MoneySpeller /** * @param float|integer $value - * @param string $currency - * @param string $format + * @param string $currency + * @param string $format + * @param null $case + * * @return string - * @throws \InvalidArgumentException * @throws \Exception */ - public static function spell($value, $currency, $format = self::NORMAL_FORMAT) + public static function spell( + $value, + $currency, + $format = self::NORMAL_FORMAT, + $case = null + ) { $currency = static::canonizeCurrency($currency); @@ -43,22 +49,35 @@ public static function spell($value, $currency, $format = self::NORMAL_FORMAT) switch ($format) { case static::SHORT_FORMAT: - return $integer.' '.NounPluralization::pluralize(static::$labels[$currency][0], $integer) + return $integer.' '.NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case) .($fractional > 0 - ? ' '.$fractional.' '.NounPluralization::pluralize(static::$labels[$currency][2], $fractional) + ? ' '.$fractional.' '.NounPluralization::pluralize(static::$labels[$currency][2], + $fractional, false, $case) : null); case static::NORMAL_FORMAT: case static::CLARIFICATION_FORMAT: case static::DUPLICATION_FORMAT: - $integer_speelled = CardinalNumeralGenerator::getCase($integer, Cases::IMENIT, static::$labels[$currency][1]); - $fractional_speelled = CardinalNumeralGenerator::getCase($fractional, Cases::IMENIT, static::$labels[$currency][3]); + $integer_spelled = CardinalNumeralGenerator::getCase( + $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 ($format == static::CLARIFICATION_FORMAT) { - return $integer.' ('.$integer_speelled.') '.NounPluralization::pluralize(static::$labels[$currency][0], $integer).' '.$fractional.' ('.$fractional_speelled.') '.NounPluralization::pluralize(static::$labels[$currency][2], $fractional); + 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_speelled.($format == static::DUPLICATION_FORMAT ? ' ('.$integer.')' : null).' '.NounPluralization::pluralize(static::$labels[$currency][0], $integer).' '.$fractional_speelled.($format == static::DUPLICATION_FORMAT ? ' ('.$fractional.')' : null).' '.NounPluralization::pluralize(static::$labels[$currency][2], $fractional); + 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); } } } diff --git a/tests/Russian/MoneySpellerTest.php b/tests/Russian/MoneySpellerTest.php index 23535c2..d228cb7 100644 --- a/tests/Russian/MoneySpellerTest.php +++ b/tests/Russian/MoneySpellerTest.php @@ -1,19 +1,34 @@ assertEquals($result, MoneySpeller::spell($value, $currency, $format)); + $this->assertEquals($result, MoneySpeller::spell($value, $currency, $format, $case)); } public function moneyAmountsProvider() @@ -22,6 +37,7 @@ public function moneyAmountsProvider() [ [123.45, Currency::RUBLE, MoneySpeller::SHORT_FORMAT, '123 рубля 45 копеек'], [321.54, Currency::DOLLAR, MoneySpeller::NORMAL_FORMAT, 'триста двадцать один доллар пятьдесят четыре цента'], + [321.54, Currency::DOLLAR, MoneySpeller::NORMAL_FORMAT, 'трехсот двадцати одного доллара пятидесяти четырех центов', Cases::RODIT], [369.12, Currency::EURO, MoneySpeller::DUPLICATION_FORMAT, 'триста шестьдесят девять (369) евро двенадцать (12) центов'], [246.35, Currency::YEN, MoneySpeller::CLARIFICATION_FORMAT, '246 (двести сорок шесть) иен 35 (тридцать пять) сенов'], [123, Currency::POUND, MoneySpeller::SHORT_FORMAT, '123 фунта'], @@ -37,6 +53,7 @@ public function moneyAmountsProvider() /** * @expectedException InvalidArgumentException + * @throws \Exception */ public function testSpellInvalid() {