add number helper - only one method right now; the other originals here need to be...
authorChuck Hagenbuch <chuck@horde.org>
Mon, 23 Feb 2009 04:48:22 +0000 (23:48 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 23 Feb 2009 04:48:22 +0000 (23:48 -0500)
framework/View/lib/Horde/View/Helper/Number.php [new file with mode: 0644]
framework/View/test/Horde/View/Helper/NumberTest.php [new file with mode: 0644]

diff --git a/framework/View/lib/Horde/View/Helper/Number.php b/framework/View/lib/Horde/View/Helper/Number.php
new file mode 100644 (file)
index 0000000..558fa9e
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Copyright 2007 Maintainable Software, LLC
+ * Copyright 2006-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @license    http://opensource.org/licenses/bsd-license.php
+ * @category   Horde
+ * @package    Horde_View
+ * @subpackage Helper
+ */
+
+/**
+ * View helpers for URLs
+ *
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @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 (file)
index 0000000..74d7f97
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Copyright 2007-2008 Maintainable Software, LLC
+ * Copyright 2008-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @license    http://opensource.org/licenses/bsd-license.php
+ * @category   Horde
+ * @package    Horde_View
+ * @subpackage UnitTests
+ */
+
+/**
+ * @group      view
+ * @author     Mike Naberezny <mike@maintainable.com>
+ * @author     Derek DeVries <derek@maintainable.com>
+ * @author     Chuck Hagenbuch <chuck@horde.org>
+ * @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));
+    }
+
+}