TYPO3 CMS  TYPO3_8-7
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 
22 
56 {
58 
64  protected $escapeChildren = false;
65 
69  public function initializeArguments()
70  {
71  parent::initializeArguments();
72  $this->registerArgument('value', 'int', 'The incoming data to convert, or NULL if VH children should be used');
73  $this->registerArgument('decimals', 'int', 'The number of digits after the decimal point', false, 0);
74  $this->registerArgument('decimalSeparator', 'string', 'The decimal point character', false, '.');
75  $this->registerArgument('thousandsSeparator', 'string', 'The character for grouping the thousand digits', false, ',');
76  $this->registerArgument('units', 'string', 'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')');
77  }
78 
88  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
89  {
90  if ($arguments['units'] !== null) {
91  $units = $arguments['units'];
92  } else {
93  $units = LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
94  }
95  $units = GeneralUtility::trimExplode(',', $units, true);
96 
97  $value = $renderChildrenClosure();
98 
99  if (is_numeric($value)) {
100  $value = (float)$value;
101  }
102  if (!is_int($value) && !is_float($value)) {
103  $value = 0;
104  }
105  $bytes = max($value, 0);
106  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
107  $pow = min($pow, count($units) - 1);
108  $bytes /= pow(2, 10 * $pow);
109 
110  return sprintf(
111  '%s %s',
112  number_format(
113  round($bytes, 4 * $arguments['decimals']),
114  $arguments['decimals'],
115  $arguments['decimalSeparator'],
116  $arguments['thousandsSeparator']
117  ),
118  $units[$pow]
119  );
120  }
121 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static translate($key, $extensionName=null, $arguments=null)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)