| Total Complexity | 29 |
| Total Lines | 140 |
| Duplicated Lines | 0 % |
| Coverage | 81.48% |
| Changes | 0 | ||
| 1 | <?php |
||
| 18 | class NumberHelper |
||
| 19 | { |
||
| 20 | |||
| 21 | const PATTERN_ORDINAL = "/\d+(st|nd|rd|th)?\.?$/"; |
||
| 22 | |||
| 23 | const PATTERN_ROMAN = "/^[ivxlcdm]+\.?$/i"; |
||
| 24 | |||
| 25 | const PATTERN_AFFIXES = "/^[a-z]?\d+[a-z]?$/i"; |
||
| 26 | |||
| 27 | const PATTERN_COMMA_AMPERSAND_RANGE = "/\d*([\s?\-&+,;\s])+\d+/"; |
||
| 28 | |||
| 29 | const ROMAN_NUMERALS = [ |
||
| 30 | ["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"], |
||
| 31 | ["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"], |
||
| 32 | ["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"], |
||
| 33 | ["", "m", "mm", "mmm", "mmmm", "mmmmm"] |
||
| 34 | ]; |
||
| 35 | |||
| 36 | const ROMAN_DIGITS = [ |
||
| 37 | "M" => 1000, |
||
| 38 | "D" => 500, |
||
| 39 | "C" => 100, |
||
| 40 | "L" => 50, |
||
| 41 | "X" => 10, |
||
| 42 | "V" => 5, |
||
| 43 | "I" => 1 |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
|
1 ignored issue
–
show
|
|||
| 47 | * @return \Closure |
||
| 48 | */ |
||
| 49 | public static function getCompareNumber() |
||
| 50 | { |
||
| 51 | return function($numA, $numB, $order) { |
||
| 52 | if (is_numeric($numA) && is_numeric($numB)) { |
||
| 53 | $ret = $numA - $numB; |
||
| 54 | } else { |
||
| 55 | $ret = strcasecmp($numA, $numB); |
||
| 56 | } |
||
| 57 | if ("descending" === $order) { |
||
| 58 | return $ret > 0 ? -1 : 1; |
||
| 59 | } |
||
| 60 | return $ret > 0 ? 1 : -1; |
||
| 61 | }; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
|
1 ignored issue
–
show
|
|||
| 65 | * @param $num |
||
|
1 ignored issue
–
show
|
|||
| 66 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 67 | */ |
||
| 68 | 4 | public static function dec2roman($num) |
|
| 69 | { |
||
| 70 | 4 | $ret = ""; |
|
| 71 | 4 | if ($num < 6000) { |
|
| 72 | |||
| 73 | 4 | $numStr = strrev($num); |
|
| 74 | 4 | $len = strlen($numStr); |
|
| 75 | 4 | for ($pos = 0; $pos < $len; $pos++) { |
|
| 76 | 4 | $n = $numStr[$pos]; |
|
| 77 | 4 | $ret = self::ROMAN_NUMERALS[$pos][$n] . $ret; |
|
| 78 | } |
||
| 79 | } |
||
| 80 | 4 | return $ret; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
|
1 ignored issue
–
show
|
|||
| 84 | * @param $romanNumber |
||
|
1 ignored issue
–
show
|
|||
| 85 | * @return int|mixed |
||
|
1 ignored issue
–
show
|
|||
| 86 | */ |
||
| 87 | 1 | public static function roman2Dec($romanNumber) |
|
| 88 | { |
||
| 89 | 1 | if (is_numeric($romanNumber)) { |
|
| 90 | return 0; |
||
| 91 | } |
||
| 92 | |||
| 93 | 1 | $values = []; |
|
| 94 | // Convert the string to an array of roman values: |
||
| 95 | 1 | for ($i = 0; $i < strlen($romanNumber); ++$i) { |
|
| 96 | 1 | $char = strtoupper($romanNumber{$i}); |
|
| 97 | 1 | if (self::ROMAN_DIGITS[$char] !== null) { |
|
| 98 | 1 | $values[] = self::ROMAN_DIGITS[$char]; |
|
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | 1 | $sum = 0; |
|
| 103 | 1 | while ($current = current($values)) { |
|
| 104 | 1 | $next = next($values); |
|
| 105 | 1 | $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current; |
|
| 106 | } |
||
| 107 | |||
| 108 | // Return the value: |
||
| 109 | 1 | return $sum; |
|
| 110 | } |
||
| 111 | |||
| 112 | 2 | public static function isRomanNumber($str) |
|
| 113 | { |
||
| 114 | 2 | for ($i = 0; $i < strlen($str); ++$i) { |
|
| 115 | 2 | $char = strtoupper($str{$i}); |
|
| 116 | 2 | if (!in_array($char, array_keys(self::ROMAN_DIGITS))) { |
|
| 117 | 2 | return false; |
|
| 118 | } |
||
| 119 | } |
||
| 120 | 2 | return true; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
|
1 ignored issue
–
show
|
|||
| 124 | * @param $str |
||
|
1 ignored issue
–
show
|
|||
| 125 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 126 | */ |
||
| 127 | 1 | public static function evaluateStringPluralism($str) |
|
| 128 | { |
||
| 129 | 1 | $plural = 'single'; |
|
| 130 | 1 | if (!empty($str)) { |
|
| 131 | 1 | $ranges = preg_split("/[-–&,]/", $str); |
|
| 132 | 1 | if (count($ranges) > 1) { |
|
| 133 | |||
| 134 | 1 | $isRange = 1; |
|
| 135 | 1 | foreach ($ranges as $range) { |
|
| 136 | 1 | if (NumberHelper::isRomanNumber(trim($range)) || is_numeric(trim($range))) { |
|
| 137 | 1 | $isRange &= 1; |
|
| 138 | } |
||
| 139 | } |
||
| 140 | 1 | if ($isRange == 1) { |
|
| 141 | 1 | return 'multiple'; |
|
| 142 | } |
||
| 143 | } else { |
||
| 144 | 1 | if (is_numeric($str) || NumberHelper::isRomanNumber($str)) { |
|
| 145 | 1 | return 'single'; |
|
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | return $plural; |
||
| 150 | } |
||
| 151 | |||
| 152 | 48 | public static function extractNumber($string) |
|
| 158 | } |
||
| 159 | } |