[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / BytesViewHelper.php
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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
22
23 /**
24 * Formats an integer with a byte count into human-readable form.
25 *
26 * = Examples =
27 *
28 * <code title="Defaults">
29 * {fileSize -> f:format.bytes()}
30 * </code>
31 * <output>
32 * 123 KB
33 * // depending on the value of {fileSize}
34 * </output>
35 *
36 * <code title="Defaults">
37 * {fileSize -> f:format.bytes(decimals: 2, decimalSeparator: '.', thousandsSeparator: ',')}
38 * </code>
39 * <output>
40 * 1,023.00 B
41 * // depending on the value of {fileSize}
42 * </output>
43 *
44 * You may provide an own set of units, like this: B,KB,MB,GB,TB,PB,EB,ZB,YB
45 * <code title="custom units">
46 * {fileSize -> f:format.bytes(units: '{f:translate(\'viewhelper.format.bytes.units\', \'fluid\')}'
47 * </code>
48 * <output>
49 * 123 KB
50 * // depending on the value of {fileSize}
51 * </output>
52 */
53 class BytesViewHelper extends AbstractViewHelper
54 {
55 use CompileWithContentArgumentAndRenderStatic;
56
57 /**
58 * Output is escaped already. We must not escape children, to avoid double encoding.
59 *
60 * @var bool
61 */
62 protected $escapeChildren = false;
63
64 /**
65 * Initialize ViewHelper arguments
66 */
67 public function initializeArguments()
68 {
69 $this->registerArgument('value', 'int', 'The incoming data to convert, or NULL if VH children should be used');
70 $this->registerArgument('decimals', 'int', 'The number of digits after the decimal point', false, 0);
71 $this->registerArgument('decimalSeparator', 'string', 'The decimal point character', false, '.');
72 $this->registerArgument('thousandsSeparator', 'string', 'The character for grouping the thousand digits', false, ',');
73 $this->registerArgument('units', 'string', 'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')');
74 }
75
76 /**
77 * Render the supplied byte count as a human readable string.
78 *
79 * @param array $arguments
80 * @param \Closure $renderChildrenClosure
81 * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
82 *
83 * @return string Formatted byte count
84 */
85 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
86 {
87 if ($arguments['units'] !== null) {
88 $units = $arguments['units'];
89 } else {
90 $units = LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
91 }
92 $units = GeneralUtility::trimExplode(',', $units, true);
93
94 $value = $renderChildrenClosure();
95
96 if (is_numeric($value)) {
97 $value = (float)$value;
98 }
99 if (!is_int($value) && !is_float($value)) {
100 $value = 0;
101 }
102 $bytes = max($value, 0);
103 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
104 $pow = min($pow, count($units) - 1);
105 $bytes /= pow(2, 10 * $pow);
106
107 return sprintf(
108 '%s %s',
109 number_format(
110 round($bytes, 4 * $arguments['decimals']),
111 $arguments['decimals'],
112 $arguments['decimalSeparator'],
113 $arguments['thousandsSeparator']
114 ),
115 $units[$pow]
116 );
117 }
118 }