yii\i18n\Formatter::asShortSize PHP Méthode

asShortSize() public méthode

This is the short form of [[asSize]]. If [[sizeFormatBase]] is 1024, binary prefixes (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result.
See also: sizeFormatBase
See also: asSize
public asShortSize ( string | integer | float $value, integer $decimals = null, array $options = [], array $textOptions = [] ) : string
$value string | integer | float value in bytes to be formatted.
$decimals integer the number of digits after the decimal point.
$options array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
$textOptions array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
Résultat string the formatted result.
    public function asShortSize($value, $decimals = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        list($params, $position) = $this->formatSizeNumber($value, $decimals, $options, $textOptions);
        if ($this->sizeFormatBase == 1024) {
            switch ($position) {
                case 0:
                    return Yii::t('yii', '{nFormatted} B', $params, $this->locale);
                case 1:
                    return Yii::t('yii', '{nFormatted} KiB', $params, $this->locale);
                case 2:
                    return Yii::t('yii', '{nFormatted} MiB', $params, $this->locale);
                case 3:
                    return Yii::t('yii', '{nFormatted} GiB', $params, $this->locale);
                case 4:
                    return Yii::t('yii', '{nFormatted} TiB', $params, $this->locale);
                default:
                    return Yii::t('yii', '{nFormatted} PiB', $params, $this->locale);
            }
        } else {
            switch ($position) {
                case 0:
                    return Yii::t('yii', '{nFormatted} B', $params, $this->locale);
                case 1:
                    return Yii::t('yii', '{nFormatted} KB', $params, $this->locale);
                case 2:
                    return Yii::t('yii', '{nFormatted} MB', $params, $this->locale);
                case 3:
                    return Yii::t('yii', '{nFormatted} GB', $params, $this->locale);
                case 4:
                    return Yii::t('yii', '{nFormatted} TB', $params, $this->locale);
                default:
                    return Yii::t('yii', '{nFormatted} PB', $params, $this->locale);
            }
        }
    }

Usage Example

Exemple #1
0
 public function testAsShortSize()
 {
     // tests for base 1000
     $this->formatter->sizeFormatBase = 1000;
     $this->assertSame("999 B", $this->formatter->asShortSize(999));
     $this->assertSame("1.05 MB", $this->formatter->asShortSize(1024 * 1024));
     $this->assertSame("1.0486 MB", $this->formatter->asShortSize(1024 * 1024, 4));
     $this->assertSame("1.00 KB", $this->formatter->asShortSize(1000));
     $this->assertSame("1.02 KB", $this->formatter->asShortSize(1023));
     $this->assertNotEquals("3 PB", $this->formatter->asShortSize(3 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000));
     // this is 3 EB not 3 PB
     // tests for base 1024
     $this->formatter->sizeFormatBase = 1024;
     $this->assertSame("1.00 KiB", $this->formatter->asShortSize(1024));
     $this->assertSame("1.00 MiB", $this->formatter->asShortSize(1024 * 1024));
     // https://github.com/yiisoft/yii2/issues/4960
     $this->assertSame("1023 B", $this->formatter->asShortSize(1023));
     $this->assertSame("5.00 GiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024));
     $this->assertNotEquals("5.00 PiB", $this->formatter->asShortSize(5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024));
     // this is 5 EiB not 5 PiB
     //$this->assertSame("1 YiB", $this->formatter->asShortSize(pow(2, 80)));
     $this->assertSame("2.00 GiB", $this->formatter->asShortSize(2147483647));
     // round 1.999 up to 2
     $this->formatter->decimalSeparator = ',';
     $this->assertSame("1,001 KiB", $this->formatter->asShortSize(1025, 3));
     // empty values
     $this->assertSame('0 bytes', $this->formatter->asSize(0));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asSize(null));
 }
All Usage Examples Of yii\i18n\Formatter::asShortSize