1
|
|
|
<?php |
2
|
|
|
namespace morphos\English; |
3
|
|
|
|
4
|
|
|
use morphos\NumeralGenerator; |
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
class CardinalNumeralGenerator extends NumeralGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string[] |
11
|
|
|
* @phpstan-var array<int, string> |
12
|
|
|
*/ |
13
|
|
|
public static $words = [ |
14
|
|
|
1 => 'one', |
15
|
|
|
2 => 'two', |
16
|
|
|
3 => 'three', |
17
|
|
|
4 => 'four', |
18
|
|
|
5 => 'five', |
19
|
|
|
6 => 'six', |
20
|
|
|
7 => 'seven', |
21
|
|
|
8 => 'eight', |
22
|
|
|
9 => 'nine', |
23
|
|
|
10 => 'ten', |
24
|
|
|
11 => 'eleven', |
25
|
|
|
12 => 'twelve', |
26
|
|
|
13 => 'thirteen', |
27
|
|
|
14 => 'fourteen', |
28
|
|
|
15 => 'fifteen', |
29
|
|
|
16 => 'sixteen', |
30
|
|
|
17 => 'seventeen', |
31
|
|
|
18 => 'eighteen', |
32
|
|
|
19 => 'nineteen', |
33
|
|
|
20 => 'twenty', |
34
|
|
|
30 => 'thirty', |
35
|
|
|
40 => 'forty', |
36
|
|
|
50 => 'fifty', |
37
|
|
|
60 => 'sixty', |
38
|
|
|
70 => 'seventy', |
39
|
|
|
80 => 'eighty', |
40
|
|
|
90 => 'ninety', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string[] |
45
|
|
|
* @phpstan-var array<int, string> |
46
|
|
|
*/ |
47
|
|
|
public static $exponents = [ |
48
|
|
|
100 => 'hundred', |
49
|
|
|
1000 => 'thousand', |
50
|
|
|
1000000 => 'million', |
51
|
|
|
1000000000 => 'billion', |
52
|
|
|
1000000000000 => 'trillion', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
public static function getCases($number) |
56
|
|
|
{ |
57
|
|
|
throw new RuntimeException('Not implemented'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getCase($number, $case) |
61
|
|
|
{ |
62
|
|
|
throw new RuntimeException('Not implemented'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $number |
67
|
|
|
* @return mixed|string |
68
|
|
|
*/ |
69
|
6 |
|
public static function generate($number) |
70
|
|
|
{ |
71
|
|
|
// simple numeral |
72
|
6 |
|
if (isset(static::$words[$number]) || isset(static::$exponents[$number])) { |
73
|
6 |
|
return isset(static::$words[$number]) ? static::$words[$number] : static::$exponents[$number]; |
74
|
|
|
} |
75
|
|
|
// compound numeral |
76
|
|
|
else { |
77
|
5 |
|
$parts = []; |
78
|
5 |
|
$words = []; |
79
|
|
|
|
80
|
5 |
View Code Duplication |
foreach (array_reverse(static::$exponents, true) as $word_number => $word) { |
|
|
|
|
81
|
5 |
|
if ($number >= $word_number) { |
82
|
5 |
|
$count = (int)floor($number / $word_number); |
83
|
5 |
|
$number = $number % ($count * $word_number); |
84
|
5 |
|
$parts[] = static::generate($count).' '.static::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
View Code Duplication |
foreach (array_reverse(static::$words, true) as $word_number => $word) { |
|
|
|
|
89
|
5 |
|
if ($number >= $word_number) { |
90
|
5 |
|
$words[] = static::generate($word_number); |
91
|
5 |
|
$number %= $word_number; |
92
|
|
|
} |
93
|
|
|
} |
94
|
5 |
|
$parts[] = implode('-', $words); |
95
|
|
|
|
96
|
5 |
|
return implode(' ', $parts); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.