Commit d12c8d58 by Vincent

#2524 updated as size method to include translation and verbose mode

parent 9427f4e7
...@@ -66,6 +66,16 @@ class Formatter extends Component ...@@ -66,6 +66,16 @@ class Formatter extends Component
* If not set, "," will be used. * If not set, "," will be used.
*/ */
public $thousandSeparator; public $thousandSeparator;
/**
* @var array the format used to format size (bytes). Three elements may be specified: "base", "decimals" and "decimalSeparator".
* They correspond to the base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte, defaults to 1024),
* the number of digits after the decimal point (defaults to 2) and the character displayed as the decimal point.
*/
public $sizeFormat=array(
'base'=>1024,
'decimals'=>2,
'decimalSeparator'=>null,
);
/** /**
* Initializes the component. * Initializes the component.
...@@ -406,27 +416,45 @@ class Formatter extends Component ...@@ -406,27 +416,45 @@ class Formatter extends Component
} }
/** /**
* Formats the value as file size with a unit representation * Formats the value in bytes as a size in human readable form.
* @param mixed $value the value to be formatted * @param integer $value value in bytes to be formatted
* @param integer $decimals the number of digits after the decimal point * @param boolean $verbose if full names should be used (e.g. bytes, kilobytes, ...).
* Defaults to false meaning that short names will be used (e.g. B, KB, ...).
* @return string the formatted result * @return string the formatted result
* @see decimalSeparator * @see sizeFormat
* @see thousandSeparator
*/ */
public function asSize($value, $decimals = 0) public function asSize($value, $verbose=false)
{ {
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$position = 0; $position = 0;
do { do {
if ($value < 1024) { if ($value < $this->sizeFormat['base']) {
return round($value, $decimals) . $units[$position]; break;
} }
$value = $value / 1024; $value = $value / $this->sizeFormat['base'];
$position++; $position++;
} while ($position < count($units)); } while ($position < count($units));
return number_format($value, $decimals) . Yii::t('yii', end($units)); $value = round($value, $this->sizeFormat['decimals']);
$formattedValue = isset($this->sizeFormat['decimalSeparator']) ? str_replace('.', $this->sizeFormat['decimalSeparator'], $value) : $value;
$params = ['n' => $formattedValue];
switch($position)
{
case 0:
return $verbose ? Yii::t('yii','{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params);
case 1:
return $verbose ? Yii::t('yii','{n, plural, =1{# kilobyte} other{# kilobytes}}', $params) : Yii::t('yii','{n} KB', $params);
case 2:
return $verbose ? Yii::t('yii','{n, plural, =1{# megabyte} other{# megabytes}}', $params) : Yii::t('yii','{n} MB', $params);
case 3:
return $verbose ? Yii::t('yii','{n, plural, =1{# gigabyte} other{# gigabytes}}', $params) : Yii::t('yii','{n} GB', $params);
case 4:
return $verbose ? Yii::t('yii','{n, plural, =1{# terabyte} other{# terabytes}}', $params) : Yii::t('yii','{n} TB', $params);
default:
return $verbose ? Yii::t('yii','{n, plural, =1{# petabyte} other{# petabytes}}', $params) : Yii::t('yii','{n} PB', $params);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment