From 9e231eb8c88ad9b201217902418c61f8cfbdbc2c Mon Sep 17 00:00:00 2001 From: Vincent Gabriel Date: Sun, 23 Feb 2014 16:23:05 -0800 Subject: [PATCH 1/6] added asSize method to format file size #2524 --- framework/base/Formatter.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index d079b5b13d..fcec1d1ce0 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -404,4 +404,30 @@ class Formatter extends Component $ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ','; return number_format($value, $decimals, $ds, $ts); } + + /** + * Formats the value as file size with a unit representation + * @param mixed $value the value to be formatted + * @param integer $decimals the number of digits after the decimal point + * @return string the formatted result + * @see decimalSeparator + * @see thousandSeparator + * @see asNumber + */ + public function asSize($value, $decimals = 0) + { + $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; + $position = 0; + + do { + if ($value < 1024) { + return round($value, $decimals) . $units[$position]; + } + + $value = $value / 1024; + $position++; + } while ($position < count($units)); + + return $this->asNumber($value, $decimals) . Yii::t('yii', end($units)); + } } From 9427f4e76b200ba74b487860cb7a046ccea62717 Mon Sep 17 00:00:00 2001 From: Vincent Gabriel Date: Sun, 23 Feb 2014 16:36:17 -0800 Subject: [PATCH 2/6] no need for as number #2524 --- framework/base/Formatter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index fcec1d1ce0..569b4236db 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -412,7 +412,6 @@ class Formatter extends Component * @return string the formatted result * @see decimalSeparator * @see thousandSeparator - * @see asNumber */ public function asSize($value, $decimals = 0) { @@ -428,6 +427,6 @@ class Formatter extends Component $position++; } while ($position < count($units)); - return $this->asNumber($value, $decimals) . Yii::t('yii', end($units)); + return number_format($value, $decimals) . Yii::t('yii', end($units)); } } From d12c8d58201c0eaf63db890d1a742122e38218b2 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 24 Feb 2014 10:57:36 -0800 Subject: [PATCH 3/6] #2524 updated as size method to include translation and verbose mode --- framework/base/Formatter.php | 48 ++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 569b4236db..09d17bb7e7 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -66,6 +66,16 @@ class Formatter extends Component * If not set, "," will be used. */ 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. @@ -406,27 +416,45 @@ class Formatter extends Component } /** - * Formats the value as file size with a unit representation - * @param mixed $value the value to be formatted - * @param integer $decimals the number of digits after the decimal point + * Formats the value in bytes as a size in human readable form. + * @param integer $value value in bytes to be formatted + * @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 - * @see decimalSeparator - * @see thousandSeparator + * @see sizeFormat */ - public function asSize($value, $decimals = 0) + public function asSize($value, $verbose=false) { $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; $position = 0; do { - if ($value < 1024) { - return round($value, $decimals) . $units[$position]; + if ($value < $this->sizeFormat['base']) { + break; } - $value = $value / 1024; + $value = $value / $this->sizeFormat['base']; $position++; } 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); + } } } From 3cb97fb8c836b371923ffb6fc2441da91e360da7 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 24 Feb 2014 12:01:53 -0800 Subject: [PATCH 4/6] updated formatting --- framework/base/Formatter.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 09d17bb7e7..fce39a23b9 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -426,22 +426,22 @@ class Formatter extends Component public function asSize($value, $verbose=false) { $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; - $position = 0; + $position = 0; - do { - if ($value < $this->sizeFormat['base']) { - break; - } + do { + if ($value < $this->sizeFormat['base']) { + break; + } - $value = $value / $this->sizeFormat['base']; - $position++; - } while ($position < count($units)); + $value = $value / $this->sizeFormat['base']; + $position++; + } while ($position < count($units)); - $value = round($value, $this->sizeFormat['decimals']); + $value = round($value, $this->sizeFormat['decimals']); $formattedValue = isset($this->sizeFormat['decimalSeparator']) ? str_replace('.', $this->sizeFormat['decimalSeparator'], $value) : $value; $params = ['n' => $formattedValue]; - switch($position) + switch($position) { case 0: return $verbose ? Yii::t('yii','{n, plural, =1{# byte} other{# bytes}}', $params) : Yii::t('yii', '{n} B', $params); From 9f6cc541549a6e30c4c18bc99a98ee97d90655c8 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 24 Feb 2014 13:26:55 -0800 Subject: [PATCH 5/6] updated code according to code style and removed unused property --- framework/base/Formatter.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index fce39a23b9..dfbd84b925 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -71,11 +71,11 @@ class Formatter extends Component * 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, - ); + public $sizeFormat = [ + 'base' => 1024, + 'decimals' => 2, + 'decimalSeparator' => null, + ]; /** * Initializes the component. @@ -425,7 +425,6 @@ class Formatter extends Component */ public function asSize($value, $verbose=false) { - $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; $position = 0; do { @@ -435,7 +434,7 @@ class Formatter extends Component $value = $value / $this->sizeFormat['base']; $position++; - } while ($position < count($units)); + } while ($position < 6); $value = round($value, $this->sizeFormat['decimals']); $formattedValue = isset($this->sizeFormat['decimalSeparator']) ? str_replace('.', $this->sizeFormat['decimalSeparator'], $value) : $value; From 84a48ac93e9f0004d29b8db69d74093d89a9fda4 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 24 Feb 2014 13:45:44 -0800 Subject: [PATCH 6/6] updated changelog --- framework/CHANGELOG.md | 1 + framework/base/Formatter.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 27a79692b1..2b0d3547e5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -122,6 +122,7 @@ Yii Framework 2 Change Log - Enh #2490: `yii\db\Query::count()` and other query scalar methods now properly handle queries with GROUP BY clause (qiangxue) - Enh #2491: Added support for using the same base class name of search model and data model in Gii (qiangxue) - Enh #2499: Added ability to downgrade migrations by their absolute apply time (resurtm, gorcer) +- Enh #2525: Added support for formatting file sizes with `yii\base\Formatter` (VinceG) - Enh: Added support for using arrays as option values for console commands (qiangxue) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index dfbd84b925..54e0ba9849 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -423,7 +423,7 @@ class Formatter extends Component * @return string the formatted result * @see sizeFormat */ - public function asSize($value, $verbose=false) + public function asSize($value, $verbose = false) { $position = 0;