FormatBytesViewHelper::renderStatic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Qbus\Qbtools\ViewHelpers;
3
4
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
5
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
6
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
7
8
/* * *************************************************************
9
 *  Copyright notice
10
 *
11
 *  (c) 2013 Axel Wüstemann <[email protected]>, Qbus Werbeagentur GmbH
12
 *
13
 *  All rights reserved
14
 *
15
 *  This script is part of the TYPO3 project. The TYPO3 project is
16
 *  free software; you can redistribute it and/or modify
17
 *  it under the terms of the GNU General Public License as published by
18
 *  the Free Software Foundation; either version 3 of the License, or
19
 *  (at your option) any later version.
20
 *
21
 *  The GNU General Public License can be found at
22
 *  http://www.gnu.org/copyleft/gpl.html.
23
 *
24
 *  This script is distributed in the hope that it will be useful,
25
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 *  GNU General Public License for more details.
28
 *
29
 *  This copyright notice MUST APPEAR in all copies of the script!
30
 * ************************************************************* */
31
32
/**
33
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
34
 */
35
class FormatBytesViewHelper extends AbstractViewHelper
36
{
37
    use CompileWithRenderStatic;
38
39
    /**
40
     * Initialize arguments
41
     */
42
    public function initializeArguments()
43
    {
44
        $this->registerArgument('size', 'int', '', true);
45
    }
46
47
    /**
48
     * @param array $arguments
49
     * @param \Closure $renderChildrenClosure
50
     * @param RenderingContextInterface $renderingContext
51
     * @return string
52
     * @throws Exception
53
     */
54
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
55
    {
56
        $size = $arguments['size'];
57
        # http://stackoverflow.com/a/2510540
58
        $base = log($size) / log(1024);
59
        $suffixes = array('', 'k', 'M', 'G', 'T');
60
61
        return trim(round(pow(1024, $base - floor($base))) . ' ' . $suffixes[floor($base)]);
62
    }
63
}
64