Moved short-unit implementation to string-util
authorMarcel Werk <burntime@woltlab.com>
Tue, 24 May 2016 21:17:44 +0000 (23:17 +0200)
committerMarcel Werk <burntime@woltlab.com>
Tue, 24 May 2016 21:17:44 +0000 (23:17 +0200)
wcfsetup/install/files/lib/system/template/plugin/ShortUnitModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/util/StringUtil.class.php

index a2f0f56f1767a9cc28efc94b1c2e591416e43403..9c71b1ba9fb35abc5f6c13800bfdf13cc80c037c 100644 (file)
@@ -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]);
        }
 }
index b18f1d784db5f05a6942ca20d41a873380ea5a6d..0ebeec32d8efb3314615ec473822f22cd1fe2e3e 100644 (file)
@@ -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.
         */