From 02282f66cab6ac7794bb74bc6fa7f6e4b8a8a401 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sun, 22 Feb 2009 23:48:22 -0500 Subject: [PATCH] add number helper - only one method right now; the other originals here need to be locale-specific --- framework/View/lib/Horde/View/Helper/Number.php | 69 ++++++++++++++++++++++ .../View/test/Horde/View/Helper/NumberTest.php | 55 +++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 framework/View/lib/Horde/View/Helper/Number.php create mode 100644 framework/View/test/Horde/View/Helper/NumberTest.php diff --git a/framework/View/lib/Horde/View/Helper/Number.php b/framework/View/lib/Horde/View/Helper/Number.php new file mode 100644 index 000000000..558fa9ee3 --- /dev/null +++ b/framework/View/lib/Horde/View/Helper/Number.php @@ -0,0 +1,69 @@ + + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_View + * @subpackage Helper + */ + +/** + * View helpers for URLs + * + * @author Mike Naberezny + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_View + * @subpackage Helper + */ +class Horde_View_Helper_Number extends Horde_View_Helper_Base +{ + /** + * Formats the bytes in $size into a more understandable representation. + * Useful for reporting file sizes to users. This method returns NULL if + * $size cannot be converted into a number. You can change the default + * precision of 1 in $precision. + * + * $this->numberToHumanSize(123) => 123 Bytes + * $this->numberToHumanSize(1234) => 1.2 KB + * $this->numberToHumanSize(12345) => 12.1 KB + * $this->numberToHumanSize(1234567) => 1.2 MB + * $this->numberToHumanSize(1234567890) => 1.1 GB + * $this->numberToHumanSize(1234567890123) => 1.1 TB + * $this->numberToHumanSize(1234567, 2) => 1.18 MB + * + * @param integer|float $size Size to format + * @param integer $preceision Level of precision + * @return string Formatted size value + */ + public function numberToHumanSize($size, $precision = 1) + { + if (! is_numeric($size)) { + return null; + } + + if ($size == 1) { + $size = '1 Byte'; + } elseif ($size < 1024) { + $size = sprintf('%d Bytes', $size); + } elseif ($size < 1048576) { + $size = sprintf("%.{$precision}f KB", $size / 1024); + } elseif ($size < 1073741824) { + $size = sprintf("%.{$precision}f MB", $size / 1048576); + } elseif ($size < 1099511627776) { + $size = sprintf("%.{$precision}f GB", $size / 1073741824); + } else { + $size = sprintf("%.{$precision}f TB", $size / 1099511627776); + } + + return str_replace('.0', '', $size); + } + +} diff --git a/framework/View/test/Horde/View/Helper/NumberTest.php b/framework/View/test/Horde/View/Helper/NumberTest.php new file mode 100644 index 000000000..74d7f9752 --- /dev/null +++ b/framework/View/test/Horde/View/Helper/NumberTest.php @@ -0,0 +1,55 @@ + + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_View + * @subpackage UnitTests + */ + +/** + * @group view + * @author Mike Naberezny + * @author Derek DeVries + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Horde_View + * @subpackage UnitTests + */ +class Horde_View_Helper_NumberTest extends Horde_Test_Case +{ + public function setUp() + { + $this->helper = new Horde_View_Helper_Number(new Horde_View()); + } + + public function testNumberToHumanSize() + { + $this->assertEquals('0 Bytes', $this->helper->numberToHumanSize(0)); + $this->assertEquals('0 Bytes', $this->helper->numberToHumanSize(0)); + $this->assertEquals('1 Byte', $this->helper->numberToHumanSize(1)); + $this->assertEquals('3 Bytes', $this->helper->numberToHumanSize(3.14159265)); + $this->assertEquals('123 Bytes', $this->helper->numberToHumanSize(123.0)); + $this->assertEquals('123 Bytes', $this->helper->numberToHumanSize(123)); + $this->assertEquals('1.2 KB', $this->helper->numberToHumanSize(1234)); + $this->assertEquals('12.1 KB', $this->helper->numberToHumanSize(12345)); + $this->assertEquals('1.2 MB', $this->helper->numberToHumanSize(1234567)); + $this->assertEquals('1.1 GB', $this->helper->numberToHumanSize(1234567890)); + $this->assertEquals('1.1 TB', $this->helper->numberToHumanSize(1234567890123)); + $this->assertEquals('444 KB', $this->helper->numberToHumanSize(444 * 1024)); + $this->assertEquals('1023 MB', $this->helper->numberToHumanSize(1023 * 1048576)); + $this->assertEquals('3 TB', $this->helper->numberToHumanSize(3 * 1099511627776)); + $this->assertEquals('1.18 MB', $this->helper->numberToHumanSize(1234567, 2)); + $this->assertEquals('3 Bytes', $this->helper->numberToHumanSize(3.14159265, 4)); + $this->assertEquals("123 Bytes", $this->helper->numberToHumanSize("123")); + $this->assertNull($this->helper->numberToHumanSize('x')); + $this->assertNull($this->helper->numberToHumanSize(null)); + } + +} -- 2.11.0