Incremental refactor Generator/Logging/Multisort

Incremental refactor Generator/Logging/Multisort
This commit is contained in:
Catalin Novgorodschi
2026-05-12 13:22:51 +03:00
parent 2bb0aa0c10
commit c8d9fae4b4
5 changed files with 331 additions and 175 deletions
+57 -36
View File
@@ -7,52 +7,73 @@
## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ##
## ##
## URLs: https://travianz.org ##
## https://github.com/Shadowss/TravianZ ##
#################################################################################
## Refactored: incremental cleanup (compat PHP 7+, readability, minor fixes) ##
#################################################################################
class multiSort {
function sorte($array)
class multiSort
{
/**
* Multi-key array sorter
* Usage: sorte($array, 'key1', true, 3, 'key2', false, 2, ...)
*/
public function sorte($array)
{
for($i = 1; $i < func_num_args(); $i += 3)
$args = func_get_args();
$array = $args[0];
// iterate key/order/type triplets
for ($i = 1; $i < count($args); $i += 3)
{
$key = func_get_arg($i);
$key = isset($args[$i]) ? $args[$i] : null;
$order = isset($args[$i + 1]) ? $args[$i + 1] : true; // true = ASC
$type = isset($args[$i + 2]) ? $args[$i + 2] : 0;
$order = true;
if($i + 1 < func_num_args())
$order = func_get_arg($i + 1);
if ($key === null) {
continue;
}
$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);
// comparator
$cmp = function ($a, $b) use ($key, $type, $order)
{
$va = isset($a[$key]) ? $a[$key] : null;
$vb = isset($b[$key]) ? $b[$key] : null;
$t = function($a, $b) use ($key, $type, $order)
{
switch($type)
{
case 1: // Case insensitive natural.
$result = strcasenatcmp($a[$key], $b[$key]);
break;
case 2: // Numeric.
$result = $a[$key] - $b[$key];
break;
case 3: // Case sensitive string.
$result = strcmp($a[$key], $b[$key]);
break;
case 4: // Case insensitive string.
$result = strcasecmp($a[$key], $b[$key]);
break;
default: // Case sensitive natural.
$result = strnatcmp($a[$key], $b[ $key]);
break;
}
return $result*($order ? 1 : -1);
switch ($type)
{
case 1: // Case insensitive natural
$result = strnatcasecmp($va, $vb);
break;
case 2: // Numeric
$result = ($va == $vb) ? 0 : (($va < $vb) ? -1 : 1);
break;
case 3: // Case sensitive string
$result = strcmp((string)$va, (string)$vb);
break;
case 4: // Case insensitive string
$result = strcasecmp((string)$va, (string)$vb);
break;
default: // Case sensitive natural
$result = strnatcmp((string)$va, (string)$vb);
break;
}
return $order ? $result : -$result;
};
usort($array, $t);
usort($array, $cmp);
}
return $array;
}
}
};
$multisort = new multiSort;
?>
$multisort = new multiSort();
?>