*/ /** Convert a format to another one */ class convert { /** Convert Date received in one format to another. * If the provided string is not corresponding to the format, generate an * exception or return the original string * Format used http://php.net/manual/en/datetime.createfromformat.php * Do not accept the locale ! The language of the dates is always in english * @param string $inputDate The date to modify * @param string $inputFormat The input format of the date * @param string $outputFormat The output format of the date * @param boolean|null $exception If set, generate an exception if the * provided date is invalid * @return string */ public static function convertDate ($inputDate, $inputFormat, $outputFormat, $exception = true) // {{{ { if (! is_string ($inputDate)) throw new \Exception ("The date to convert is not a string", 500); if (! is_string ($inputFormat)) throw new \Exception ("The convert input format is not a string", 500); if (! is_string ($outputFormat)) throw new \Exception ("The convert output format is not a string", 500); $date = \DateTime::CreateFromFormat ($inputFormat, $inputDate); if ($date === false) { if ($exception === true) throw new \Exception ( "Invalid date provided or not matching the format", 500); return $inputDate; } $errors = $date->getLastErrors(); if ($errors["warning_count"] > 0 || $errors["error_count"] > 0) { if ($exception === true) throw new \Exception ( "Invalid date provided or not matching the format", 500); return $inputDate; } return $date->format ($outputFormat); } // }}} /** Convert the first char to capital and the rest of the sentence in * lowercase (like ucfirst, but UTF8 compliant) * @param string $str The string to convert */ public static function ucfirst ($str) // {{{ { if (! function_exists ("mb_strtoupper")) throw new \Exception ("PHP don't have the MB Support. Please add it !", 500); $a = mb_strtoupper (mb_substr ($str, 0, 1, 'UTF-8'), 'UTF-8'); return $a . mb_substr ($str, 1, null, 'UTF-8'); } // }}} /** Convert the first char of each word of a sentence to capital. The word * delimiter can be provided. * The sentence is in UTF-8. * The sentence is converted to lowercase before doing the action (in * contrary of the original PHP function) * @param string $str The string to convert * @param string $delimiters The delimiters (by default " \t\r\n\f\v") * @return string */ public static function ucwords ($str, $delimiters = " \t\r\n\f\v") // {{{ { if (! function_exists ("mb_strtolower")) throw new \Exception ("PHP don't have the MB Support. Please add it !", 500); $str = mb_strtolower ($str, "utf-8"); $res = ""; foreach (preg_split ("#([".preg_quote ($delimiters)."]+)#", $str, -1, PREG_SPLIT_DELIM_CAPTURE) as $tok) { $res .= \convert::ucfirst ($tok); } return $res; } // }}} /** Convert the provided float to human readable format * Example : 1440000 => 1.44MB * @param float|integer $value The number to convert * @param integer|null $decimals The number of decimal (2 by default) * @param integer|null $power (1000 by default or 1024) * @param string|null $unit The Unit displayed after the multiplier (B for * Bytes by default) */ public static function humanSize ($value, $decimals = 2, $power = 1000, $unit = "B") // {{{ { if (! is_integer ($value) && ! is_float ($value)) throw new \Exception ("convert::humanSize value not numerical : ". gettype ($value), 500); if (! is_integer ($decimals)) throw new \Exception ("convert::humanSize decimal not integer : ". gettype ($decimals), 500); if ($decimals < 0) throw new \Exception ("convert::humanSize decimal value negative", 500); if ($power !== 1000 && $power !== 1024) throw new \Exception ("convert::humanSize power value !== 1000 and 1024". " : ".var_export ($power, true), 500); $size = array ( -8 => 'y', // yocto -7 => 'z', // zepto -6 => 'a', // atto -5 => 'f', // femto -4 => 'p', // pico -3 => 'n', // nano -2 => 'u', // micro -1 => 'm', // milli 0 => '', 1 => 'k', // kilo 2 => 'M', // mega 3 => 'G', // giga 4 => 'T', // tera 5 => 'P', // peta 6 => 'E', // exa 7 => 'Z', // zetta 8 => 'Y');// yotta for ($factor = 8 ; $factor >= -8 ; $factor--) { if (abs ($value) >= pow ($power, $factor)) break; } if ($factor > 0) $display = $value / pow ($power, $factor); elseif ($factor < 0) $display = $value * pow ($power, -1 * $factor); else $display = $value; return sprintf ("%.${decimals}f", $display).$size[$factor].$unit; } // }}} }