‪TYPO3CMS  9.5
BytesViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
22 
61 class ‪BytesViewHelper extends AbstractViewHelper
62 {
63  use CompileWithContentArgumentAndRenderStatic;
64 
70  protected ‪$escapeChildren = false;
71 
75  public function ‪initializeArguments()
76  {
77  $this->registerArgument('value', 'int', 'The incoming data to convert, or NULL if VH children should be used');
78  $this->registerArgument('decimals', 'int', 'The number of digits after the decimal point', false, 0);
79  $this->registerArgument('decimalSeparator', 'string', 'The decimal point character', false, '.');
80  $this->registerArgument('thousandsSeparator', 'string', 'The character for grouping the thousand digits', false, ',');
81  $this->registerArgument('units', 'string', 'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')');
82  }
83 
93  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
94  {
95  if ($arguments['units'] !== null) {
96  $units = $arguments['units'];
97  } else {
98  $units = ‪LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
99  }
100  $units = GeneralUtility::trimExplode(',', $units, true);
101 
102  $value = $renderChildrenClosure();
103 
104  if (is_numeric($value)) {
105  $value = (float)$value;
106  }
107  if (!is_int($value) && !is_float($value)) {
108  $value = 0;
109  }
110  $bytes = max($value, 0);
111  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
112  $pow = min($pow, count($units) - 1);
113  $bytes /= pow(2, 10 * $pow);
114 
115  return sprintf(
116  '%s %s',
117  number_format(
118  round($bytes, 4 * $arguments['decimals']),
119  $arguments['decimals'],
120  $arguments['decimalSeparator'],
121  $arguments['thousandsSeparator']
122  ),
123  $units[$pow]
124  );
125  }
126 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:29
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\initializeArguments
‪initializeArguments()
Definition: BytesViewHelper.php:73
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate($key, $extensionName=null, $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:63
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: BytesViewHelper.php:68
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper
Definition: BytesViewHelper.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: BytesViewHelper.php:91