[FEATURE] Allow unit string as viewhelper argument 89/49289/8
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 1 Aug 2016 15:50:56 +0000 (17:50 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 2 Aug 2016 14:40:22 +0000 (16:40 +0200)
The ByteViewHelper accepts a parameter 'units' in order to provide
a custom collection of units.

Resolves: #77336
Releases: master
Change-Id: I58a6609eb1d1c8eba1e2610430c20c576481488b
Reviewed-on: https://review.typo3.org/49289
Reviewed-by: Sebastian Bumann <bumann.sebastian@gmail.com>
Tested-by: Sebastian Bumann <bumann.sebastian@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Documentation/Changelog/master/Feature-77336-AllowPassingAnOwnUnitCollectionToByteViewHelper.rst [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Format/BytesViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/BytesViewHelperTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-77336-AllowPassingAnOwnUnitCollectionToByteViewHelper.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-77336-AllowPassingAnOwnUnitCollectionToByteViewHelper.rst
new file mode 100644 (file)
index 0000000..cb31df1
--- /dev/null
@@ -0,0 +1,26 @@
+========================================================================
+Feature: #77336 - Allow passing an own unit collection to ByteViewHelper
+========================================================================
+
+Description
+===========
+
+The viewhelper accepts a new parameter named units. It must be a comma separated list of units.
+
+First example: Use the translation VH
+
+.. code-block::
+{fileSize -> f:format.bytes(units: '{f:translate(\'viewhelper.format.bytes.units\', \'fluid\')}'}
+
+Second example: Provide a plain list
+
+.. code-block::
+<f:format.bytes units="byte, kilo, mega, husel, pusel">{size}</f:format.bytes>
+
+results in the currently used collection, provided by the core.
+
+
+Impact
+======
+
+A custom list of units can be passed to the viewHelper and will be used for formatting. Existing behaviour is not changed.
\ No newline at end of file
index e3afc53..c7bd8e0 100644 (file)
@@ -40,6 +40,15 @@ use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  * // depending on the value of {fileSize}
  * </output>
  *
+ * You may provide an own set of units, like this: B,KB,MB,GB,TB,PB,EB,ZB,YB
+ * <code title="custom units">
+ * {fileSize -> f:format.bytes(units: '{f:translate(\'viewhelper.format.bytes.units\', \'fluid\')}'
+ * </code>
+ * <output>
+ * 123 KB
+ * // depending on the value of {fileSize}
+ * </output>
+ *
  * @api
  */
 class BytesViewHelper extends AbstractViewHelper
@@ -67,14 +76,11 @@ class BytesViewHelper extends AbstractViewHelper
             '.');
         $this->registerArgument('thousandsSeparator', 'string',
             'The character for grouping the thousand digits', false, ',');
+        $this->registerArgument('units', 'string',
+            'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')');
     }
 
     /**
-     * @var array
-     */
-    protected static $units = array();
-
-    /**
      * Render the supplied byte count as a human readable string.
      *
      * @return string Formatted byte count
@@ -90,19 +96,6 @@ class BytesViewHelper extends AbstractViewHelper
     }
 
     /**
-     * set units - temporary method, will vanish with https://review.typo3.org/#/c/49289/
-     *
-     */
-    public static function setUnits($units = [])
-    {
-        self::$units = $units;
-        if (empty($units)) {
-            self::$units = GeneralUtility::trimExplode(',',
-                LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid'));
-        }
-    }
-
-    /**
      * Applies htmlspecialchars() on the specified value.
      *
      * @param array $arguments
@@ -116,9 +109,12 @@ class BytesViewHelper extends AbstractViewHelper
         \Closure $renderChildrenClosure,
         RenderingContextInterface $renderingContext
     ) {
-        if (empty(self::$units)) {
-            self::setUnits();
+        if ($arguments['units'] !== null) {
+            $units = $arguments['units'];
+        } else {
+            $units = LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
         }
+        $units = GeneralUtility::trimExplode(',', $units, true);
 
         $value = $arguments['value'];
         if ($value === null) {
@@ -133,14 +129,14 @@ class BytesViewHelper extends AbstractViewHelper
         }
         $bytes = max($value, 0);
         $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
-        $pow = min($pow, count(self::$units) - 1);
+        $pow = min($pow, count($units) - 1);
         $bytes /= pow(2, 10 * $pow);
 
         return sprintf(
             '%s %s',
             number_format(round($bytes, 4 * $arguments['decimals']), $arguments['decimals'],
                 $arguments['decimalSeparator'], $arguments['thousandsSeparator']),
-            self::$units[$pow]
+            $units[$pow]
         );
     }
 }
index ebc3393..0b0afb0 100644 (file)
@@ -14,8 +14,6 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
 use TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper;
 
@@ -32,14 +30,8 @@ class BytesViewHelperTest extends ViewHelperBaseTestcase
     protected function setUp()
     {
         parent::setUp();
-        // XXX: This is bad from a testing POV but the only option right now
-        $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
-        $property = $reflectionClass->getProperty('configurationManager');
-        $property->setAccessible(true);
-        $property->setValue($this->createMock(ConfigurationManagerInterface::class));
         $this->viewHelper = new BytesViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
-        BytesViewHelper::setUnits(GeneralUtility::trimExplode(',', 'B,KB,MB,GB,TB,PB,EB,ZB,YB'));
     }
 
     /**
@@ -142,6 +134,7 @@ class BytesViewHelperTest extends ViewHelperBaseTestcase
                 'decimals' => $decimals,
                 'decimalSeparator' => $decimalSeparator,
                 'thousandsSeparator' => $thousandsSeparator,
+                'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
             ]
         );
         $actualResult = $this->viewHelper->initializeArgumentsAndRender();
@@ -158,6 +151,12 @@ class BytesViewHelperTest extends ViewHelperBaseTestcase
                 return 12345;
             }
         );
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
+            ]
+        );
         $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('12 KB', $actualResult);
     }