TYPO3 CMS  TYPO3_6-2
BytesViewHelper.php
Go to the documentation of this file.
1 <?php
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 
38 
42  protected $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
43 
54  public function render($value = NULL, $decimals = 0, $decimalSeparator = '.', $thousandsSeparator = ',') {
55  if ($value === NULL) {
56  $value = $this->renderChildren();
57  }
58 
59  if (!is_integer($value) && !is_float($value)) {
60  if (is_numeric($value)) {
61  $value = (float)$value;
62  } else {
63  $value = 0;
64  }
65  }
66  $bytes = max($value, 0);
67  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
68  $pow = min($pow, count($this->units) - 1);
69  $bytes /= pow(2, (10 * $pow));
70 
71  return sprintf(
72  '%s %s',
73  number_format(round($bytes, 4 * $decimals), $decimals, $decimalSeparator, $thousandsSeparator),
74  $this->units[$pow]
75  );
76  }
77 
78 }
render($value=NULL, $decimals=0, $decimalSeparator='.', $thousandsSeparator=',')