Completed
Push — master ( 08d356...86d138 )
by Alec
02:54
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;
6
7
use function AlecRabbit\Helpers\bounds;
8
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION;
9
10
class Pretty
11
{
12
    public const DEFAULT_DECIMALS = 2;
13
    public const PERCENT_MAX_DECIMALS = 4;
14
    public const DECIMAL_POINT = '.';
15
    public const THOUSANDS_SEPARATOR = '';
16
    public const DEFAULT_PRECISION = DEFAULT_PRECISION;
17
18
    /** @var null|string */
19
    private static $decimalPoint;
20
    /** @var null|string */
21
    private static $thousandsSeparator;
22
    /** @var null|int */
23
    private static $percentMaxDecimals;
24
25
    /**
26
     * Static class. Private Constructor.
27
     */
28
    // @codeCoverageIgnoreStart
29
    private function __construct()
30
    {
31
    }
32
    // @codeCoverageIgnoreEnd
33
34
    /**
35
     * @param int $number
36
     * @param null|string $unit
37
     * @param int|null $decimals
38
     * @return string
39
     */
40 11
    public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string
41
    {
42
        return
43 11
            format_bytes($number, $unit, $decimals ?? static::DEFAULT_DECIMALS);
44
    }
45
46
    /**
47
     * @param float $value
48
     * @param int|null $units
49
     * @param int|null $precision
50
     * @return string
51
     */
52 20
    public static function time(float $value, ?int $units = null, ?int $precision = null): string
53
    {
54 20
        if (null === $units && null === $precision) {
55 13
            return format_time_auto($value);
56
        }
57
        return
58 7
            format_time($value, $units, $precision ?? static::DEFAULT_PRECISION);
59
    }
60
61
    /**
62
     * @param float $fraction
63
     * @param null|int $decimals
64
     * @param null|string $prefix
65
     * @param string $suffix
66
     * @return string
67
     */
68 28
    public static function percent(
69
        float $fraction,
70
        ?int $decimals = null,
71
        ?string $prefix = null,
72
        string $suffix = '%'
73
    ): string {
74
        $decimals =
75 28
            (int)bounds(
76 28
                $decimals ?? static::DEFAULT_DECIMALS,
77 28
                0,
78 28
                static::$percentMaxDecimals ?? static::PERCENT_MAX_DECIMALS
0 ignored issues
show
Bug introduced by
Since $percentMaxDecimals is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $percentMaxDecimals to at least protected.
Loading history...
79
            );
80
        return
81 28
            ($prefix ?? '') .
82 28
            number_format(
83 28
                $fraction * 100,
84 28
                $decimals,
85 28
                static::$decimalPoint ?? static::DECIMAL_POINT,
0 ignored issues
show
Bug introduced by
Since $decimalPoint is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $decimalPoint to at least protected.
Loading history...
86 28
                static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR
0 ignored issues
show
Bug introduced by
Since $thousandsSeparator is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $thousandsSeparator to at least protected.
Loading history...
87
            ) .
88 28
            $suffix;
89
    }
90
91
    /**
92
     * @param string $decimalPoint
93
     */
94 14
    public static function setDecimalPoint(string $decimalPoint): void
95
    {
96 14
        self::$decimalPoint = $decimalPoint;
97 14
    }
98
99
    /**
100
     * @param string $thousandsSeparator
101
     */
102 14
    public static function setThousandsSeparator(string $thousandsSeparator): void
103
    {
104 14
        self::$thousandsSeparator = $thousandsSeparator;
105 14
    }
106
107
    /**
108
     * @param int $percentMaxDecimals
109
     */
110 7
    public static function setPercentMaxDecimals(int $percentMaxDecimals): void
111
    {
112 7
        self::$percentMaxDecimals = abs($percentMaxDecimals);
113 7
    }
114
115 21
    public static function resetDecimalPoint(): void
116
    {
117 21
        self::$decimalPoint = null;
118 21
    }
119
120 21
    public static function resetThousandsSeparator(): void
121
    {
122 21
        self::$thousandsSeparator = null;
123 21
    }
124
125 7
    public static function resetPercentMaxDecimals(): void
126
    {
127 7
        self::$percentMaxDecimals = null;
128 7
    }
129
}
130