2013-11-10 03:07:45 +01:00
|
|
|
<?php
|
|
|
|
|
2013-11-10 17:38:54 +01:00
|
|
|
namespace ManiaControl;
|
|
|
|
|
2013-11-10 03:07:45 +01:00
|
|
|
/**
|
2013-11-12 16:08:22 +01:00
|
|
|
* Class offering methods to format texts and values
|
2013-11-10 03:07:45 +01:00
|
|
|
*
|
2013-11-12 16:08:22 +01:00
|
|
|
* @author steeffeen & kremsy
|
2013-11-10 03:07:45 +01:00
|
|
|
*/
|
2013-11-12 16:08:22 +01:00
|
|
|
abstract class Formatter {
|
2013-11-10 03:07:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats the given time (milliseconds)
|
|
|
|
*
|
|
|
|
* @param int $time
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function formatTime($time) {
|
|
|
|
if (!is_int($time)) {
|
|
|
|
$time = (int) $time;
|
|
|
|
}
|
|
|
|
$milliseconds = $time % 1000;
|
|
|
|
$seconds = floor($time / 1000);
|
|
|
|
$minutes = floor($seconds / 60);
|
|
|
|
$hours = floor($minutes / 60);
|
|
|
|
$minutes -= $hours * 60;
|
|
|
|
$seconds -= $hours * 60 + $minutes * 60;
|
|
|
|
$format = ($hours > 0 ? $hours . ':' : '');
|
|
|
|
$format .= ($hours > 0 && $minutes < 10 ? '0' : '') . $minutes . ':';
|
|
|
|
$format .= ($seconds < 10 ? '0' : '') . $seconds . ':';
|
|
|
|
$format .= ($milliseconds < 100 ? '0' : '') . ($milliseconds < 10 ? '0' : '') . $milliseconds;
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats the given time (seconds) to hh:mm:ss
|
|
|
|
*
|
2013-11-12 19:33:25 +01:00
|
|
|
* @param int $seconds
|
2013-11-10 03:07:45 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-12 19:33:25 +01:00
|
|
|
public static function formatTimeH($seconds) {
|
2013-11-10 03:07:45 +01:00
|
|
|
return gmdate("H:i:s", $seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given time (seconds) to mysql timestamp
|
|
|
|
*
|
2013-11-12 19:33:25 +01:00
|
|
|
* @param int $seconds
|
2013-11-10 03:07:45 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-11-12 19:33:25 +01:00
|
|
|
public static function formatTimestamp($seconds) {
|
|
|
|
return date("Y-m-d H:i:s", $seconds);
|
2013-11-10 03:07:45 +01:00
|
|
|
}
|
2013-11-12 16:08:22 +01:00
|
|
|
|
2013-12-24 13:27:59 +01:00
|
|
|
/**
|
|
|
|
* Strip all codes except colors from the string
|
|
|
|
*/
|
2013-12-26 11:25:10 +01:00
|
|
|
public static function stripCodesWithoutColors($string) { //Todo don't remove $i, $s
|
2013-12-24 13:27:59 +01:00
|
|
|
$string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
|
|
|
|
$string = self::stripLinks($string);
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2013-11-12 16:08:22 +01:00
|
|
|
/**
|
|
|
|
* Strip $codes from the string
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function stripCodes($string) {
|
|
|
|
$string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
|
|
|
|
$string = self::stripLinks($string);
|
2013-12-26 11:25:10 +01:00
|
|
|
$string = self::stripColors($string);
|
2013-11-12 16:08:22 +01:00
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove link codes from the string
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function stripLinks($string) {
|
|
|
|
return preg_replace('/(?<!\$)((?:\$\$)*)\$[hlp](?:\[.*?\])?(.*?)(?:\$[hlp]|(\$z)|$)/iu', '$1$2$3', $string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove colors from the string
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static function stripColors($string) {
|
|
|
|
return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
|
|
|
|
}
|
2013-11-10 03:07:45 +01:00
|
|
|
}
|