mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-11 06:28:40 +00:00
More type checks
This commit is contained in:
parent
c8d938591a
commit
42877f570d
|
|
@ -64,22 +64,26 @@ class Numeric
|
||||||
return $numberInput;
|
return $numberInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fromBase = str_split($fromBaseInput, 1);
|
$fromBase = \str_split($fromBaseInput, 1);
|
||||||
$toBase = str_split($toBaseInput, 1);
|
$toBase = \str_split($toBaseInput, 1);
|
||||||
$number = str_split($numberInput, 1);
|
$number = \str_split($numberInput, 1);
|
||||||
$fromLen = strlen($fromBaseInput);
|
$fromLen = \strlen($fromBaseInput);
|
||||||
$toLen = strlen($toBaseInput);
|
$toLen = \strlen($toBaseInput);
|
||||||
$numberLen = strlen($numberInput);
|
$numberLen = \strlen($numberInput);
|
||||||
$newOutput = '';
|
$newOutput = '';
|
||||||
|
|
||||||
|
if ($fromBase === false || $toBase === false || $number === false) {
|
||||||
|
throw new \Exception();
|
||||||
|
}
|
||||||
|
|
||||||
if ($toBaseInput === '0123456789') {
|
if ($toBaseInput === '0123456789') {
|
||||||
$newOutput = 0;
|
$newOutput = '0';
|
||||||
|
|
||||||
for ($i = 1; $i <= $numberLen; ++$i) {
|
for ($i = 1; $i <= $numberLen; ++$i) {
|
||||||
$newOutput = bcadd(
|
$newOutput = bcadd(
|
||||||
(string) $newOutput,
|
$newOutput,
|
||||||
bcmul(
|
bcmul(
|
||||||
(string) array_search($number[$i - 1], $fromBase),
|
(string) \array_search($number[$i - 1], $fromBase),
|
||||||
bcpow((string) $fromLen, (string) ($numberLen - $i))
|
bcpow((string) $fromLen, (string) ($numberLen - $i))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
@ -88,15 +92,15 @@ class Numeric
|
||||||
return $newOutput;
|
return $newOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
$base10 = $fromBaseInput != '0123456789' ? self::convertBase($numberInput, $fromBaseInput, '0123456789') : $numberInput;
|
$base10 = (int) ($fromBaseInput != '0123456789' ? self::convertBase($numberInput, $fromBaseInput, '0123456789') : $numberInput);
|
||||||
|
|
||||||
if ($base10 < strlen($toBaseInput)) {
|
if ($base10 < \strlen($toBaseInput)) {
|
||||||
return $toBase[$base10];
|
return $toBase[$base10];
|
||||||
}
|
}
|
||||||
|
|
||||||
while ($base10 !== '0') {
|
while ($base10 !== '0') {
|
||||||
$newOutput = $toBase[bcmod($base10, (string) $toLen)] . $newOutput;
|
$newOutput = $toBase[(int) bcmod((string) $base10, (string) $toLen)] . $newOutput;
|
||||||
$base10 = bcdiv($base10, (string) $toLen, 0);
|
$base10 = bcdiv((string) $base10, (string) $toLen, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $newOutput;
|
return $newOutput;
|
||||||
|
|
@ -146,7 +150,7 @@ class Numeric
|
||||||
foreach (self::ROMANS as $key => $value) {
|
foreach (self::ROMANS as $key => $value) {
|
||||||
while (strpos($roman, $key) === 0) {
|
while (strpos($roman, $key) === 0) {
|
||||||
$result += $value;
|
$result += $value;
|
||||||
$roman = substr($roman, strlen($key));
|
$roman = substr($roman, \strlen($key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -169,7 +173,7 @@ class Numeric
|
||||||
$alpha = '';
|
$alpha = '';
|
||||||
|
|
||||||
for ($i = 1; $number >= 0 && $i < 10; ++$i) {
|
for ($i = 1; $number >= 0 && $i < 10; ++$i) {
|
||||||
$alpha = chr(0x41 + ($number % pow(26, $i) / pow(26, $i - 1))) . $alpha;
|
$alpha = chr(0x41 + (int) ($number % pow(26, $i) / pow(26, $i - 1))) . $alpha;
|
||||||
$number -= pow(26, $i);
|
$number -= pow(26, $i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -188,7 +192,7 @@ class Numeric
|
||||||
public static function alphaToNumeric(string $alpha) : int
|
public static function alphaToNumeric(string $alpha) : int
|
||||||
{
|
{
|
||||||
$numeric = 0;
|
$numeric = 0;
|
||||||
$length = strlen($alpha);
|
$length = \strlen($alpha);
|
||||||
|
|
||||||
for ($i = 0; $i < $length; ++$i) {
|
for ($i = 0; $i < $length; ++$i) {
|
||||||
$numeric += pow(26, $i) * (ord($alpha[$length - $i - 1]) - 0x40);
|
$numeric += pow(26, $i) * (ord($alpha[$length - $i - 1]) - 0x40);
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ class ArrayRandomize
|
||||||
/**
|
/**
|
||||||
* Yates array shuffler.
|
* Yates array shuffler.
|
||||||
*
|
*
|
||||||
* @param array $arr Array to randomize
|
* @param array $arr Array to randomize. Array must NOT be associative
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*
|
*
|
||||||
|
|
@ -38,8 +38,8 @@ class ArrayRandomize
|
||||||
$shuffled = [];
|
$shuffled = [];
|
||||||
|
|
||||||
while (!empty($arr)) {
|
while (!empty($arr)) {
|
||||||
$rnd = array_rand($arr);
|
$rnd = (int) array_rand($arr);
|
||||||
$shuffled[] = $arr[$rnd];
|
$shuffled[] = $arr[$rnd] ?? null;
|
||||||
array_splice($arr, $rnd, 1);
|
array_splice($arr, $rnd, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user