| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Arabic |
| 10 |
{ |
| 11 |
use ArrayEnabled; |
| 12 |
|
| 13 |
private const ROMAN_LOOKUP = [ |
| 14 |
'M' => 1000, |
| 15 |
'D' => 500, |
| 16 |
'C' => 100, |
| 17 |
'L' => 50, |
| 18 |
'X' => 10, |
| 19 |
'V' => 5, |
| 20 |
'I' => 1, |
| 21 |
]; |
| 22 |
|
| 23 |
/** |
| 24 |
* Recursively calculate the arabic value of a roman numeral. |
| 25 |
* |
| 26 |
* @param string[] $roman |
| 27 |
*/ |
| 28 |
private static function calculateArabic(array $roman, int &$sum = 0, int $subtract = 0): int |
| 29 |
{ |
| 30 |
$numeral = array_shift($roman); |
| 31 |
if (!isset(self::ROMAN_LOOKUP[$numeral])) { |
| 32 |
throw new Exception('Invalid character detected'); |
| 33 |
} |
| 34 |
|
| 35 |
$arabic = self::ROMAN_LOOKUP[$numeral]; |
| 36 |
if (count($roman) > 0 && isset(self::ROMAN_LOOKUP[$roman[0]]) && $arabic < self::ROMAN_LOOKUP[$roman[0]]) { |
| 37 |
$subtract += $arabic; |
| 38 |
} else { |
| 39 |
$sum += ($arabic - $subtract); |
| 40 |
$subtract = 0; |
| 41 |
} |
| 42 |
|
| 43 |
if (count($roman) > 0) { |
| 44 |
self::calculateArabic($roman, $sum, $subtract); |
| 45 |
} |
| 46 |
|
| 47 |
return $sum; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* ARABIC. |
| 52 |
* |
| 53 |
* Converts a Roman numeral to an Arabic numeral. |
| 54 |
* |
| 55 |
* Excel Function: |
| 56 |
* ARABIC(text) |
| 57 |
* |
| 58 |
* @param mixed $roman Should be a string, or can be an array of strings |
| 59 |
* |
| 60 |
* @return array<mixed>|int|string the arabic numeral contrived from the roman numeral |
| 61 |
* If an array of numbers is passed as the argument, then the returned result will also be an array |
| 62 |
* with the same dimensions |
| 63 |
*/ |
| 64 |
public static function evaluate($roman) |
| 65 |
{ |
| 66 |
if (is_array($roman)) { |
| 67 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $roman); |
| 68 |
} |
| 69 |
|
| 70 |
// An empty string should return 0 |
| 71 |
$roman = substr(trim(strtoupper((string) $roman)), 0, 255); |
| 72 |
if ($roman === '') { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
// Convert the roman numeral to an arabic number |
| 77 |
$negativeNumber = $roman[0] === '-'; |
| 78 |
if ($negativeNumber) { |
| 79 |
$roman = trim((string) substr($roman, 1)); |
| 80 |
if ($roman === '') { |
| 81 |
return ExcelError::NAN(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
try { |
| 86 |
$arabic = self::calculateArabic(mb_str_split($roman, 1, 'UTF-8')); |
| 87 |
} catch (Exception $exception) { |
| 88 |
return ExcelError::VALUE(); // Invalid character detected |
| 89 |
} |
| 90 |
|
| 91 |
if ($negativeNumber) { |
| 92 |
$arabic *= -1; // The number should be negative |
| 93 |
} |
| 94 |
|
| 95 |
return $arabic; |
| 96 |
} |
| 97 |
} |
| 98 |
|