Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Range
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 5
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isNumberIn
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
72
 isNumberWithin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __toString
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * @author Tim Starling
4 * @author Niklas Laxström
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace CLDRPluralRuleParser;
10
11/**
12 * Evaluator helper class representing a range list.
13 */
14class Range {
15    /**
16     * The parts
17     *
18     * @var array
19     */
20    public $parts = [];
21
22    /**
23     * Initialize a new instance of Range
24     *
25     * @param int $start The start of the range
26     * @param int|bool $end The end of the range, or false if the range is not bounded.
27     */
28    public function __construct( $start, $end = false ) {
29        if ( $end === false ) {
30            $this->parts[] = $start;
31        } else {
32            $this->parts[] = [ $start, $end ];
33        }
34    }
35
36    /**
37     * Determine if the given number is inside the range.
38     *
39     * @param int $number The number to check
40     * @param bool $integerConstraint If true, also asserts the number is an integer;
41     *   otherwise, number simply has to be inside the range.
42     * @return bool True if the number is inside the range; otherwise, false.
43     */
44    public function isNumberIn( $number, $integerConstraint = true ): bool {
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
59        return false;
60    }
61
62    /**
63     * Readable alias for isNumberIn( $number, false ), and the implementation
64     * of the "within" operator.
65     *
66     * @param int $number The number to check
67     * @return bool True if the number is inside the range; otherwise, false.
68     */
69    public function isNumberWithin( $number ): bool {
70        return $this->isNumberIn( $number, false );
71    }
72
73    /**
74     * Add another part to this range.
75     *
76     * @param Range|int $other The part to add, either
77     *   a range object itself or a single number.
78     */
79    public function add( $other ) {
80        if ( $other instanceof self ) {
81            $this->parts = array_merge( $this->parts, $other->parts );
82        } else {
83            $this->parts[] = $other;
84        }
85    }
86
87    /**
88     * Returns the string representation of the rule evaluator range.
89     * The purpose of this method is to help debugging.
90     *
91     * @return string The string representation of the rule evaluator range
92     */
93    public function __toString() {
94        $s = 'Range(';
95        foreach ( $this->parts as $i => $part ) {
96            if ( $i ) {
97                $s .= ', ';
98            }
99            if ( is_array( $part ) ) {
100                $s .= $part[0] . '..' . $part[1];
101            } else {
102                $s .= $part;
103            }
104        }
105        $s .= ')';
106
107        return $s;
108    }
109}