Passed
Push — master ( 707534...dc95cb )
by Alec
01:50
created

Pretty::setThousandsSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Accessories;
6
7
use function AlecRabbit\format_bytes;
8
use function AlecRabbit\format_time;
9
use function AlecRabbit\format_time_auto;
10
use function AlecRabbit\Helpers\bounds;
11
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION;
12
13
class Pretty
14
{
15
    public const DEFAULT_DECIMALS = 2;
16
    public const MAX_DECIMALS = 6;
17
    public const DECIMAL_POINT = '.';
18
    public const THOUSANDS_SEPARATOR = '';
19
    public const DEFAULT_PRECISION = DEFAULT_PRECISION;
20
21
    /** @var null|string */
22
    protected static $decimalPoint;
23
    /** @var null|string */
24
    protected static $thousandsSeparator;
25
    /** @var null|int */
26
    protected static $maxDecimals;
27
28
    /**
29
     * Static class. Private Constructor.
30
     */
31
    // @codeCoverageIgnoreStart
32
    private function __construct()
33
    {
34
    }
35
    // @codeCoverageIgnoreEnd
36
37
    /**
38
     * @param int $number
39
     * @param null|string $unit
40
     * @param int|null $decimals
41
     * @return string
42
     */
43 11
    public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string
44
    {
45
        return
46 11
            format_bytes($number, $unit, static::refineDecimals($decimals));
47
    }
48
49
    /**
50
     * @param null|int $decimals
51
     * @return int
52
     */
53 64
    public static function refineDecimals(?int $decimals): int
54
    {
55
56
        return
57 64
            (int)bounds(
58 64
                $decimals ?? static::DEFAULT_DECIMALS,
59 64
                0,
60 64
                static::$maxDecimals ?? static::MAX_DECIMALS
61
            );
62
    }
63
64
    /**
65
     * @param float $seconds
66
     * @param null|int $units
67
     * @param null|int $decimals
68
     * @return string
69
     */
70 21
    public static function seconds(float $seconds, ?int $units = null, ?int $decimals = null): string
71
    {
72
        return
73 21
            static::time($seconds, $units, $decimals);
74
    }
75
76
    /**
77
     * @param float $seconds
78
     * @param int|null $units
79
     * @param int|null $decimals
80
     * @return string
81
     */
82 77
    public static function time(float $seconds, ?int $units = null, ?int $decimals = null): string
83
    {
84 77
        if (null === $units && null === $decimals) {
85 52
            return format_time_auto($seconds);
86
        }
87
        return
88 25
            format_time(
89 25
                $seconds,
90 25
                $units,
91 25
                static::refineDecimals($decimals),
92 25
                static::$decimalPoint ?? static::DECIMAL_POINT,
93 25
                static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR
94
            );
95
    }
96
97
    /**
98
     * @param float $useconds
99
     * @param null|int $units
100
     * @param null|int $decimals
101
     * @return string
102
     */
103 20
    public static function useconds(float $useconds, ?int $units = null, ?int $decimals = null): string
104
    {
105
        return
106 20
            static::time($useconds / 1000000, $units, $decimals);
107
    }
108
109
    /**
110
     * @param float $nanoseconds
111
     * @param null|int $units
112
     * @param null|int $decimals
113
     * @return string
114
     */
115 15
    public static function nanoseconds(float $nanoseconds, ?int $units = null, ?int $decimals = null): string
116
    {
117
        return
118 15
            static::time($nanoseconds / 1000000000, $units, $decimals);
119
    }
120
121
    /**
122
     * @param float $fraction
123
     * @param null|int $decimals
124
     * @param null|string $prefix
125
     * @param string $suffix
126
     * @return string
127
     */
128 28
    public static function percent(
129
        float $fraction,
130
        ?int $decimals = null,
131
        ?string $prefix = null,
132
        string $suffix = '%'
133
    ): string {
134
        return
135 28
            ($prefix ?? '') .
136 28
            number_format(
137 28
                $fraction * 100,
138 28
                static::refineDecimals($decimals),
139 28
                static::$decimalPoint ?? static::DECIMAL_POINT,
140 28
                static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR
141
            ) .
142 28
            $suffix;
143
    }
144
145
    /**
146
     * @param string $decimalPoint
147
     */
148 14
    public static function setDecimalPoint(string $decimalPoint): void
149
    {
150 14
        self::$decimalPoint = $decimalPoint;
151 14
    }
152
153
    /**
154
     * @param string $thousandsSeparator
155
     */
156 14
    public static function setThousandsSeparator(string $thousandsSeparator): void
157
    {
158 14
        self::$thousandsSeparator = $thousandsSeparator;
159 14
    }
160
161
    /**
162
     * @param int $maxDecimals
163
     */
164 7
    public static function setMaxDecimals(int $maxDecimals): void
165
    {
166 7
        self::$maxDecimals = abs($maxDecimals);
167 7
    }
168
169 21
    public static function resetDecimalPoint(): void
170
    {
171 21
        self::$decimalPoint = null;
172 21
    }
173
174 21
    public static function resetThousandsSeparator(): void
175
    {
176 21
        self::$thousandsSeparator = null;
177 21
    }
178
179 7
    public static function resetMaxDecimals(): void
180
    {
181 7
        self::$maxDecimals = null;
182 7
    }
183
}
184