Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / languages / utils / CLDRPluralRuleEvaluatorRange.php
blob996c22e34990844b1254d34929d42977075af6f4
1 <?php
2 /**
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
8 * @file
9 * @since 1.20
12 /**
13 * Evaluator helper class representing a range list.
15 class CLDRPluralRuleEvaluatorRange {
16 /**
17 * The parts
19 * @var array
21 public $parts = array();
23 /**
24 * Initialize a new instance of CLDRPluralRuleEvaluatorRange
26 * @param int $start The start of the range
27 * @param int|bool $end The end of the range, or false if the range is not bounded.
29 function __construct( $start, $end = false ) {
30 if ( $end === false ) {
31 $this->parts[] = $start;
32 } else {
33 $this->parts[] = array( $start, $end );
37 /**
38 * Determine if the given number is inside the range.
40 * @param int $number The number to check
41 * @param bool $integerConstraint If true, also asserts the number is an integer;
42 * otherwise, number simply has to be inside the range.
43 * @return bool True if the number is inside the range; otherwise, false.
45 function isNumberIn( $number, $integerConstraint = true ) {
46 foreach ( $this->parts as $part ) {
47 if ( is_array( $part ) ) {
48 if ( ( !$integerConstraint || floor( $number ) === (float)$number )
49 && $number >= $part[0] && $number <= $part[1]
50 ) {
51 return true;
53 } else {
54 if ( $number == $part ) {
55 return true;
60 return false;
63 /**
64 * Readable alias for isNumberIn( $number, false ), and the implementation
65 * of the "within" operator.
67 * @param int $number The number to check
68 * @return bool True if the number is inside the range; otherwise, false.
70 function isNumberWithin( $number ) {
71 return $this->isNumberIn( $number, false );
74 /**
75 * Add another part to this range.
77 * @param CLDRPluralRuleEvaluatorRange|int $other The part to add, either
78 * a range object itself or a single number.
80 function add( $other ) {
81 if ( $other instanceof self ) {
82 $this->parts = array_merge( $this->parts, $other->parts );
83 } else {
84 $this->parts[] = $other;
88 /**
89 * Returns the string representation of the rule evaluator range.
90 * The purpose of this method is to help debugging.
92 * @return string The string representation of the rule evaluator range
94 function __toString() {
95 $s = 'Range(';
96 foreach ( $this->parts as $i => $part ) {
97 if ( $i ) {
98 $s .= ', ';
100 if ( is_array( $part ) ) {
101 $s .= $part[0] . '..' . $part[1];
102 } else {
103 $s .= $part;
106 $s .= ')';
108 return $s;