ICanBoogie/CLDR 1.9.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • CLDR
      • Plurals
      • Units

Classes

  • AbstractCollection
  • AbstractSectionCollection
  • Calendar
  • CalendarCollection
  • ContextTransforms
  • Currency
  • CurrencyCollection
  • CurrencyFormatter
  • DateFormatter
  • DateTimeAccessor
  • DateTimeFormatter
  • FileProvider
  • ListFormatter
  • Locale
  • LocaleCollection
  • LocalizedCurrency
  • LocalizedCurrencyFormatter
  • LocalizedDateTime
  • LocalizedListFormatter
  • LocalizedLocale
  • LocalizedNumberFormatter
  • LocalizedObject
  • LocalizedObjectWithFormatter
  • LocalizedTerritory
  • Number
  • NumberFormatter
  • NumberPattern
  • NumberPatternParser
  • Numbers
  • Plurals
  • ProviderCollection
  • RedisProvider
  • Repository
  • RunTimeProvider
  • Supplemental
  • Territory
  • TerritoryCollection
  • TimeFormatter
  • Units
  • WebProvider

Interfaces

  • Exception
  • Formatter
  • LocalizeAwareInterface
  • Provider

Traits

  • CodePropertyTrait
  • CollectionTrait
  • LocalePropertyTrait
  • LocalizeTrait
  • ProviderStorageBinding
  • RepositoryPropertyTrait

Exceptions

  • ResourceNotFound
  • TerritoryNotDefined
  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 147 148 149 150 151 152 153 154 155 156 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\CLDR;

use ICanBoogie\CLDR\Plurals\Rule;
use ICanBoogie\CLDR\Plurals\Samples;

/**
 * Representation of plurals
 */
class Plurals extends \ArrayObject
{
    const COUNT_ZERO = 'zero';
    const COUNT_ONE = 'one';
    const COUNT_TWO = 'two';
    const COUNT_FEW = 'few';
    const COUNT_MANY = 'many';
    const COUNT_OTHER = 'other';

    const RULE_COUNT_PREFIX = 'pluralRule-count-';

    /**
     * @var Rule[][]
     */
    private $rules = [];

    /**
     * @var Samples[][]
     */
    private $samples = [];

    /**
     * @param number $number
     * @param string $locale
     *
     * @return string One of `COUNT_*`.
     */
    public function rule_for($number, $locale)
    {
        foreach ($this->rule_instances_for($locale) as $count => $rule)
        {
            if ($rule->validate($number))
            {
                return $count;
            }
        }

        return self::COUNT_OTHER; // @codeCoverageIgnore
    }

    /**
     * @param string $locale
     *
     * @return string[]
     */
    public function rules_for($locale)
    {
        return array_keys($this->rule_instances_for($locale));
    }

    /**
     * @param string $locale
     *
     * @return Samples[]
     */
    public function samples_for($locale)
    {
        $samples = &$this->samples[$locale];

        return $samples ?: $samples = $this->create_samples_for($locale);
    }

    /**
     * @param string $locale
     *
     * @return Rule[]
     */
    private function rule_instances_for($locale)
    {
        $rules = &$this->rules[$locale];

        return $rules ?: $rules = $this->create_rules_for($locale);
    }

    /**
     * @param string $locale
     *
     * @return Rule[]
     */
    private function create_rules_for($locale)
    {
        $rules = [];
        $prefix_length = strlen(self::RULE_COUNT_PREFIX);

        foreach ($this[$locale] as $count => $rule_string)
        {
            $count = substr($count, $prefix_length);
            $rules[$count] = Rule::from($this->extract_rule($rule_string));
        }

        return $rules;
    }

    /**
     * @param string $rule_string
     *
     * @return string
     */
    private function extract_rule($rule_string)
    {
        $rule = explode('@', $rule_string, 2);
        $rule = array_shift($rule);
        $rule = trim($rule);

        return $rule;
    }

    /**
     * @param string $locale
     *
     * @return Samples[]
     */
    private function create_samples_for($locale)
    {
        $samples = [];
        $prefix_length = strlen(self::RULE_COUNT_PREFIX);

        foreach ($this[$locale] as $count => $rule_string)
        {
            $count = substr($count, $prefix_length);
            $samples[$count] = Samples::from($this->extract_samples($rule_string));
        }

        return $samples;
    }

    /**
     * @param string $rule_string
     *
     * @return string
     */
    private function extract_samples($rule_string)
    {
        return substr($rule_string, strpos($rule_string, '@'));
    }
}
ICanBoogie/CLDR 1.9.x API documentation generated by ApiGen