Skip to content

Commit

Permalink
remove LocalizedNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
antograssiot committed Sep 4, 2014
1 parent 85c294a commit a78d765
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 181 deletions.
50 changes: 0 additions & 50 deletions src/I18n/LocalizedNumber.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/I18n/Number.php
Expand Up @@ -53,6 +53,28 @@ public static function precision($value, $precision = 3) {
return $formatter->format($value);
}

/**
* Returns a formatted-for-humans file size.
*
* @param int $size Size in bytes
* @return string Human readable size
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
*/
public static function toReadableSize($size) {
switch (true) {
case $size < 1024:
return __dn('cake', '{0,number,integer} Byte', '{0,number,integer} Bytes', $size, $size);
case round($size / 1024) < 1024:
return __d('cake', '{0,number,#,###.##} KB', $size / 1024);
case round($size / 1024 / 1024, 2) < 1024:
return __d('cake', '{0,number,#,###.##} MB', $size / 1024 / 1024);
case round($size / 1024 / 1024 / 1024, 2) < 1024:
return __d('cake', '{0,number,#,###.##} GB', $size / 1024 / 1024 / 1024);
default:
return __d('cake', '{0,number,#,###.##} TB', $size / 1024 / 1024 / 1024 / 1024);
}
}

/**
* Converts filesize from human readable string to bytes
*
Expand Down
131 changes: 0 additions & 131 deletions tests/TestCase/I18n/LocalizedNumberTest.php

This file was deleted.

81 changes: 81 additions & 0 deletions tests/TestCase/I18n/NumberTest.php
Expand Up @@ -413,6 +413,87 @@ public function testToPercentage() {
$this->assertEquals($expected, $result);
}

/**
* testToReadableSize method
*
* @return void
*/
public function testToReadableSize() {
$result = $this->Number->toReadableSize(0);
$expected = '0 Bytes';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1);
$expected = '1 Byte';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(45);
$expected = '45 Bytes';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1023);
$expected = '1,023 Bytes';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024);
$expected = '1 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 + 123);
$expected = '1.12 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 512);
$expected = '512 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 - 1);
$expected = '1 MB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(512.05 * 1024 * 1024);
$expected = '512.05 MB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 - 1);
$expected = '1 GB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
$expected = '512 GB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 - 1);
$expected = '1 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 512);
$expected = '512 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 - 1);
$expected = '1,024 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 * 1024);
$expected = '1,048,576 TB';
$this->assertEquals($expected, $result);
}

/**
* test toReadableSize() with locales
*
* @return void
*/
public function testReadableSizeLocalized() {
I18n::defaultLocale('fr_FR');
$result = $this->Number->toReadableSize(1321205);
$this->assertEquals('1,26 MB', $result);

$result = $this->Number->toReadableSize(512.05 * 1024 * 1024 * 1024);
$this->assertEquals('512,05 GB', $result);
}

/**
* testFromReadableSize
*
Expand Down

0 comments on commit a78d765

Please sign in to comment.