1
|
|
|
<?php |
2
|
|
|
namespace morphos\English; |
3
|
|
|
|
4
|
|
|
use morphos\NumeralCreation; |
5
|
|
|
|
6
|
|
|
class OrdinalNumeral extends NumeralCreation { |
7
|
|
|
|
8
|
|
|
static protected $words = array( |
9
|
|
|
1 => 'first', |
10
|
|
|
2 => 'second', |
11
|
|
|
3 => 'third', |
12
|
|
|
5 => 'fifth', |
13
|
|
|
8 => 'eighth', |
14
|
|
|
9 => 'ninth', |
15
|
|
|
12 => 'twelfth', |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
static public function getCases($number) {} |
19
|
|
|
static public function getCase($number, $case) {} |
20
|
|
|
|
21
|
|
|
static public function generate($number, $short = false) { |
22
|
|
|
// simple numeral |
23
|
|
|
if (isset(self::$words[$number])) { |
24
|
|
|
return !$short ? self::$words[$number] : $number.substr(self::$words[$number], -2); |
25
|
|
|
} else if (isset(CardinalNumeral::$words[$number]) || isset(CardinalNumeral::$exponents[$number])) { |
26
|
|
|
if ($short) return $number.'th'; |
27
|
|
|
$word = isset(CardinalNumeral::$words[$number]) ? CardinalNumeral::$words[$number] : CardinalNumeral::$exponents[$number]; |
28
|
|
|
if (substr($word, -1) == 'y') $word = substr($word, 0, -1).'ie'; |
29
|
|
|
$word .= 'th'; |
30
|
|
|
return $word; |
31
|
|
|
} |
32
|
|
|
// compound numeral |
33
|
|
|
else { |
34
|
|
|
$parts = array(); |
35
|
|
|
$words = array(); |
36
|
|
|
|
37
|
|
|
$original_number = $number; |
38
|
|
|
|
39
|
|
View Code Duplication |
foreach (array_reverse(CardinalNumeral::$exponents, true) as $word_number => $word) { |
|
|
|
|
40
|
|
|
if ($number >= $word_number) { |
41
|
|
|
$count = floor($number / $word_number); |
42
|
|
|
$number = $number % ($count * $word_number); |
43
|
|
|
$parts[] = CardinalNumeral::generate($count).' '.CardinalNumeral::generate($word_number).($word_number != 100 && $number > 0 ? ',' : null); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach (array_reverse(CardinalNumeral::$words, true) as $word_number => $word) { |
48
|
|
|
if ($number >= $word_number) { |
49
|
|
|
// if last part |
50
|
|
|
if ($number % $word_number === 0) |
51
|
|
|
$words[] = self::generate($word_number); |
52
|
|
|
else |
53
|
|
|
$words[] = CardinalNumeral::generate($word_number); |
54
|
|
|
$number %= $word_number; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
$parts[] = implode('-', $words); |
58
|
|
|
|
59
|
|
|
if ($short) return $original_number.substr(array_pop($words), -2); |
60
|
|
|
|
61
|
|
|
return implode(' ', $parts); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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.