3 * @author Niklas Laxström, Tim Starling
5 * @copyright Copyright © 2010-2012, Niklas Laxström
6 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
13 * Helper for CLDRPluralRuleConverter.
14 * An operator object, representing a region of the input string (for error
15 * messages), and the binary operator at that location.
17 class CLDRPluralRuleConverterOperator
extends CLDRPluralRuleConverterFragment
{
18 /** @var string The name */
22 * Each op type has three characters: left operand type, right operand type and result type
28 * A number is a kind of range.
32 private static $opTypes = array(
40 'not-within' => 'nrb',
47 * Map converting from the abbrevation to the full form.
51 private static $typeSpecMap = array(
58 * Map for converting the new operators introduced in Rev 33 to the old forms
60 private static $aliasMap = array(
67 * Initialize a new instance of a CLDRPluralRuleConverterOperator object
69 * @param CLDRPluralRuleConverter $parser The parser
70 * @param string $name The operator name
71 * @param int $pos The length
74 function __construct( $parser, $name, $pos, $length ) {
75 parent
::__construct( $parser, $pos, $length );
76 if ( isset( self
::$aliasMap[$name] ) ) {
77 $name = self
::$aliasMap[$name];
83 * Compute the operation
85 * @param CLDRPluralRuleConverterExpression $left The left part of the expression
86 * @param CLDRPluralRuleConverterExpression $right The right part of the expression
87 * @return CLDRPluralRuleConverterExpression The result of the operation
89 public function operate( $left, $right ) {
90 $typeSpec = self
::$opTypes[$this->name
];
92 $leftType = self
::$typeSpecMap[$typeSpec[0]];
93 $rightType = self
::$typeSpecMap[$typeSpec[1]];
94 $resultType = self
::$typeSpecMap[$typeSpec[2]];
96 $start = min( $this->pos
, $left->pos
, $right->pos
);
97 $end = max( $this->end
, $left->end
, $right->end
);
98 $length = $end - $start;
100 $newExpr = new CLDRPluralRuleConverterExpression( $this->parser
, $resultType,
101 "{$left->rpn} {$right->rpn} {$this->name}",
104 if ( !$left->isType( $leftType ) ) {
105 $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
108 if ( !$right->isType( $rightType ) ) {
109 $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );