Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
45 / 45 |
| FormatIt | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
22 | |
100.00% |
45 / 45 |
| numberClean | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| number_format | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
| bytes2units | |
100.00% |
1 / 1 |
5 | |
100.00% |
13 / 13 |
|||
| units2bytes | |
100.00% |
1 / 1 |
8 | |
100.00% |
18 / 18 |
|||
| milliSecondsFormat | |
100.00% |
1 / 1 |
5 | |
100.00% |
10 / 10 |
|||
| <?php | |
| namespace ia\Util; | |
| class FormatIt { | |
| public static function numberClean($s) { | |
| // scientific notation not accepted | |
| return preg_replace('/[^\d\.\-\+]/mS', '', Str::strim($s)); | |
| } | |
| public static function number_format($number, $decimals = 0, $dec_point = '.', $thousand_sep = ',') { | |
| if(!is_numeric($number)) { | |
| return $number; | |
| } | |
| return number_format($number, $decimals, $decimals === 0 ? '' : $dec_point, $thousand_sep) ; | |
| } | |
| /** | |
| * Format bytes into readable units Kb, Mb,.. | |
| * | |
| * @param int $bytes | |
| * @return string | |
| */ | |
| public static function bytes2units($bytes) { | |
| if(empty($bytes) || !is_numeric($bytes)) | |
| return $bytes; | |
| if($bytes < 0) { | |
| $signo = '-'; | |
| $bytes *= -1; | |
| } else | |
| $signo = ''; | |
| if($bytes < 1024) { | |
| $decs = 0; | |
| $punto = ''; | |
| } else { | |
| $decs = 2; | |
| $punto = '.'; | |
| } | |
| $unit =['b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb']; | |
| return $signo.number_format($bytes/pow(1024,($i=floor(log($bytes,1024)))),$decs,$punto,',').' '.$unit[$i]; | |
| } | |
| /** | |
| * Transform units (1K) to bytes 1024 | |
| * | |
| * @param string $num 1K o 2048 | |
| * @return string number of bytes | |
| */ | |
| public static function units2bytes($num) { | |
| if(empty($num)) { | |
| return 0; | |
| } | |
| $units = ''; | |
| $n = str_replace(' ', '', $num); //TODO all spaces ie unicode spaces? | |
| $len = mb_strlen($n,'UTF-8'); | |
| for($i = 1; $i <= $len; $i++) { | |
| $u = mb_substr($n, -$i, 1, 'UTF-8'); | |
| if(ctype_digit($u) || $u === '.') { | |
| break; | |
| } | |
| $units = $u.$units; | |
| } | |
| $units = mb_convert_case($units, MB_CASE_UPPER); | |
| $val = str_replace(',', '', mb_substr($num,0,$len-$i+1,'UTF-8')); | |
| if($units === '' || $units === 'B') { | |
| return $val; | |
| } | |
| static $byteUnits = array( | |
| 'K'=>1024, 'KB' => 1024, | |
| 'M'=>1048576, 'MB' => 1048576, | |
| 'G'=>1073741824, 'GB' => 1073741824, | |
| 'T'=>1099511627776, 'TB' => 1099511627776 | |
| ); | |
| if(isset($byteUnits[$units])) | |
| return $val * $byteUnits[$units]; | |
| return false; | |
| } | |
| /** | |
| * Formats milliseconds strings with time units | |
| * | |
| * @param number $t0 the number of milliseconds to format | |
| * @return string formatted lapsed time in milliseconds | |
| */ | |
| public static function milliSecondsFormat($t0) { | |
| if(empty($t0) || !is_numeric($t0)) { | |
| $t0 = 0; | |
| } | |
| if($t0 < 0) { | |
| $t0 = -1 * $t0; | |
| } | |
| if($t0 >= 60*60*24) { | |
| return ">= 1 day not supported"; //@TODO milliSecondsFormat support 1 day adnd more | |
| } | |
| return Str::strim( str_replace( | |
| array('0 h','00 min','00 sec',' 01',' 02',' 03',' 04',' 05',' 06',' 07',' 08',' 09') | |
| ,array('','','' ,' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9') | |
| ,gmDate(' G \h i \m\i\n s \s\e\c '.(round($t0-floor($t0),3)*1000).' \m\s',floor($t0) ) | |
| )); | |
| } | |
| } |