ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Gettext\Languages\Category Class Reference

A helper class that handles a single category rules (eg 'zero', 'one', ...) and its formula and examples. More...

+ Collaboration diagram for Gettext\Languages\Category:

Public Member Functions

 __construct ($cldrCategoryId, $cldrFormulaAndExamples)
 Initialize the instance and parse the formula. More...
 
 getExampleIntegers ()
 Return a list of numbers corresponding to the $examples value. More...
 

Static Public Member Functions

static expandExamples ($examples)
 Expand a list of examples as defined by CLDR. More...
 

Data Fields

 $id
 
 $formula
 
 $examples
 

Detailed Description

A helper class that handles a single category rules (eg 'zero', 'one', ...) and its formula and examples.

Definition at line 9 of file Category.php.

Constructor & Destructor Documentation

◆ __construct()

Gettext\Languages\Category::__construct (   $cldrCategoryId,
  $cldrFormulaAndExamples 
)

Initialize the instance and parse the formula.

Parameters
string$cldrCategoryIdThe CLDR category identifier (eg 'pluralRule-count-one').
string$cldrFormulaAndExamplesThe CLDR formula and examples (eg 'i = 1 and v = 0 1').
Exceptions
Exception

Definition at line 32 of file Category.php.

References Gettext\Languages\CldrData\$categories, $s, array, Gettext\Languages\FormulaConverter\convertFormula(), and Gettext\Languages\CldrData\OTHER_CATEGORY.

33  {
34  $matches = array();
35  if (!preg_match('/^pluralRule-count-(.+)$/', $cldrCategoryId, $matches)) {
36  throw new Exception("Invalid CLDR category: '$cldrCategoryId'");
37  }
38  if (!in_array($matches[1], CldrData::$categories)) {
39  throw new Exception("Invalid CLDR category: '$cldrCategoryId'");
40  }
41  $this->id = $matches[1];
42  $cldrFormulaAndExamplesNormalized = trim(preg_replace('/\s+/', ' ', $cldrFormulaAndExamples));
43  if (!preg_match('/^([^@]*)(?:@integer([^@]+))?(?:@decimal(?:[^@]+))?$/', $cldrFormulaAndExamplesNormalized, $matches)) {
44  throw new Exception("Invalid CLDR category rule: $cldrFormulaAndExamples");
45  }
46  $cldrFormula = trim($matches[1]);
47  $s = isset($matches[2]) ? trim($matches[2]) : '';
48  $this->examples = ($s === '') ? null : $s;
49  switch ($this->id) {
51  if ($cldrFormula !== '') {
52  throw new Exception("The '".CldrData::OTHER_CATEGORY."' category should not have any formula, but it has '$cldrFormula'");
53  }
54  $this->formula = null;
55  break;
56  default:
57  if ($cldrFormula === '') {
58  throw new Exception("The '{$this->id}' category does not have a formula");
59  }
60  $this->formula = FormulaConverter::convertFormula($cldrFormula);
61  break;
62  }
63  }
$s
Definition: pwgen.php:45
static convertFormula($cldrFormula)
Converts a formula from the CLDR representation to the gettext representation.
Create styles array
The data for the language used.
+ Here is the call graph for this function:

Member Function Documentation

◆ expandExamples()

static Gettext\Languages\Category::expandExamples (   $examples)
static

Expand a list of examples as defined by CLDR.

Parameters
string$examplesA string like '1, 2, 5...7, …'.
Exceptions
ExceptionThrows an Exception if we weren't able to expand $examples.
Returns
int[]

Definition at line 79 of file Category.php.

References Gettext\Languages\Category\$examples, $from, $i, $m, $result, and array.

80  {
81  $result = array();
82  $m = null;
83  if (substr($examples, -strlen(', …')) === ', …') {
84  $examples = substr($examples, 0, strlen($examples) -strlen(', …'));
85  }
86  foreach (explode(',', str_replace(' ', '', $examples)) as $range) {
87  if (preg_match('/^\d+$/', $range)) {
88  $result[] = intval($range);
89  } elseif (preg_match('/^(\d+)~(\d+)$/', $range, $m)) {
90  $from = intval($m[1]);
91  $to = intval($m[2]);
92  $delta = $to - $from;
93  $step = (int) max(1, $delta / 100);
94  for ($i = $from; $i < $to; $i += $step) {
95  $result[] = $i;
96  }
97  $result[] = $to;
98  } else {
99  throw new Exception("Unhandled test range '$range' in '$examples'");
100  }
101  }
102  if (empty($result)) {
103  throw new Exception("No test numbers from '$examples'");
104  }
105 
106  return $result;
107  }
$result
$from
Create styles array
The data for the language used.
$i
Definition: disco.tpl.php:19

◆ getExampleIntegers()

Gettext\Languages\Category::getExampleIntegers ( )

Return a list of numbers corresponding to the $examples value.

Exceptions
ExceptionThrows an Exception if we weren't able to expand the examples.
Returns
int[]

Definition at line 69 of file Category.php.

70  {
71  return self::expandExamples($this->examples);
72  }

Field Documentation

◆ $examples

Gettext\Languages\Category::$examples

Definition at line 25 of file Category.php.

Referenced by Gettext\Languages\Category\expandExamples().

◆ $formula

Gettext\Languages\Category::$formula

Definition at line 20 of file Category.php.

◆ $id

Gettext\Languages\Category::$id

Definition at line 15 of file Category.php.


The documentation for this class was generated from the following file: