public getPluralForm ( mixed $quantity, |
||
$quantity | mixed | A number to find plural form for (float or int) |
$locale | ||
return | string | One of plural form constants |
public function getPluralForm($quantity, Locale $locale)
{
if (!isset($this->rulesetsIndices[$locale->getLanguage()])) {
return self::RULE_OTHER;
}
$ruleset = $this->rulesets[$locale->getLanguage()][$this->rulesetsIndices[$locale->getLanguage()]];
if ($ruleset === null) {
return self::RULE_OTHER;
}
foreach ($ruleset as $form => $rule) {
foreach ($rule as $subrule) {
$subrulePassed = false;
if ($subrule['modulo'] !== false) {
$quantity = fmod($quantity, $subrule['modulo']);
}
if ($quantity == floor($quantity)) {
$quantity = (int) $quantity;
}
$condition = $subrule['condition'];
switch ($condition[0]) {
case 'is':
case 'isnot':
if (is_int($quantity) && $quantity === $condition[1]) {
$subrulePassed = true;
}
if ($condition[0] === 'isnot') {
$subrulePassed = !$subrulePassed;
}
break;
case 'in':
case 'notin':
if (is_int($quantity) && $quantity >= $condition[1] && $quantity <= $condition[2]) {
$subrulePassed = true;
}
if ($condition[0] === 'notin') {
$subrulePassed = !$subrulePassed;
}
break;
case 'within':
case 'notwithin':
if ($quantity >= $condition[1] && $quantity <= $condition[2]) {
$subrulePassed = true;
}
if ($condition[0] === 'notwithin') {
$subrulePassed = !$subrulePassed;
}
break;
}
if ($subrulePassed && $subrule['logicalOperator'] === 'or' || !$subrulePassed && $subrule['logicalOperator'] === 'and') {
break;
}
}
if ($subrulePassed) {
return $form;
}
}
return self::RULE_OTHER;
}
/** * Get the plural form to be used. * * If $quantity is numeric and non-NULL, the plural form for provided $locale will be * chosen according to it. * * In all other cases, NULL is returned. * * @param mixed $quantity * @param Locale $locale * @return string */ protected function getPluralForm($quantity, Locale $locale) { if (!is_numeric($quantity)) { return null; } else { return $this->pluralsReader->getPluralForm($quantity, $locale); } }