Skip to content

Instantly share code, notes, and snippets.

@dereckson
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dereckson/a37147ad5425544a2ed2 to your computer and use it in GitHub Desktop.
Save dereckson/a37147ad5425544a2ed2 to your computer and use it in GitHub Desktop.
Determine a systematic element name
<?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);
}
}
@dereckson
Copy link
Author

L11 should be @param int $lastDigit mixed (int|null)

@dereckson
Copy link
Author

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment