1
|
|
|
<?php |
2
|
|
|
namespace morphos\Russian; |
3
|
|
|
|
4
|
|
|
use morphos\Gender; |
5
|
|
|
use morphos\CurrenciesHelper; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
class MoneySpeller extends \morphos\MoneySpeller |
9
|
|
|
{ |
10
|
|
|
use CurrenciesHelper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array[] |
14
|
|
|
* @phpstan-var array<string, string[]> |
15
|
|
|
*/ |
16
|
|
|
protected static $labels = [ |
17
|
|
|
self::DOLLAR => ['доллар', Gender::MALE, 'цент', Gender::MALE], |
18
|
|
|
self::EURO => ['евро', Gender::NEUTER, 'цент', Gender::MALE], |
19
|
|
|
self::YEN => ['иена', Gender::FEMALE, 'сен', Gender::MALE], |
20
|
|
|
self::POUND => ['фунт', Gender::MALE, 'пенни', Gender::NEUTER], |
21
|
|
|
self::FRANC => ['франк', Gender::MALE, 'сантим', Gender::MALE], |
22
|
|
|
self::YUAN => ['юань', Gender::MALE, 'цзяо', Gender::NEUTER], |
23
|
|
|
self::KRONA => ['крона', Gender::FEMALE, 'эре', Gender::NEUTER], |
24
|
|
|
self::PESO => ['песо', Gender::NEUTER, 'сентаво', Gender::NEUTER], |
25
|
|
|
self::WON => ['вон', Gender::MALE, 'чон', Gender::MALE], |
26
|
|
|
self::LIRA => ['лира', Gender::FEMALE, 'куруш', Gender::MALE], |
27
|
|
|
self::RUBLE => ['рубль', Gender::MALE, 'копейка', Gender::FEMALE], |
28
|
|
|
self::RUPEE => ['рупия', Gender::FEMALE, 'пайка', Gender::FEMALE], |
29
|
|
|
self::REAL => ['реал', Gender::MALE, 'сентаво', Gender::NEUTER], |
30
|
|
|
self::RAND => ['рэнд', Gender::MALE, 'цент', Gender::MALE], |
31
|
|
|
self::HRYVNIA => ['гривна', Gender::FEMALE, 'копейка', Gender::FEMALE], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param float|integer $value |
36
|
|
|
* @param string $currency |
37
|
|
|
* @param string $format |
38
|
|
|
* @param string|null $case |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
20 |
|
public static function spell( |
44
|
|
|
$value, |
45
|
|
|
$currency, |
46
|
|
|
$format = self::NORMAL_FORMAT, |
47
|
|
|
$case = null |
48
|
|
|
) |
49
|
|
|
{ |
50
|
20 |
|
$currency = static::canonizeCurrency($currency); |
51
|
|
|
|
52
|
19 |
|
$integer = (int)floor($value); |
53
|
19 |
|
$fractional = fmod($value, $integer); |
54
|
19 |
|
$fractional = round($fractional, 2); |
55
|
19 |
|
$fractional = (int)($fractional * 100); |
56
|
|
|
|
57
|
|
|
switch ($format) { |
58
|
19 |
|
case static::SHORT_FORMAT: |
59
|
14 |
|
return $integer.' '.NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case) |
60
|
14 |
|
.($fractional > 0 |
61
|
6 |
|
? ' '.$fractional.' '.NounPluralization::pluralize(static::$labels[$currency][2], |
62
|
6 |
|
$fractional, false, $case) |
63
|
14 |
|
: null); |
64
|
|
|
|
65
|
5 |
|
case static::NORMAL_FORMAT: |
66
|
3 |
|
case static::CLARIFICATION_FORMAT: |
67
|
2 |
|
case static::DUPLICATION_FORMAT: |
68
|
|
|
|
69
|
5 |
|
$integer_spelled = CardinalNumeralGenerator::getCase( |
70
|
5 |
|
$integer, |
71
|
5 |
|
$case !== null ? $case : Cases::IMENIT, |
72
|
5 |
|
static::$labels[$currency][1]); |
73
|
5 |
|
$fractional_spelled = CardinalNumeralGenerator::getCase( |
74
|
5 |
|
$fractional, |
75
|
5 |
|
$case !== null ? $case : Cases::IMENIT, |
76
|
5 |
|
static::$labels[$currency][3]); |
77
|
|
|
|
78
|
5 |
|
if ($format == static::CLARIFICATION_FORMAT) { |
79
|
1 |
|
return $integer.' ('.$integer_spelled.') ' |
80
|
1 |
|
.NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case).' ' |
81
|
1 |
|
.$fractional.' ('.$fractional_spelled.') ' |
82
|
1 |
|
.NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case); |
83
|
|
|
} else { |
84
|
4 |
|
return $integer_spelled.($format == static::DUPLICATION_FORMAT ? ' ('.$integer.')' : null) |
85
|
4 |
|
.' '.NounPluralization::pluralize(static::$labels[$currency][0], $integer, false, $case).' ' |
86
|
4 |
|
.$fractional_spelled.($format == static::DUPLICATION_FORMAT ? ' ('.$fractional.')' : null).' ' |
87
|
4 |
|
.NounPluralization::pluralize(static::$labels[$currency][2], $fractional, false, $case); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new RuntimeException('Unreachable'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|