From: Sam Reed Date: Sun, 8 Jan 2012 20:40:13 +0000 (+0000) Subject: Followup r108363 for bug 33571 X-Git-Tag: 1.31.0-rc.0~25422 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=e6f1ac4f5bd3e98e1b4d92846403476c40c71119;p=lhc%2Fweb%2Fwiklou.git Followup r108363 for bug 33571 Refactor out duplicate code --- diff --git a/languages/Language.php b/languages/Language.php index fe9f1b83a0..f455e99e82 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3816,29 +3816,41 @@ class Language { * @return string */ function formatBitrate( $bps ) { - if ( $bps <= 0 ) { - return str_replace( '$1', $this->formatNum( $bps ), $this->getMessageFromDB( 'bitrate-bits' ) ); + return $this->formatComputingNumbers( $bps, 1000, "bitrate-$1bits" ); + } + + /** + * @param $size int Size of the unit + * @param $boundary int Size boundary (1000, or 1024 in most cases) + * @param $messageKey string Message key to be uesd + * @return string + */ + function formatComputingNumbers( $size, $boundary, $messageKey ) { + if ( $size <= 0 ) { + return str_replace( '$1', $this->formatNum( $size ), + $this->getMessageFromDB( str_replace( '$1', '', $messageKey ) ) + ); } - $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' ); + $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' ); $index = 0; - $maxIndex = count( $units ) - 1; - while ( $bps >= 1000 && $index < $maxIndex ) { + $maxIndex = count( $sizes ) - 1; + while ( $size >= $boundary && $index < $maxIndex ) { $index++; - $bps /= 1000; + $size /= $boundary; } - // For small units no decimal places necessary + // For small sizes no decimal places necessary $round = 0; if ( $index > 1 ) { // For MB and bigger two decimal places are smarter $round = 2; } - $msg = "bitrate-{$units[$index]}bits"; + $msg = str_replace( '$1', $messageKey, $sizes[$index] ); - $bps = round( $bps, $round ); + $size = round( $size, $round ); $text = $this->getMessageFromDB( $msg ); - return str_replace( '$1', $this->formatNum( $bps ), $text ); + return str_replace( '$1', $this->formatNum( $size ), $text ); } /** @@ -3849,26 +3861,7 @@ class Language { * @return string Plain text (not HTML) */ function formatSize( $size ) { - $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' ); - $index = 0; - - $maxIndex = count( $sizes ) - 1; - while ( $size >= 1024 && $index < $maxIndex ) { - $index++; - $size /= 1024; - } - - // For small sizes no decimal places necessary - $round = 0; - if ( $index > 1 ) { - // For MB and bigger two decimal places are smarter - $round = 2; - } - $msg = "size-{$sizes[$index]}bytes"; - - $size = round( $size, $round ); - $text = $this->getMessageFromDB( $msg ); - return str_replace( '$1', $this->formatNum( $size ), $text ); + return $this->formatComputingNumbers( $size, 1024, "size-$1bytes" ); } /**