Remove underscore from classes CLDRPluralRule*
[lhc/web/wiklou.git] / languages / utils / CLDRPluralRuleConverterOperator.php
1 <?php
2
3 /**
4 * @author Niklas Laxström, Tim Starling
5 *
6 * @copyright Copyright © 2010-2012, Niklas Laxström
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 *
9 * @file
10 * @since 1.20
11 */
12
13 /**
14 * Helper for CLDRPluralRuleConverter.
15 * An operator object, representing a region of the input string (for error
16 * messages), and the binary operator at that location.
17 */
18 class CLDRPluralRuleConverterOperator extends CLDRPluralRuleConverterFragment {
19 /** @var string The name */
20 public $name;
21
22 /**
23 * Each op type has three characters: left operand type, right operand type and result type
24 *
25 * b = boolean
26 * n = number
27 * r = range
28 *
29 * A number is a kind of range.
30 *
31 * @var array
32 */
33 static $opTypes = array(
34 'or' => 'bbb',
35 'and' => 'bbb',
36 'is' => 'nnb',
37 'is-not' => 'nnb',
38 'in' => 'nrb',
39 'not-in' => 'nrb',
40 'within' => 'nrb',
41 'not-within' => 'nrb',
42 'mod' => 'nnn',
43 ',' => 'rrr',
44 '..' => 'nnr',
45 );
46
47 /**
48 * Map converting from the abbrevation to the full form.
49 *
50 * @var array
51 */
52 static $typeSpecMap = array(
53 'b' => 'boolean',
54 'n' => 'number',
55 'r' => 'range',
56 );
57
58 /**
59 * Map for converting the new operators introduced in Rev 33 to the old forms
60 */
61 static $aliasMap = array(
62 '%' => 'mod',
63 '!=' => 'not-in',
64 '=' => 'in'
65 );
66
67 /**
68 * Initialize a new instance of a CLDRPluralRuleConverterOperator object
69 *
70 * @param CLDRPluralRuleConverter $parser The parser
71 * @param string $name The operator name
72 * @param int $pos The length
73 * @param int $length
74 */
75 function __construct( $parser, $name, $pos, $length ) {
76 parent::__construct( $parser, $pos, $length );
77 if ( isset( self::$aliasMap[$name] ) ) {
78 $name = self::$aliasMap[$name];
79 }
80 $this->name = $name;
81 }
82
83 /**
84 * Compute the operation
85 *
86 * @param CLDRPluralRuleConverterExpression $left The left part of the expression
87 * @param CLDRPluralRuleConverterExpression $right The right part of the expression
88 * @return CLDRPluralRuleConverterExpression The result of the operation
89 */
90 public function operate( $left, $right ) {
91 $typeSpec = self::$opTypes[$this->name];
92
93 $leftType = self::$typeSpecMap[$typeSpec[0]];
94 $rightType = self::$typeSpecMap[$typeSpec[1]];
95 $resultType = self::$typeSpecMap[$typeSpec[2]];
96
97 $start = min( $this->pos, $left->pos, $right->pos );
98 $end = max( $this->end, $left->end, $right->end );
99 $length = $end - $start;
100
101 $newExpr = new CLDRPluralRuleConverterExpression( $this->parser, $resultType,
102 "{$left->rpn} {$right->rpn} {$this->name}",
103 $start, $length );
104
105 if ( !$left->isType( $leftType ) ) {
106 $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
107 }
108
109 if ( !$right->isType( $rightType ) ) {
110 $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );
111 }
112 return $newExpr;
113 }
114 }