Completed
Push — master ( 016c28...e7cc53 )
by Jan
03:46
created

SIFormatter::convertValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services;
33
34
/**
35
 * A service that helps you to format values using the SI prefixes.
36
 * @package App\Services
37
 */
38
class SIFormatter
39
{
40
    /**
41
     * Returns the magnitude of a value (the count of decimal place of the highest decimal place).
42
     * For example, for 100 (=10^2) this function returns 2. For -2500 (=-2.5*10^3) this function returns 3.
43
     * @param float $value The value of which the magnitude should be determined.
44
     * @return int The magnitude of the value
45
     */
46
    public function getMagnitude(float $value) : int
47
    {
48
        return (int) floor(log10(abs($value)));
49
    }
50
51
    /**
52
     * Returns the best SI prefix (and its corresponding divisor) for the given magnitude.
53
     * @param int $magnitude The magnitude for which the prefix should be determined.
54
     * @return array A array, containing the divisor in first element, and the prefix symbol in second. For example, [1000, "k"].
55
     */
56
    public function getPrefixByMagnitude(int $magnitude) : array
57
    {
58
        $prefixes_pos = ['' ,'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
59
        $prefixes_neg = ['' ,'m', 'μ', 'n', 'p', 'f', 'a', 'z', 'y'];
60
61
        //Determine nearest prefix index.
62
        $nearest = (int) round(abs($magnitude) / 3);
63
64
        if ($magnitude >= 0) {
65
            $symbol = $prefixes_pos[$nearest];
66
        } else {
67
            $symbol = $prefixes_neg[$nearest];
68
        }
69
70
        if ($magnitude < 0) {
71
            $nearest *= -1;
72
        }
73
74
        return [10 ** (3 * $nearest), $symbol];
75
    }
76
77
    /**
78
     *
79
     * @param float $value
80
     * @return array
81
     */
82
    public function convertValue(float $value) : array
83
    {
84
        //Choose the prefix to use
85
        $tmp = $this->getPrefixByMagnitude($this->getMagnitude($value));
86
87
        $ret = array(
88
            'value' => $value / $tmp[0],
89
            'prefix_magnitude' => log10($tmp[0]),
90
            'prefix' => $tmp[1]
91
        );
92
93
        return $ret;
94
    }
95
96
    /**
97
     * Formats the given value to a string, using the given options
98
     * @param float $value The value that should be converted
99
     * @param string $unit The unit that should be appended after the prefix
100
     * @param int $decimals The number of decimals (after decimal dot) that should be outputed.
101
     * @return string
102
     */
103
    public function format(float $value, string $unit = '', int $decimals = 2) : string
104
    {
105
        [$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value));
106
        $value /= $divisor;
107
        //Build the format string, e.g.: %.2d km
108
        $format_string = '%.' . $decimals . 'f ' . $symbol . $unit;
109
110
        return sprintf($format_string, $value);
111
    }
112
113
}