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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
<?php
namespace Punic;
class Plural
{
public static function getRules($locale = '')
{
$node = Data::getLanguageNode(Data::getGeneric('plurals'), $locale);
return array_merge(
array_keys($node),
array('other')
);
}
public static function getRule($number, $locale = '')
{
if (is_int($number)) {
$intPartAbs = (string) abs($number);
$floatPart = '';
} elseif (is_float($number)) {
$s = (string) $number;
if (strpos($s, '.') === false) {
$intPart = $s;
$floatPart = '';
} else {
list($intPart, $floatPart) = explode('.', $s);
}
$intPartAbs = (string) abs((int) $intPart);
} elseif (is_string($number) && $number !== '') {
if (preg_match('/^[+|\\-]?\\d+\\.?$/', $number)) {
$v = (int) $number;
$intPartAbs = (string) abs($v);
$floatPart = '';
} elseif (preg_match('/^(\\d*)\\.(\\d+)$/', $number, $m)) {
list($intPart, $floatPart) = explode('.', $number);
$v = @(int) $intPart;
$intPartAbs = (string) abs($v);
} else {
throw new Exception\BadArgumentType($number, 'number');
}
} else {
throw new Exception\BadArgumentType($number, 'number');
}
$v1 = $intPartAbs.(strlen($floatPart) ? ".$floatPart" : '');
$v2 = $intPartAbs;
$v3 = strlen($floatPart);
$v4 = strlen(rtrim($floatPart, '0'));
$v5 = strlen($floatPart) ? (string) ((int) $floatPart) : '0';
$v6 = trim($floatPart, '0');
if ($v6 === '') {
$v6 = '0';
}
$result = 'other';
$node = Data::getLanguageNode(Data::getGeneric('plurals'), $locale);
foreach ($node as $rule => $formulaPattern) {
$formula = sprintf($formulaPattern, $v1, $v2, $v3, $v4, $v5, $v6);
$check = str_replace(array('static::inRange(', ' and ', ' or ', ', false, ', ', true, ', ', array('), ' , ', $formula);
if (preg_match('/[a-z]/', $check)) {
throw new \Exception('Bad formula!');
}
while (preg_match('/(\\d+\\.\\d+) % (\\d+(\\.\\d+)?)/', $formula, $m)) {
list(, $decimalPart) = explode('.', $m[1], 2);
$decimals = strlen(rtrim($decimalPart, '0'));
if ($decimals > 0) {
$pow = (int) pow(10, $decimals);
$repl = '('.(string) ((int) ((float) $m[1] * $pow)).' % '.(string) ((int) ((float) ($m[2] * $pow))).') / '.$pow;
} else {
$repl = (string) ((int) $m[1]).' % '.$m[2];
}
$formula = str_replace($m[0], $repl, $formula);
}
$formulaResult = @eval("return ($formula) ? 'yes' : 'no';");
if ($formulaResult === 'yes') {
$result = $rule;
break;
} elseif ($formulaResult !== 'no') {
throw new \Exception('There was a problem in the formula '.$formulaPattern);
}
}
return $result;
}
protected static function inRange($value, $mustBeIncluded)
{
if (is_int($value)) {
$isInt = true;
} elseif ((int) $value == $value) {
$isInt = true;
} else {
$isInt = false;
}
$rangeValues = (func_num_args() > 2) ? array_slice(func_get_args(), 2) : array();
$included = false;
foreach ($rangeValues as $rangeValue) {
if (is_array($rangeValue)) {
if ($isInt && ($value >= $rangeValue[0]) && ($value <= $rangeValue[1])) {
$included = true;
break;
}
} elseif ($value == $rangeValue) {
$included = true;
break;
}
}
return $included == $mustBeIncluded;
}
}