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