19 if (strpbrk($cldrFormula,
'()') !==
false) {
20 throw new Exception(
"Unable to convert the formula '$cldrFormula': parenthesis handling not implemented");
22 $orSeparatedChunks = array();
23 foreach (explode(
' or ', $cldrFormula) as $cldrFormulaChunk) {
24 $gettextFormulaChunk = null;
25 $andSeparatedChunks = array();
26 foreach (explode(
' and ', $cldrFormulaChunk) as $cldrAtom) {
27 $gettextAtom = self::convertAtom($cldrAtom);
28 if ($gettextAtom ===
false) {
30 $gettextFormulaChunk =
false;
32 } elseif ($gettextAtom !==
true) {
33 $andSeparatedChunks[] = $gettextAtom;
36 if (!isset($gettextFormulaChunk)) {
37 if (empty($andSeparatedChunks)) {
39 $gettextFormulaChunk =
true;
41 $gettextFormulaChunk = implode(
' && ', $andSeparatedChunks);
43 switch ($gettextFormulaChunk) {
44 case 'n >= 0 && n <= 2 && n != 2':
45 $gettextFormulaChunk =
'n == 0 || n == 1';
50 if ($gettextFormulaChunk ===
true) {
53 } elseif ($gettextFormulaChunk !==
false) {
54 $orSeparatedChunks[] = $gettextFormulaChunk;
57 if (empty($orSeparatedChunks)) {
61 return implode(
' || ', $orSeparatedChunks);
73 $gettextAtom = $cldrAtom;
74 $gettextAtom = str_replace(
' = ',
' == ', $gettextAtom);
75 $gettextAtom = str_replace(
'i',
'n', $gettextAtom);
76 if (preg_match(
'/^n( % \d+)? (!=|==) \d+$/', $gettextAtom)) {
79 if (preg_match(
'/^n( % \d+)? (!=|==) \d+(,\d+|\.\.\d+)+$/', $gettextAtom)) {
80 return self::expandAtom($gettextAtom);
82 if (preg_match(
'/^(?:v|w)(?: % 10+)? == (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
83 return (intval(
$m[1]) === 0) ? true :
false;
85 if (preg_match(
'/^(?:v|w)(?: % 10+)? != (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
86 return (intval(
$m[1]) === 0) ? false :
true;
88 if (preg_match(
'/^(?:f|t)(?: % 10+)? == (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
89 return (intval(
$m[1]) === 0) ? true :
false;
91 if (preg_match(
'/^(?:f|t)(?: % 10+)? != (\d+)(?:\.\.\d+)?$/', $gettextAtom,
$m)) {
92 return (intval(
$m[1]) === 0) ? false :
true;
94 throw new Exception(
"Unable to convert the formula chunk '$cldrAtom' from CLDR to gettext");
105 if (preg_match(
'/^(n(?: % \d+)?) (==|!=) (\d+(?:\.\.\d+|,\d+)+)$/', $atom,
$m)) {
109 foreach (explode(
',',
$m[3]) as $range) {
111 if ((!isset($chunk)) && preg_match(
'/^\d+$/', $range)) {
112 $chunk =
"$what $op $range";
114 if ((!isset($chunk)) && preg_match(
'/^(\d+)\.\.(\d+)$/', $range,
$m)) {
117 if (($to -
$from) === 1) {
120 $chunk =
"($what == $from || $what == $to)";
123 $chunk =
"$what != $from && $what == $to";
129 $chunk =
"$what >= $from && $what <= $to";
132 if ($what ===
'n' &&
$from <= 0) {
133 $chunk =
"$what > $to";
135 $chunk =
"($what < $from || $what > $to)";
141 if (!isset($chunk)) {
142 throw new Exception(
"Unhandled range '$range' in '$atom'");
146 if (count($chunks) === 1) {
151 return '('.implode(
' || ', $chunks).
')';
break;
153 return implode(
' && ', $chunks);
156 throw new Exception(
"Unable to expand '$atom'");
static expandAtom($atom)
Expands an atom containing a range (for instance: 'n == 1,3..5').
static convertFormula($cldrFormula)
Converts a formula from the CLDR representation to the gettext representation.
static convertAtom($cldrAtom)
Converts an atomic part of the CLDR formula to its gettext representation.
A helper class to convert a CLDR formula to a gettext formula.