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