forked from OblikStudio/php-pluralization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguage.php
78 lines (67 loc) · 2.18 KB
/
Language.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Oblik\Pluralization;
abstract class Language
{
/**
* n absolute value of the source number (integer and decimals).
* i integer digits of n.
* v number of visible fraction digits in n, with trailing zeros.
* w number of visible fraction digits in n, without trailing zeros.
* f visible fractional digits in n, with trailing zeros.
* t visible fractional digits in n, without trailing zeros.
* @see http://unicode.org/reports/tr35/tr35-numbers.html#Operands
*/
private static function parseNumber($number) {
if (!is_string($number)) {
$number = (string) $number;
}
$split = explode('.', $number);
$integer = array_shift($split);
$fraction = implode('', $split);
$noTrailFraction = rtrim($fraction, '0');
return [
(float) $number, // n
(int) $integer, // i
strlen($fraction), // v
strlen($noTrailFraction), // w
(int) $fraction, // f
(int) $noTrailFraction, // t
];
}
protected static function inRange(int $n, array $range) {
return $n >= $range[0] && $n <= $range[1];
}
public static function getCardinal($number)
{
return call_user_func_array(
static::class . '::cardinal',
self::parseNumber($number)
);
}
public static function getOrdinal($number)
{
return static::ordinal((int) $number);
}
public static function getRange($start, $end)
{
$startType = static::getCardinal($start);
$endType = static::getCardinal($end);
$rangeType = $startType . $endType;
if (isset(static::RANGE[$rangeType])) {
return static::RANGE[$rangeType];
} else {
return null;
}
}
public static function formName($form) {
switch ($form) {
case ZERO: return 'zero';
case ONE: return 'one';
case TWO: return 'two';
case FEW: return 'few';
case MANY: return 'many';
case OTHER: return 'other';
}
return null;
}
}