]> _ Git - cubist_util.git/commitdiff
wip #5393
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 16 Aug 2022 11:12:41 +0000 (13:12 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 16 Aug 2022 11:12:41 +0000 (13:12 +0200)
src/Math.php
src/ObjectUtil.php

index 357326e3e0e30e9cc16ae36b8798a891dcf6f86d..571254f50d8ca3202b5773cd060c61dde61876b4 100644 (file)
 <?php
+
 namespace Cubist\Util;
-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;
-       }
-
-       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;
+    }
 
 }
index 02056f9e640016eea9e377be035b73268b163fbb..4ea4d825a83a84cff9169306f39f39649b4b63cc 100644 (file)
@@ -1,44 +1,53 @@
 <?php
+
 namespace Cubist\Util;
-class ObjectUtil {
+class ObjectUtil
+{
 
-       protected $_orderOnProperty = null;
-       protected $_orderOnIsMethod = false;
+    protected $_orderOnProperty = null;
+    protected $_orderOnIsMethod = false;
 
     /**
      * @param $o
      * @return array
      */
-       public static function toArray($o) {
-               if (is_array($o)) {
-                       return $o;
-               }
-               if (is_object($o)) {
-                       return get_object_vars($o);
-               }
-       }
-
-       public static function cloneObject($o) {
-               return unserialize(serialize($o));
-       }
-
-       /**
-        * @param $o mixed
-        * @return object \stdClass
-        */
-       public static function asObject($o) {
-               if ($o instanceof \stdClass) {
-                       return $o;
-               }
-               return (object)$o;
-       }
-
-    public static function safeUnserialize($str)
+    public static function toArray($o)
+    {
+        if (is_array($o)) {
+            return $o;
+        }
+        if (is_object($o)) {
+            return get_object_vars($o);
+        }
+    }
+
+    public static function cloneObject($o)
     {
-        $class = 'stdClass';
-        $str = preg_replace('/^O:\d+:"[^"]++"/', 'O:' . strlen($class) . ':"' . $class . '"', $str);
+        return unserialize(serialize($o));
+    }
+
+    /**
+     * @param $o mixed
+     * @return object \stdClass
+     */
+    public static function asObject($o)
+    {
+        if ($o instanceof \stdClass) {
+            return $o;
+        }
+        return (object)$o;
+    }
+
+    public static function safeUnserialize($str, $default = false)
+    {
+        $str = preg_replace('/^O:\d+:"[^"]++"/', 'O:8:"stdClass"', $str);
         $str = str_replace("s:8:\"\0*\0datas\"", 's:5:"datas"', $str);
-        return unserialize($str);
+
+        $res = unserialize($str);
+        if (!$res) {
+            return $default;
+        }
+        return $res;
     }