Skip to content

Commit

Permalink
#64. Added ability to pass $case to MoneySpeller::spell()
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Dec 29, 2019
1 parent d8ee7db commit 30b4202
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
8 changes: 8 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, 'родительный') => 'ста двадцати трех рублей сорока пяти копеек'
```

Доступные валюты:

| Знак | Идентификатор | Валюта |
Expand Down
11 changes: 9 additions & 2 deletions src/MoneySpeller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {}
}
39 changes: 29 additions & 10 deletions src/Russian/MoneySpeller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions tests/Russian/MoneySpellerTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
<?php
namespace morphos\test\Russian;


use morphos\Gender;
use morphos\Currency;
use morphos\Russian\Cases;
use morphos\Russian\MoneySpeller;

class MoneySpellerTest extends \PHPUnit_Framework_TestCase
{

/**
* @dataProvider moneyAmountsProvider()
*
* @param $value
* @param $currency
* @param $format
* @param $result
*
* @param null $case
*
* @throws \Exception
*/
public function testSpell($value, $currency, $format, $result)
public function testSpell(
$value,
$currency,
$format,
$result,
$case = null
)
{
$this->assertEquals($result, MoneySpeller::spell($value, $currency, $format));
$this->assertEquals($result, MoneySpeller::spell($value, $currency, $format, $case));
}

public function moneyAmountsProvider()
Expand All @@ -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 фунта'],
Expand All @@ -37,6 +53,7 @@ public function moneyAmountsProvider()

/**
* @expectedException InvalidArgumentException
* @throws \Exception
*/
public function testSpellInvalid()
{
Expand Down

0 comments on commit 30b4202

Please sign in to comment.