summaryrefslogtreecommitdiff
path: root/languages/utils/CLDRPluralRuleEvaluatorRange.php
blob: 996c22e34990844b1254d34929d42977075af6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
 * @author Niklas Laxström, Tim Starling
 *
 * @copyright Copyright © 2010-2012, Niklas Laxström
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 *
 * @file
 * @since 1.20
 */

/**
 * Evaluator helper class representing a range list.
 */
class CLDRPluralRuleEvaluatorRange {
	/**
	 * The parts
	 *
	 * @var array
	 */
	public $parts = array();

	/**
	 * Initialize a new instance of CLDRPluralRuleEvaluatorRange
	 *
	 * @param int $start The start of the range
	 * @param int|bool $end The end of the range, or false if the range is not bounded.
	 */
	function __construct( $start, $end = false ) {
		if ( $end === false ) {
			$this->parts[] = $start;
		} else {
			$this->parts[] = array( $start, $end );
		}
	}

	/**
	 * Determine if the given number is inside the range.
	 *
	 * @param int $number The number to check
	 * @param bool $integerConstraint If true, also asserts the number is an integer;
	 *   otherwise, number simply has to be inside the range.
	 * @return bool True if the number is inside the range; otherwise, false.
	 */
	function isNumberIn( $number, $integerConstraint = true ) {
		foreach ( $this->parts as $part ) {
			if ( is_array( $part ) ) {
				if ( ( !$integerConstraint || floor( $number ) === (float)$number )
					&& $number >= $part[0] && $number <= $part[1]
				) {
					return true;
				}
			} else {
				if ( $number == $part ) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * Readable alias for isNumberIn( $number, false ), and the implementation
	 * of the "within" operator.
	 *
	 * @param int $number The number to check
	 * @return bool True if the number is inside the range; otherwise, false.
	 */
	function isNumberWithin( $number ) {
		return $this->isNumberIn( $number, false );
	}

	/**
	 * Add another part to this range.
	 *
	 * @param CLDRPluralRuleEvaluatorRange|int $other The part to add, either
	 *   a range object itself or a single number.
	 */
	function add( $other ) {
		if ( $other instanceof self ) {
			$this->parts = array_merge( $this->parts, $other->parts );
		} else {
			$this->parts[] = $other;
		}
	}

	/**
	 * Returns the string representation of the rule evaluator range.
	 * The purpose of this method is to help debugging.
	 *
	 * @return string The string representation of the rule evaluator range
	 */
	function __toString() {
		$s = 'Range(';
		foreach ( $this->parts as $i => $part ) {
			if ( $i ) {
				$s .= ', ';
			}
			if ( is_array( $part ) ) {
				$s .= $part[0] . '..' . $part[1];
			} else {
				$s .= $part;
			}
		}
		$s .= ')';

		return $s;
	}
}