4 * @author Niklas Laxström, Tim Starling
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
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.
18 class CLDRPluralRuleConverter_Operator
extends CLDRPluralRuleConverter_Fragment
{
19 /** @var string The name */
23 * Each op type has three characters: left operand type, right operand type and result type
29 * A number is a kind of range.
33 static $opTypes = array(
41 'not-within' => 'nrb',
48 * Map converting from the abbrevation to the full form.
52 static $typeSpecMap = array(
59 * Map for converting the new operators introduced in Rev 33 to the old forms
61 static $aliasMap = array(
68 * Initialize a new instance of a CLDRPluralRuleConverter_Operator object
70 * @param CLDRPluralRuleConverter $parser The parser
71 * @param string $name The operator name
72 * @param int $pos The length
75 function __construct( $parser, $name, $pos, $length ) {
76 parent
::__construct( $parser, $pos, $length );
77 if ( isset( self
::$aliasMap[$name] ) ) {
78 $name = self
::$aliasMap[$name];
84 * Compute the operation
86 * @param CLDRPluralRuleConverter_Expression $left The left part of the expression
87 * @param CLDRPluralRuleConverter_Expression $right The right part of the expression
88 * @return CLDRPluralRuleConverter_Expression The result of the operation
90 public function operate( $left, $right ) {
91 $typeSpec = self
::$opTypes[$this->name
];
93 $leftType = self
::$typeSpecMap[$typeSpec[0]];
94 $rightType = self
::$typeSpecMap[$typeSpec[1]];
95 $resultType = self
::$typeSpecMap[$typeSpec[2]];
97 $start = min( $this->pos
, $left->pos
, $right->pos
);
98 $end = max( $this->end
, $left->end
, $right->end
);
99 $length = $end - $start;
101 $newExpr = new CLDRPluralRuleConverter_Expression( $this->parser
, $resultType,
102 "{$left->rpn} {$right->rpn} {$this->name}",
105 if ( !$left->isType( $leftType ) ) {
106 $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
109 if ( !$right->isType( $rightType ) ) {
110 $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );