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