<?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;
+ }
}
<?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;
}