Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 byteSizeToString
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 kilobyteSizeToString
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Utils\Converter
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Utils\Converter;
16
17/**
18 * File converter.
19 *
20 * @package phpOMS\Utils\Converter
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25final class File
26{
27    /**
28     * Constructor.
29     *
30     * @since 1.0.0
31     * @codeCoverageIgnore
32     */
33    private function __construct()
34    {
35    }
36
37    /**
38     * Get file size string.
39     *
40     * @param int    $bytes     Amount of bytes
41     * @param string $decimal   Decimal char
42     * @param string $thousands Thousands char
43     *
44     * @return string
45     *
46     * @since 1.0.0
47     */
48    public static function byteSizeToString(int $bytes, string $decimal = '.', string $thousands = ',') : string
49    {
50        if ($bytes < 1000) {
51            return $bytes . 'b';
52        } elseif ($bytes < 1000000) {
53            return \rtrim(\rtrim(\number_format($bytes / 1000, 1, $decimal, $thousands), '0'), $decimal) . 'kb';
54        } elseif ($bytes < 1000000000) {
55            return \rtrim(\rtrim(\number_format($bytes / 1000000, 1, $decimal, $thousands), '0'), $decimal) . 'mb';
56        }
57
58        return \rtrim(\rtrim(\number_format($bytes / 1000000000, 1, $decimal, $thousands), '0'), $decimal) . 'gb';
59    }
60
61    /**
62     * Get file size string.
63     *
64     * @param int    $kilobytes Amount of kilobytes
65     * @param string $decimal   Decimal char
66     * @param string $thousands Thousands char
67     *
68     * @return string
69     *
70     * @since 1.0.0
71     */
72    public static function kilobyteSizeToString(int $kilobytes, string $decimal = '.', string $thousands = ',') : string
73    {
74        if ($kilobytes < 1000) {
75            return \rtrim(\rtrim(\number_format($kilobytes, 1, $decimal, $thousands), '0'), $decimal) . 'kb';
76        } elseif ($kilobytes < 1000000) {
77            return \rtrim(\rtrim(\number_format($kilobytes / 1000, 1, $decimal, $thousands), '0'), $decimal) . 'mb';
78        }
79
80        return \rtrim(\rtrim(\number_format($kilobytes / 1000000, 1, $decimal, $thousands), '0'), $decimal) . 'gb';
81    }
82}