Last active
August 29, 2015 14:09
-
-
Save dereckson/a37147ad5425544a2ed2 to your computer and use it in GitHub Desktop.
Determine a systematic element name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Represents a systematic element name | |
*/ | |
class SystematicElementName { | |
/** | |
* Gets the root matching the digit | |
* | |
* @param int $digit The digit | |
* @param mixed $nextDigit To respect elision rules, the next digit (as int) or null if this is the last digit (facultative) | |
* @return string The root | |
*/ | |
static public function getDigitRoot ($digit, $nextDigit = false) { | |
switch ((int)$digit) { | |
case 0: return "nil"; break; | |
case 1: return "un"; break; | |
case 2: return ($nextDigit === null) ? "b" : "bi"; break; | |
case 3: return ($nextDigit === null) ? "tr" : "tri"; break; | |
case 4: return "quad"; break; | |
case 5: return "pent"; break; | |
case 6: return "hex"; break; | |
case 7: return "sept"; break; | |
case 8: return "oct"; break; | |
case 9: return ($nextDigit === 0) ? "en" : "enn"; break; | |
default: throw new InvalidArgumentException("getDigitRoot: a digit were expected, got '$digit' instead."); | |
} | |
return $digit; | |
} | |
/** | |
* Gets the systematic name of the element having the specified number | |
* | |
* @param int $number the number of the element | |
* @return string the systematic name of the element | |
*/ | |
static public function getName ($number) { | |
$name = []; | |
$digits = str_split($number); | |
$n = count($digits); | |
for ($i = 0 ; $i < $n ; $i++) { | |
$nextDigit = ($i < $n - 1) ? (int)$digits[$i + 1] : null; | |
$name[] = self::getDigitRoot($digits[$i], $nextDigit); | |
} | |
$name[] = 'ium'; | |
return join($name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
L11 should be @param int $lastDigit mixed (int|null)