MediaWiki  REL1_24
CLDRPluralRuleConverterOperator.php
Go to the documentation of this file.
00001 <?php
00017 class CLDRPluralRuleConverterOperator extends CLDRPluralRuleConverterFragment {
00019     public $name;
00020 
00032     private static $opTypes = array(
00033         'or' => 'bbb',
00034         'and' => 'bbb',
00035         'is' => 'nnb',
00036         'is-not' => 'nnb',
00037         'in' => 'nrb',
00038         'not-in' => 'nrb',
00039         'within' => 'nrb',
00040         'not-within' => 'nrb',
00041         'mod' => 'nnn',
00042         ',' => 'rrr',
00043         '..' => 'nnr',
00044     );
00045 
00051     private static $typeSpecMap = array(
00052         'b' => 'boolean',
00053         'n' => 'number',
00054         'r' => 'range',
00055     );
00056 
00060     private static $aliasMap = array(
00061         '%' => 'mod',
00062         '!=' => 'not-in',
00063         '=' => 'in'
00064     );
00065 
00074     function __construct( $parser, $name, $pos, $length ) {
00075         parent::__construct( $parser, $pos, $length );
00076         if ( isset( self::$aliasMap[$name] ) ) {
00077             $name = self::$aliasMap[$name];
00078         }
00079         $this->name = $name;
00080     }
00081 
00089     public function operate( $left, $right ) {
00090         $typeSpec = self::$opTypes[$this->name];
00091 
00092         $leftType = self::$typeSpecMap[$typeSpec[0]];
00093         $rightType = self::$typeSpecMap[$typeSpec[1]];
00094         $resultType = self::$typeSpecMap[$typeSpec[2]];
00095 
00096         $start = min( $this->pos, $left->pos, $right->pos );
00097         $end = max( $this->end, $left->end, $right->end );
00098         $length = $end - $start;
00099 
00100         $newExpr = new CLDRPluralRuleConverterExpression( $this->parser, $resultType,
00101             "{$left->rpn} {$right->rpn} {$this->name}",
00102             $start, $length );
00103 
00104         if ( !$left->isType( $leftType ) ) {
00105             $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
00106         }
00107 
00108         if ( !$right->isType( $rightType ) ) {
00109             $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );
00110         }
00111 
00112         return $newExpr;
00113     }
00114 }