61ab947b326a87c84ab93f057b1c352085208802
[lhc/web/wiklou.git] / languages / utils / CLDRPluralRuleEvaluator.php
1 <?php
2 /**
3 * Parse and evaluate a plural rule.
4 *
5 * UTS #35 Revision 33
6 * http://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
7 *
8 * @author Niklas Laxström, Tim Starling
9 *
10 * @copyright Copyright © 2010-2012, Niklas Laxström
11 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
12 * or later
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
28 *
29 *
30 * @file
31 * @since 1.20
32 */
33
34 class CLDRPluralRuleEvaluator {
35 /**
36 * Evaluate a number against a set of plural rules. If a rule passes,
37 * return the index of plural rule.
38 *
39 * @param int $number The number to be evaluated against the rules
40 * @param array $rules The associative array of plural rules in pluralform => rule format.
41 * @return int The index of the plural form which passed the evaluation
42 */
43 public static function evaluate( $number, array $rules ) {
44 $rules = self::compile( $rules );
45 return self::evaluateCompiled( $number, $rules );
46 }
47
48 /**
49 * Convert a set of rules to a compiled form which is optimised for
50 * fast evaluation. The result will be an array of strings, and may be cached.
51 *
52 * @param array $rules The rules to compile
53 * @return array An array of compile rules.
54 */
55 public static function compile( array $rules ) {
56 // We can't use array_map() for this because it generates a warning if
57 // there is an exception.
58 foreach ( $rules as &$rule ) {
59 $rule = CLDRPluralRuleConverter::convert( $rule );
60 }
61 return $rules;
62 }
63
64 /**
65 * Evaluate a compiled set of rules returned by compile(). Do not allow
66 * the user to edit the compiled form, or else PHP errors may result.
67 *
68 * @param string $number The number to be evaluated against the rules, in English, or it
69 * may be a type convertible to string.
70 * @param array $rules The associative array of plural rules in pluralform => rule format.
71 * @return int The index of the plural form which passed the evaluation
72 */
73 public static function evaluateCompiled( $number, array $rules ) {
74 // Calculate the values of the operand symbols
75 $number = strval( $number );
76 if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {
77 wfDebug( __METHOD__ . ": invalid number input, returning 'other'\n" );
78 return count( $rules );
79 }
80 if ( !isset( $m[3] ) ) {
81 $operandSymbols = array(
82 'n' => intval( $m[1] ),
83 'i' => intval( $m[1] ),
84 'v' => 0,
85 'w' => 0,
86 'f' => 0,
87 't' => 0
88 );
89 } else {
90 $absValStr = $m[1];
91 $intStr = $m[2];
92 $fracStr = $m[3];
93 $operandSymbols = array(
94 'n' => floatval( $absValStr ),
95 'i' => intval( $intStr ),
96 'v' => strlen( $fracStr ),
97 'w' => strlen( rtrim( $fracStr, '0' ) ),
98 'f' => intval( $fracStr ),
99 't' => intval( rtrim( $fracStr, '0' ) ),
100 );
101 }
102
103 // The compiled form is RPN, with tokens strictly delimited by
104 // spaces, so this is a simple RPN evaluator.
105 foreach ( $rules as $i => $rule ) {
106 $stack = array();
107 $zero = ord( '0' );
108 $nine = ord( '9' );
109 foreach ( StringUtils::explode( ' ', $rule ) as $token ) {
110 $ord = ord( $token );
111 if ( isset( $operandSymbols[$token] ) ) {
112 $stack[] = $operandSymbols[$token];
113 } elseif ( $ord >= $zero && $ord <= $nine ) {
114 $stack[] = intval( $token );
115 } else {
116 $right = array_pop( $stack );
117 $left = array_pop( $stack );
118 $result = self::doOperation( $token, $left, $right );
119 $stack[] = $result;
120 }
121 }
122 if ( $stack[0] ) {
123 return $i;
124 }
125 }
126 // None of the provided rules match. The number belongs to category
127 // 'other', which comes last.
128 return count( $rules );
129 }
130
131 /**
132 * Do a single operation
133 *
134 * @param string $token The token string
135 * @param mixed $left The left operand. If it is an object, its state may be destroyed.
136 * @param mixed $right The right operand
137 * @throws CLDRPluralRuleError
138 * @return mixed The operation result
139 */
140 private static function doOperation( $token, $left, $right ) {
141 if ( in_array( $token, array( 'in', 'not-in', 'within', 'not-within' ) ) ) {
142 if ( !( $right instanceof CLDRPluralRuleEvaluator_Range ) ) {
143 $right = new CLDRPluralRuleEvaluator_Range( $right );
144 }
145 }
146 switch ( $token ) {
147 case 'or':
148 return $left || $right;
149 case 'and':
150 return $left && $right;
151 case 'is':
152 return $left == $right;
153 case 'is-not':
154 return $left != $right;
155 case 'in':
156 return $right->isNumberIn( $left );
157 case 'not-in':
158 return !$right->isNumberIn( $left );
159 case 'within':
160 return $right->isNumberWithin( $left );
161 case 'not-within':
162 return !$right->isNumberWithin( $left );
163 case 'mod':
164 if ( is_int( $left ) ) {
165 return (int)fmod( $left, $right );
166 }
167 return fmod( $left, $right );
168 case ',':
169 if ( $left instanceof CLDRPluralRuleEvaluator_Range ) {
170 $range = $left;
171 } else {
172 $range = new CLDRPluralRuleEvaluator_Range( $left );
173 }
174 $range->add( $right );
175 return $range;
176 case '..':
177 return new CLDRPluralRuleEvaluator_Range( $left, $right );
178 default:
179 throw new CLDRPluralRuleError( "Invalid RPN token" );
180 }
181 }
182 }