TYPO3  7.6
BytesViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 
19 
44 {
48  protected static $units = array();
49 
60  public function render($value = null, $decimals = 0, $decimalSeparator = '.', $thousandsSeparator = ',')
61  {
62  return static::renderStatic(
63  array(
64  'value' => $value,
65  'decimals' => $decimals,
66  'decimalSeparator' => $decimalSeparator,
67  'thousandsSeparator' => $thousandsSeparator
68  ),
70  $this->renderingContext
71  );
72  }
73 
83  {
84  $value = $arguments['value'];
85  if ($value === null) {
86  $value = $renderChildrenClosure();
87  }
88 
89  if (empty(self::$units)) {
90  self::$units = GeneralUtility::trimExplode(',', LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid'));
91  }
92  if (!is_integer($value) && !is_float($value)) {
93  if (is_numeric($value)) {
94  $value = (float)$value;
95  } else {
96  $value = 0;
97  }
98  }
99  $bytes = max($value, 0);
100  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
101  $pow = min($pow, count(self::$units) - 1);
102  $bytes /= pow(2, (10 * $pow));
103 
104  return sprintf(
105  '%s %s',
106  number_format(round($bytes, 4 * $arguments['decimals']), $arguments['decimals'], $arguments['decimalSeparator'], $arguments['thousandsSeparator']),
107  self::$units[$pow]
108  );
109  }
110 }