Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ByteUnitConverter
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 parseBytes
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Shared\Infrastructure\Format;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Utility for parsing human-readable memory/byte units into integer bytes.
13 *
14 * @package App\Shared\Infrastructure\Format
15 */
16final class ByteUnitConverter
17{
18    /**
19     * Parses byte unit strings (e.g. '128M', '2G', '512K') into integer bytes.
20     *
21     * @param string $val Human-readable byte representation.
22     * @return int Value in bytes or -1 if empty/disabled.
23     */
24    public static function parseBytes(string $val): int
25    {
26        $val = trim($val);
27        if ($val === '' || $val === '-1') {
28            return -1;
29        }
30
31        $last = strtolower($val[strlen($val) - 1]);
32        $num = (int) $val;
33
34        return match ($last) {
35            'g' => $num * 1024 * 1024 * 1024,
36            'm' => $num * 1024 * 1024,
37            'k' => $num * 1024,
38            default => $num,
39        };
40    }
41}