[FEATURE] ViewHelper to display file sizes 36/21436/3
authorWouter Wolters <typo3@wouterwolters.nl>
Fri, 14 Jun 2013 18:04:11 +0000 (20:04 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 22 Jun 2013 22:18:23 +0000 (00:18 +0200)
Simple ViewHelper that converts the size of a file to a human
readable string.

Usage:
{fileSize -> f:format.bytes()}

Change-Id: Iffb155e209fe0d2e89aae6d93706e994b4b0b8e7
Resolves: #49139
Related: #49128
Releases: 6.2
Reviewed-on: https://review.typo3.org/21436
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Zbigniew Jacko
Tested-by: Zbigniew Jacko
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/fluid/Classes/ViewHelpers/Format/BytesViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/BytesViewHelperTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Format/BytesViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Format/BytesViewHelper.php
new file mode 100644 (file)
index 0000000..51ea19e
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
+
+/*                                                                        *
+ * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License, either version 3   *
+ *  of the License, or (at your option) any later version.                *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * Formats an integer with a byte count into human-readable form.
+ *
+ * = Examples =
+ *
+ * <code title="Defaults">
+ * {fileSize -> f:format.bytes()}
+ * </code>
+ * <output>
+ * 123 KB
+ * // depending on the value of {fileSize}
+ * </output>
+ *
+ * <code title="Defaults">
+ * {fileSize -> f:format.bytes(decimals: 2, decimalSeparator: ',', thousandsSeparator: ',')}
+ * </code>
+ * <output>
+ * 1,023.00 B
+ * // depending on the value of {fileSize}
+ * </output>
+ *
+ * @api
+ */
+class BytesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+
+       /**
+        * @var array
+        */
+       protected $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
+
+       /**
+        * Render the supplied byte count as a human readable string.
+        *
+        * @param integer $value The incoming data to convert, or NULL if VH children should be used
+        * @param integer $decimals The number of digits after the decimal point
+        * @param string $decimalSeparator The decimal point character
+        * @param string $thousandsSeparator The character for grouping the thousand digits
+        * @return string Formatted byte count
+        * @api
+        */
+       public function render($value = NULL, $decimals = 0, $decimalSeparator = '.', $thousandsSeparator = ',') {
+               if ($value === NULL) {
+                       $value = $this->renderChildren();
+               }
+
+               if (!is_integer($value) && !is_float($value)) {
+                       if (is_numeric($value)) {
+                               $value = (float)$value;
+                       } else {
+                               $value = 0;
+                       }
+               }
+               $bytes = max($value, 0);
+               $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
+               $pow = min($pow, count($this->units) - 1);
+               $bytes /= pow(2, (10 * $pow));
+
+               return sprintf(
+                       '%s %s',
+                       number_format(round($bytes, 4 * $decimals), $decimals, $decimalSeparator, $thousandsSeparator),
+                       $this->units[$pow]
+               );
+       }
+
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/BytesViewHelperTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/BytesViewHelperTest.php
new file mode 100644 (file)
index 0000000..cd2e1d5
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
+
+/*                                                                        *
+ * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU General Public License, either version 3 of the   *
+ * License, or (at your option) any later version.                        *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * Test for \TYPO3\Fluid\ViewHelpers\Format\BytesViewHelper
+ */
+class BytesViewHelperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Fluid\ViewHelpers\Format\NumberViewHelper
+        */
+       protected $viewHelper;
+
+       public function setUp() {
+               $this->viewHelper = $this->getMock('TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper', array('renderChildren'));
+       }
+
+       /**
+        * @return array
+        */
+       public function valueDataProvider() {
+               return array(
+
+                               // invalid values
+                       array(
+                               'value' => 'invalid',
+                               'decimals' => NULL,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '0 B'
+                       ),
+                       array(
+                               'value' => '',
+                               'decimals' => 2,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '0.00 B'
+                       ),
+                       array(
+                               'value' => array(),
+                               'decimals' => 2,
+                               'decimalSeparator' => ',',
+                               'thousandsSeparator' => NULL,
+                               'expected' => '0,00 B'
+                       ),
+                               // valid values
+                       array(
+                               'value' => 123,
+                               'decimals' => NULL,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '123 B'
+                       ),
+                       array(
+                               'value' => '43008',
+                               'decimals' => 1,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '42.0 KB'
+                       ),
+                       array(
+                               'value' => 1024,
+                               'decimals' => 1,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '1.0 KB'
+                       ),
+                       array(
+                               'value' => 1023,
+                               'decimals' => 2,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '1,023.00 B'
+                       ),
+                       array(
+                               'value' => 1073741823,
+                               'decimals' => 1,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => '.',
+                               'expected' => '1.024.0 MB'
+                       ),
+                       array(
+                               'value' => pow(1024, 5),
+                               'decimals' => 1,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '1.0 PB'
+                       ),
+                       array(
+                               'value' => pow(1024, 8),
+                               'decimals' => 1,
+                               'decimalSeparator' => NULL,
+                               'thousandsSeparator' => NULL,
+                               'expected' => '1.0 YB'
+                       )
+               );
+       }
+
+       /**
+        * @param $value
+        * @param $decimals
+        * @param $decimalSeparator
+        * @param $thousandsSeparator
+        * @param $expected
+        * @test
+        * @dataProvider valueDataProvider
+        */
+       public function renderCorrectlyConvertsAValue($value, $decimals, $decimalSeparator, $thousandsSeparator, $expected) {
+               $actualResult = $this->viewHelper->render($value, $decimals, $decimalSeparator, $thousandsSeparator);
+               $this->assertEquals($expected, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function renderUsesChildNodesIfValueArgumentIsOmitted() {
+               $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(12345));
+               $actualResult = $this->viewHelper->render();
+               $this->assertEquals('12 KB', $actualResult);
+       }
+}
+
+?>
\ No newline at end of file