ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Category.php
Go to the documentation of this file.
1<?php
3
4use Exception;
5
10{
15 public $id;
20 public $formula;
25 public $examples;
32 public function __construct($cldrCategoryId, $cldrFormulaAndExamples)
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 }
69 public function getExampleIntegers()
70 {
71 return self::expandExamples($this->examples);
72 }
79 public static function expandExamples($examples)
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 }
108}
$result
An exception for terminatinating execution or to throw for unit testing.
A helper class that handles a single category rules (eg 'zero', 'one', ...) and its formula and examp...
Definition: Category.php:10
__construct($cldrCategoryId, $cldrFormulaAndExamples)
Initialize the instance and parse the formula.
Definition: Category.php:32
static expandExamples($examples)
Expand a list of examples as defined by CLDR.
Definition: Category.php:79
getExampleIntegers()
Return a list of numbers corresponding to the $examples value.
Definition: Category.php:69
static convertFormula($cldrFormula)
Converts a formula from the CLDR representation to the gettext representation.
$i
Definition: disco.tpl.php:19
$s
Definition: pwgen.php:45
$from