[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/languages/utils/ -> CLDRPluralRuleConverterOperator.php (source)

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


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1