From: Marcel Werk Date: Tue, 24 May 2016 21:17:44 +0000 (+0200) Subject: Moved short-unit implementation to string-util X-Git-Tag: 3.0.0_Beta_1~1640^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a15ec9be06984697e433142d912022ceb12e2558;p=GitHub%2FWoltLab%2FWCF.git Moved short-unit implementation to string-util --- diff --git a/wcfsetup/install/files/lib/system/template/plugin/ShortUnitModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/ShortUnitModifierTemplatePlugin.class.php index a2f0f56f17..9c71b1ba9f 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/ShortUnitModifierTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/ShortUnitModifierTemplatePlugin.class.php @@ -21,30 +21,6 @@ class ShortUnitModifierTemplatePlugin implements IModifierTemplatePlugin { * @inheritDoc */ public function execute($tagArgs, TemplateEngine $tplObj) { - $number = $tagArgs[0]; - $unitPrefix = ''; - - if ($number >= 1000000) { - $number /= 1000000; - if ($number > 10) { - $number = floor($number); - } - else { - $number = round($number, 1); - } - $unitPrefix = 'M'; - } - else if ($number >= 1000) { - $number /= 1000; - if ($number > 10) { - $number = floor($number); - } - else { - $number = round($number, 1); - } - $unitPrefix = 'k'; - } - - return StringUtil::formatNumeric($number) . $unitPrefix; + return StringUtil::getShortUnit($tagArgs[0]); } } diff --git a/wcfsetup/install/files/lib/util/StringUtil.class.php b/wcfsetup/install/files/lib/util/StringUtil.class.php index b18f1d784d..0ebeec32d8 100644 --- a/wcfsetup/install/files/lib/util/StringUtil.class.php +++ b/wcfsetup/install/files/lib/util/StringUtil.class.php @@ -728,6 +728,39 @@ final class StringUtil { return $result; } + /** + * Shortens numbers larger than 1000 by using unit prefixes. + * + * @param integer $number + * @return string + */ + public static function getShortUnit($number) { + $unitPrefix = ''; + + if ($number >= 1000000) { + $number /= 1000000; + if ($number > 10) { + $number = floor($number); + } + else { + $number = round($number, 1); + } + $unitPrefix = 'M'; + } + else if ($number >= 1000) { + $number /= 1000; + if ($number > 10) { + $number = floor($number); + } + else { + $number = round($number, 1); + } + $unitPrefix = 'k'; + } + + return self::formatNumeric($number) . $unitPrefix; + } + /** * Forbid creation of StringUtil objects. */