| Total Complexity | 36 |
| Total Lines | 176 |
| Duplicated Lines | 0 % |
| Coverage | 85.71% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 20 | class NumberHelper |
||
| 21 | { |
||
| 22 | |||
| 23 | const PATTERN_ORDINAL = "/\d+(st|nd|rd|th)?\.?$/"; |
||
| 24 | |||
| 25 | const PATTERN_ROMAN = "/^[ivxlcdm]+\.?$/i"; |
||
| 26 | |||
| 27 | const PATTERN_AFFIXES = "/^[a-z]?\d+[a-z]?$/i"; |
||
| 28 | |||
| 29 | const PATTERN_COMMA_AMPERSAND_RANGE = "/\d*([\s?\-&+,;\s])+\d+/"; |
||
| 30 | |||
| 31 | const ROMAN_NUMERALS = [ |
||
| 32 | ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"], |
||
| 33 | ["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"], |
||
| 34 | ["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"], |
||
| 35 | ["", "m", "mm", "mmm", "mmmm", "mmmmm"] |
||
| 36 | ]; |
||
| 37 | |||
| 38 | const ROMAN_DIGITS = [ |
||
| 39 | "M" => 1000, |
||
| 40 | "D" => 500, |
||
| 41 | "C" => 100, |
||
| 42 | "L" => 50, |
||
| 43 | "X" => 10, |
||
| 44 | "V" => 5, |
||
| 45 | "I" => 1 |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
|
1 ignored issue
–
show
|
|||
| 49 | * @return Closure |
||
| 50 | */ |
||
| 51 | public static function getCompareNumber() |
||
| 63 | }; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
|
1 ignored issue
–
show
|
|||
| 67 | * @param $num |
||
|
2 ignored issues
–
show
|
|||
| 68 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 69 | */ |
||
| 70 | 4 | public static function dec2roman($num) |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
|
1 ignored issue
–
show
|
|||
| 86 | * @param $romanNumber |
||
|
2 ignored issues
–
show
|
|||
| 87 | * @return int|mixed |
||
|
1 ignored issue
–
show
|
|||
| 88 | */ |
||
| 89 | 4 | public static function roman2Dec($romanNumber) |
|
| 90 | { |
||
| 91 | 4 | $romanNumber = trim($romanNumber); |
|
| 92 | 4 | if (is_numeric($romanNumber)) { |
|
| 93 | return 0; |
||
| 94 | } |
||
| 95 | |||
| 96 | 4 | $values = []; |
|
| 97 | // Convert the string to an array of roman values: |
||
| 98 | 4 | for ($i = 0; $i < strlen($romanNumber); ++$i) { |
|
| 99 | 4 | $char = strtoupper($romanNumber{$i}); |
|
| 100 | 4 | if (self::ROMAN_DIGITS[$char] !== null) { |
|
| 101 | 4 | $values[] = self::ROMAN_DIGITS[$char]; |
|
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | 4 | $sum = 0; |
|
| 106 | 4 | while ($current = current($values)) { |
|
| 107 | 4 | $next = next($values); |
|
| 108 | 4 | $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current; |
|
| 109 | } |
||
| 110 | 4 | return $sum; |
|
| 111 | } |
||
| 112 | |||
| 113 | 22 | public static function isRomanNumber($str) |
|
| 114 | { |
||
| 115 | 22 | $number = trim($str); |
|
| 116 | 22 | for ($i = 0; $i < strlen($number); ++$i) { |
|
| 117 | 22 | $char = strtoupper($number{$i}); |
|
| 118 | 22 | if (!in_array($char, array_keys(self::ROMAN_DIGITS))) { |
|
| 119 | 21 | return false; |
|
| 120 | } |
||
| 121 | } |
||
| 122 | 4 | return true; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
|
1 ignored issue
–
show
|
|||
| 126 | * @param $str |
||
|
2 ignored issues
–
show
|
|||
| 127 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 128 | */ |
||
| 129 | 1 | public static function evaluateStringPluralism($str) |
|
| 130 | { |
||
| 131 | 1 | $plural = 'single'; |
|
| 132 | 1 | if (!empty($str)) { |
|
| 133 | 1 | $isRange = self::isRange($str); |
|
| 134 | 1 | if ($isRange) { |
|
| 135 | 1 | return 'multiple'; |
|
| 136 | } else { |
||
| 137 | 1 | if (is_numeric($str) || NumberHelper::isRomanNumber($str)) { |
|
| 138 | 1 | return 'single'; |
|
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | return $plural; |
||
| 143 | } |
||
| 144 | |||
| 145 | 49 | public static function extractNumber($string) |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
|
1 ignored issue
–
show
|
|||
| 154 | * @param $str |
||
|
2 ignored issues
–
show
|
|||
| 155 | * @return array[]|false|string[] |
||
|
1 ignored issue
–
show
|
|||
| 156 | */ |
||
| 157 | 20 | public static function splitByRangeDelimiter($str) |
|
| 158 | { |
||
| 159 | 20 | return preg_split("/[-–&,]/", $str); |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
|
1 ignored issue
–
show
|
|||
| 163 | * @param string $str |
||
|
2 ignored issues
–
show
|
|||
| 164 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 165 | */ |
||
| 166 | 1 | private static function isRange($str) |
|
|
1 ignored issue
–
show
|
|||
| 167 | { |
||
| 168 | 1 | $rangeParts = self::splitByRangeDelimiter($str); |
|
| 169 | 1 | $isRange = false; |
|
| 170 | 1 | if (count($rangeParts) > 1) { |
|
| 171 | 1 | $isRange = true; |
|
| 172 | 1 | foreach ($rangeParts as $range) { |
|
| 173 | 1 | if (NumberHelper::isRomanNumber(trim($range)) || is_numeric(trim($range))) { |
|
| 174 | 1 | $isRange = $isRange && true; |
|
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | 1 | return $isRange; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
|
1 ignored issue
–
show
|
|||
| 182 | * @param int|string $number |
||
|
2 ignored issues
–
show
|
|||
| 183 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 184 | */ |
||
| 185 | 19 | public static function isRomanRange($number) |
|
| 196 | } |
||
| 197 | } |
||
| 198 |