Merge "Avoid image table updates on file upload failure"
[lhc/web/wiklou.git] / languages / utils / CLDRPluralRuleEvaluator_Range.php
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 * Evaluator helper class representing a range list.
14 */
15 class CLDRPluralRuleEvaluator_Range {
16 /**
17 * The parts
18 *
19 * @var array
20 */
21 public $parts = array();
22
23 /**
24 * Initialize a new instance of CLDRPluralRuleEvaluator_Range
25 *
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.
28 */
29 function __construct( $start, $end = false ) {
30 if ( $end === false ) {
31 $this->parts[] = $start;
32 } else {
33 $this->parts[] = array( $start, $end );
34 }
35 }
36
37 /**
38 * Determine if the given number is inside the range.
39 *
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.
43 */
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]
49 ) {
50 return true;
51 }
52 } else {
53 if ( $number == $part ) {
54 return true;
55 }
56 }
57 }
58 return false;
59 }
60
61 /**
62 * Readable alias for isNumberIn( $number, false ), and the implementation
63 * of the "within" operator.
64 *
65 * @param int $number The number to check
66 * @return bool True if the number is inside the range; otherwise, false.
67 */
68 function isNumberWithin( $number ) {
69 return $this->isNumberIn( $number, false );
70 }
71
72 /**
73 * Add another part to this range.
74 *
75 * @param CLDRPluralRuleEvaluator_Range|int $other The part to add, either
76 * a range object itself or a single number.
77 */
78 function add( $other ) {
79 if ( $other instanceof self ) {
80 $this->parts = array_merge( $this->parts, $other->parts );
81 } else {
82 $this->parts[] = $other;
83 }
84 }
85
86 /**
87 * Returns the string representation of the rule evaluator range.
88 * The purpose of this method is to help debugging.
89 *
90 * @return string The string representation of the rule evaluator range
91 */
92 function __toString() {
93 $s = 'Range(';
94 foreach ( $this->parts as $i => $part ) {
95 if ( $i ) {
96 $s .= ', ';
97 }
98 if ( is_array( $part ) ) {
99 $s .= $part[0] . '..' . $part[1];
100 } else {
101 $s .= $part;
102 }
103 }
104 $s .= ')';
105 return $s;
106 }
107
108 }