Completed
Push — master ( aafbda...c2b43f )
by Jan
04:12
created

SIFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 8 1
A getMagnitude() 0 3 1
A getPrefixByMagnitude() 0 19 3
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
     * @param float $value
79
     * @param string $unit
80
     * @param int $decimals
81
     * @return string
82
     */
83
    public function format(float $value, string $unit = '', int $decimals = 2)
84
    {
85
        [$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value));
86
        $value /= $divisor;
87
        //Build the format string, e.g.: %.2d km
88
        $format_string = '%.' . $decimals . 'f ' . $symbol . $unit;
89
90
        return sprintf($format_string, $value);
91
    }
92
93
}