‪TYPO3CMS  ‪main
BytesViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
25 
64 final class ‪BytesViewHelper extends AbstractViewHelper
65 {
66  use CompileWithContentArgumentAndRenderStatic;
67 
73  protected ‪$escapeChildren = false;
74 
75  public function ‪initializeArguments(): void
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 
87  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
88  {
89  if ($arguments['units'] !== null) {
90  $units = $arguments['units'];
91  } else {
92  $units = ‪LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
93  }
94  $units = ‪GeneralUtility::trimExplode(',', (string)$units, true);
95 
96  $value = $renderChildrenClosure();
97 
98  if (is_numeric($value)) {
99  $value = (float)$value;
100  }
101  if (!is_int($value) && !is_float($value)) {
102  $value = 0;
103  }
104  $bytes = max($value, 0);
105  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
106  $pow = min($pow, count($units) - 1);
107  $bytes /= 2 ** (10 * $pow);
108 
109  return sprintf(
110  '%s %s',
111  number_format(
112  round($bytes, 4 * $arguments['decimals']),
113  (int)$arguments['decimals'],
114  $arguments['decimalSeparator'],
115  $arguments['thousandsSeparator']
116  ),
117  $units[$pow]
118  );
119  }
120 
124  public function ‪resolveContentArgumentName(): string
125  {
126  return 'value';
127  }
128 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: BytesViewHelper.php:85
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\initializeArguments
‪initializeArguments()
Definition: BytesViewHelper.php:73
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\resolveContentArgumentName
‪resolveContentArgumentName()
Definition: BytesViewHelper.php:122
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: BytesViewHelper.php:71
‪TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper
Definition: BytesViewHelper.php:65
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817