Skip to content
Permalink
Browse files

split Number class

  • Loading branch information...
antograssiot committed Sep 1, 2014
1 parent 8691204 commit c492bb4919960d5016590bf2c725d88b70bdbbdf
Showing with 183 additions and 104 deletions.
  1. +50 −0 src/I18n/I18nNumber.php
  2. +1 −23 src/Utility/Number.php
  3. +132 −0 tests/TestCase/I18n/I18nNumberTest.php
  4. +0 −81 tests/TestCase/Utility/NumberTest.php
@@ -0,0 +1,50 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;
use Cake\Utility\Number;
/**
* Number helper library.
*
* Method to handle translation of Number.
*
* @link http://book.cakephp.org/3.0/en/
*/
class I18nNumber extends Number {
/**
* 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);
}
}
}
@@ -53,28 +53,6 @@ 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
*
@@ -251,7 +229,7 @@ public static function defaultCurrency($currency = null) {
/**
* Returns a formatter object that can be reused for similar formatting task
* under the same locale and options. This is often a speedier alternative to
* using other methods in this class as on;y one formatter object needs to be
* using other methods in this class as only one formatter object needs to be
* constructed.
*
* The options array accepts the following keys:
@@ -0,0 +1,132 @@
<?php
/**
* NumberTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\I18n;
use Cake\I18n\I18n;
use Cake\I18n\I18nNumber;
use Cake\TestSuite\TestCase;
/**
* NumberTest class
*
*/
class NumberTest extends TestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->Number = new I18nNumber();
$this->locale = I18n::defaultLocale();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Number);
I18n::defaultLocale($this->locale);
}
/**
* 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);
}
}
@@ -355,87 +355,6 @@ public function testCurrencyIntlCode() {
$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);
}
/**
* test precision() with locales
*

0 comments on commit c492bb4

Please sign in to comment.
You can’t perform that action at this time.