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
13 * Evaluator helper class representing a range list.
15 class CLDRPluralRuleEvaluatorRange
{
21 public $parts = array();
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;
33 $this->parts
[] = array( $start, $end );
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; otherwise, number simply has to be inside the range.
42 * @return bool True if the number is inside the range; otherwise, false.
44 function isNumberIn( $number, $integerConstraint = true ) {
45 foreach ( $this->parts
as $part ) {
46 if ( is_array( $part ) ) {
47 if ( ( !$integerConstraint ||
floor( $number ) === (float)$number )
48 && $number >= $part[0] && $number <= $part[1]
53 if ( $number == $part ) {
63 * Readable alias for isNumberIn( $number, false ), and the implementation
64 * of the "within" operator.
66 * @param int $number The number to check
67 * @return bool True if the number is inside the range; otherwise, false.
69 function isNumberWithin( $number ) {
70 return $this->isNumberIn( $number, false );
74 * Add another part to this range.
76 * @param CLDRPluralRuleEvaluatorRange|int $other The part to add, either
77 * a range object itself or a single number.
79 function add( $other ) {
80 if ( $other instanceof self
) {
81 $this->parts
= array_merge( $this->parts
, $other->parts
);
83 $this->parts
[] = $other;
88 * Returns the string representation of the rule evaluator range.
89 * The purpose of this method is to help debugging.
91 * @return string The string representation of the rule evaluator range
93 function __toString() {
95 foreach ( $this->parts
as $i => $part ) {
99 if ( is_array( $part ) ) {
100 $s .= $part[0] . '..' . $part[1];