Created
June 14, 2013 11:29
-
-
Save kdambekalns/5781153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TYPO3\Fluid\ViewHelpers\Format; | |
/* * | |
* This script belongs to the TYPO3 Flow package "Fluid". * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU Lesser General Public License, either version 3 * | |
* of the License, or (at your option) any later version. * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
use TYPO3\Flow\Annotations as Flow; | |
use TYPO3\Flow\I18n; | |
use TYPO3\Fluid\Core\ViewHelper; | |
/** | |
* Formats an integer with a byte count into human-readable form. | |
* | |
* = Examples = | |
* | |
* <code title="Defaults"> | |
* <f:format.bytes>{filesize}</f:format.date> | |
* </code> | |
* <output> | |
* 123KB | |
* </output> | |
* | |
* @api | |
*/ | |
class BytesViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper { | |
/** | |
* @var array | |
*/ | |
protected $units = array('B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB'); | |
/** | |
* Render the supplied byte count as a human readable string. | |
* | |
* @param integer $decimals The number of digits after the decimal point | |
* @param string $decimalSeparator The decimal point character | |
* @param string $thousandsSeparator The character for grouping the thousand digits | |
* @return string Formatted byte count | |
* @throws \TYPO3\Fluid\Core\ViewHelper\Exception | |
* @api | |
*/ | |
public function render($decimals = 1, $decimalSeparator = '.', $thousandsSeparator = ',') { | |
$bytes = max((integer)$this->renderChildren(), 0); | |
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | |
$pow = min($pow, count($this->units) - 1); | |
$bytes /= (1 << (10 * $pow)); | |
return sprintf( | |
'%s %s', | |
number_format(round($bytes, 4 * $decimals), $decimals, $decimalSeparator, $thousandsSeparator), | |
$this->units[$pow] | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment