From 24b02ed2377f0bc93e30bf9d16ba3a74885fef4f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 16 Aug 2022 13:12:41 +0200 Subject: [PATCH] wip #5393 --- src/Math.php | 290 ++++++++++++++++++++++----------------------- src/ObjectUtil.php | 71 ++++++----- 2 files changed, 180 insertions(+), 181 deletions(-) diff --git a/src/Math.php b/src/Math.php index 357326e..571254f 100644 --- a/src/Math.php +++ b/src/Math.php @@ -1,154 +1,144 @@ $max) { - $s = $min; - $min = $max; - $max = $s; - } - - return max(min($val, 100), 0); - } - - public static function round($val, $precision = 0) { - if (is_int($precision)) { - return round($val, $precision); - } - return round($val * $precision) / $precision; - } - - public static function fill($val, $nb, $fill = '0') { - return str_pad((string) $val, $nb, $fill, STR_PAD_LEFT); - } - - public static function isOdd($i) { - return abs($i) % 2 == 1; - } - - public static function toRoman($i, $upper = false) { - $entier = intval($i); - if ($entier < 1 || $entier > 9999) { - return ''; - } - - $rom[3] = array("M", "", "", ""); - $rom[2] = array("C", "CD", "D", "CM"); - $rom[1] = array("X", "XL", "L", "XC"); - $rom[0] = array("I", "IV", "V", "IX"); - - $i = 1; - $n = 0; - $romain = ""; - - for ($n = 0; $n < 4; $n++) { - $chiffre = intval(($entier % ($i * 10)) / $i); - if ($n < 3) { - if ($chiffre < 4) { - while ($chiffre > 0) { - $romain = $rom[$n][0] . $romain; - $chiffre--; - } - } elseif ($chiffre == 4) - $romain = $rom[$n][1] . $romain; - elseif ($chiffre < 9) { - while ($chiffre > 5) { - $romain = $rom[$n][0] . $romain; - $chiffre--; - } - $romain = $rom[$n][2] . $romain; - } else - $romain = $rom[$n][3] . $romain; - } else { - while ($chiffre > 0) { - $romain = $rom[$n][0] . $romain; - $chiffre--; - } - } - $i = $i * 10; - } - if (!$upper) { - $romain = strtolower($romain); - } - return $romain; - } - - public static function toPDFLetter($i, $upper = false) { - $dividende = floor($i / 26) + 1; - $reste = ($i % 26) + 1; - - $char = chr(64 + $reste); - if (!$upper) { - $char = strtolower($char); - } - - return str_repeat($char, $dividende); - } - - public static function moyenne() { - $args = func_get_args(); - if (!count($args)) { - return 0; - } - if (is_array($args[0])) { - $args = $args[0]; - if (!count($args)) { - return 0; - } - } - return array_sum($args) / count($args); - } - - // # http://www.php.net/manual/fr/function.stats-standard-deviation.php#66447 - // The average function can be use independantly but the deviation function uses the average function. - public static function ecart_type($array) { - if (!count($array)) { - return 0; - } - $avg = self::moyenne($array); - $variance = 0; - foreach ($array as $value) { - $variance += pow($value - $avg, 2); - } - $deviation = sqrt($variance / (count($array))); - return $deviation; - } - - public static function is_int($val) { - if (is_int($val)) { - return true; - } - if (is_string($val)) { - $v = (string) (int) $val; - if ($val == $v) { - return true; - } - } - return false; - } - - public static function compare($x, $y, $tolerance = 1) { - $diff = $x / $y; - if ($diff < 1) { - $diff = 1 / $diff; - } - if ($tolerance < 1) { - $tolerance = 1 / $tolerance; - } - - return ($diff <= $tolerance); - } - - /** - * - * @param int $n - * @return string - */ - public static function num2alpha($n) { - for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) - $r = chr($n % 26 + 0x41) . $r; - return $r; - } +class Math +{ + + public static function interval($val, $min, $max) + { + if ($min > $max) { + $s = $min; + $min = $max; + $max = $s; + } + + return max(min($val, 100), 0); + } + + public static function round($val, $precision = 0) + { + if (is_int($precision)) { + return round($val, $precision); + } + return round($val * $precision) / $precision; + } + + public static function fill($val, $nb, $fill = '0') + { + return str_pad((string)$val, $nb, $fill, STR_PAD_LEFT); + } + + public static function isOdd($i) + { + return abs($i) % 2 == 1; + } + + /** + * @param $i integer + * @param $upper boolean + * @return string + */ + public static function toRoman($i, $upper = false) + { + $number = intval($i); + $map = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1); + $returnValue = ''; + while ($number > 0) { + foreach ($map as $roman => $int) { + if ($number >= $int) { + $number -= $int; + $returnValue .= $roman; + break; + } + } + } + if (!$upper) { + $returnValue = strtolower($returnValue); + } + return $returnValue; + } + + public static function toPDFLetter($i, $upper = false) + { + $dividende = floor($i / 26) + 1; + $reste = ($i % 26) + 1; + + $char = chr(64 + $reste); + if (!$upper) { + $char = strtolower($char); + } + + return str_repeat($char, $dividende); + } + + public static function moyenne() + { + $args = func_get_args(); + if (!count($args)) { + return 0; + } + if (is_array($args[0])) { + $args = $args[0]; + if (!count($args)) { + return 0; + } + } + return array_sum($args) / count($args); + } + + // # http://www.php.net/manual/fr/function.stats-standard-deviation.php#66447 + // The average function can be use independantly but the deviation function uses the average function. + public static function ecart_type($array) + { + if (!count($array)) { + return 0; + } + $avg = self::moyenne($array); + $variance = 0; + foreach ($array as $value) { + $variance += pow($value - $avg, 2); + } + $deviation = sqrt($variance / (count($array))); + return $deviation; + } + + public static function is_int($val) + { + if (is_int($val)) { + return true; + } + if (is_string($val)) { + $v = (string)(int)$val; + if ($val == $v) { + return true; + } + } + return false; + } + + public static function compare($x, $y, $tolerance = 1) + { + $diff = $x / $y; + if ($diff < 1) { + $diff = 1 / $diff; + } + if ($tolerance < 1) { + $tolerance = 1 / $tolerance; + } + + return ($diff <= $tolerance); + } + + /** + * + * @param int $n + * @return string + */ + public static function num2alpha($n) + { + for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) + $r = chr($n % 26 + 0x41) . $r; + return $r; + } } diff --git a/src/ObjectUtil.php b/src/ObjectUtil.php index 02056f9..4ea4d82 100644 --- a/src/ObjectUtil.php +++ b/src/ObjectUtil.php @@ -1,44 +1,53 @@