1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Server\Status\Server\Memory; |
5
|
|
|
|
6
|
|
|
use Innmind\Server\Status\Exception\{ |
7
|
|
|
BytesCannotBeNegative, |
8
|
|
|
UnknownBytesFormat, |
9
|
|
|
}; |
10
|
|
|
use Innmind\Immutable\Str; |
11
|
|
|
|
12
|
|
|
final class Bytes |
13
|
|
|
{ |
14
|
|
|
public const BYTES = 1024; |
15
|
|
|
public const KILOBYTES = 1024 ** 2; |
16
|
|
|
public const MEGABYTES = 1024 ** 3; |
17
|
|
|
public const GIGABYTES = 1024 ** 4; |
18
|
|
|
public const TERABYTES = 1024 ** 5; |
19
|
|
|
public const PETABYTES = 1024 ** 6; |
20
|
|
|
|
21
|
|
|
private int $value; |
22
|
|
|
private string $string; |
23
|
|
|
|
24
|
105 |
|
public function __construct(int $value) |
25
|
|
|
{ |
26
|
105 |
|
if ($value < 0) { |
27
|
3 |
|
throw new BytesCannotBeNegative((string) $value); |
28
|
|
|
} |
29
|
|
|
|
30
|
102 |
|
$this->value = $value; |
31
|
|
|
$this->string = $value.'B'; |
32
|
|
|
|
33
|
102 |
|
switch (true) { |
34
|
42 |
|
case $value < self::BYTES: |
35
|
42 |
|
$this->string = $value.'B'; |
36
|
|
|
break; |
37
|
72 |
|
|
38
|
12 |
|
case $value < self::KILOBYTES: |
39
|
12 |
|
$this->string = \sprintf( |
40
|
12 |
|
'%sKB', |
41
|
|
|
\round($value/self::BYTES, 3), |
42
|
12 |
|
); |
43
|
|
|
break; |
44
|
60 |
|
|
45
|
24 |
|
case $value < self::MEGABYTES: |
46
|
24 |
|
$this->string = \sprintf( |
47
|
24 |
|
'%sMB', |
48
|
|
|
\round($value/self::KILOBYTES, 3), |
49
|
24 |
|
); |
50
|
|
|
break; |
51
|
48 |
|
|
52
|
24 |
|
case $value < self::GIGABYTES: |
53
|
24 |
|
$this->string = \sprintf( |
54
|
24 |
|
'%sGB', |
55
|
|
|
\round($value/self::MEGABYTES, 3), |
56
|
24 |
|
); |
57
|
|
|
break; |
58
|
24 |
|
|
59
|
12 |
|
case $value < self::TERABYTES: |
60
|
12 |
|
$this->string = \sprintf( |
61
|
12 |
|
'%sTB', |
62
|
|
|
\round($value/self::GIGABYTES, 3), |
63
|
12 |
|
); |
64
|
|
|
break; |
65
|
12 |
|
|
66
|
12 |
|
case $value < self::PETABYTES: |
67
|
12 |
|
$this->string = \sprintf( |
68
|
12 |
|
'%sPB', |
69
|
|
|
\round($value/self::TERABYTES, 3), |
70
|
12 |
|
); |
71
|
|
|
break; |
72
|
102 |
|
} |
73
|
|
|
} |
74
|
39 |
|
|
75
|
|
|
public function toInt(): int |
76
|
39 |
|
{ |
77
|
|
|
return $this->value; |
78
|
|
|
} |
79
|
75 |
|
|
80
|
|
|
public function toString(): string |
81
|
75 |
|
{ |
82
|
|
|
return $this->string; |
83
|
|
|
} |
84
|
57 |
|
|
85
|
|
|
public static function of(string $bytes): self |
86
|
57 |
|
{ |
87
|
9 |
|
if ($bytes === (string) (int) $bytes) { |
88
|
|
|
return new self((int) $bytes); |
89
|
|
|
} |
90
|
54 |
|
|
91
|
|
|
$bytes = Str::of($bytes); |
92
|
54 |
|
|
93
|
6 |
|
if ($bytes->length() < 2) { |
94
|
|
|
throw new UnknownBytesFormat($bytes->toString()); |
95
|
|
|
} |
96
|
|
|
|
97
|
48 |
|
try { |
98
|
48 |
|
return self::fromUnit( |
99
|
48 |
|
$bytes->substring(0, -1), |
100
|
|
|
$bytes->substring(-1), |
101
|
24 |
|
); |
102
|
24 |
|
} catch (UnknownBytesFormat $e) { |
103
|
24 |
|
return self::fromUnit( |
104
|
24 |
|
$bytes->substring(0, -2), |
105
|
|
|
$bytes->substring(-2), |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private static function fromUnit(Str $bytes, Str $unit): self |
111
|
|
|
{ |
112
|
|
|
if ($bytes->length() === 0) { |
113
|
51 |
|
throw new UnknownBytesFormat($bytes->toString()); |
114
|
|
|
} |
115
|
51 |
|
|
116
|
|
|
switch ($unit->toString()) { |
117
|
|
|
case 'B': |
118
|
48 |
|
case 'Bi': |
119
|
|
|
$multiplier = 1; |
120
|
48 |
|
break; |
121
|
3 |
|
|
122
|
|
|
case 'K': |
123
|
|
|
case 'Ki': |
124
|
48 |
|
$multiplier = Bytes::BYTES; |
125
|
48 |
|
break; |
126
|
45 |
|
|
127
|
6 |
|
case 'M': |
128
|
6 |
|
case 'Mi': |
129
|
|
|
$multiplier = Bytes::KILOBYTES; |
130
|
45 |
|
break; |
131
|
42 |
|
|
132
|
6 |
|
case 'G': |
133
|
6 |
|
case 'Gi': |
134
|
|
|
$multiplier = Bytes::MEGABYTES; |
135
|
42 |
|
break; |
136
|
39 |
|
|
137
|
12 |
|
case 'T': |
138
|
12 |
|
case 'Ti': |
139
|
|
|
$multiplier = Bytes::GIGABYTES; |
140
|
39 |
|
break; |
141
|
30 |
|
|
142
|
12 |
|
case 'P': |
143
|
12 |
|
case 'Pi': |
144
|
|
|
$multiplier = Bytes::TERABYTES; |
145
|
30 |
|
break; |
146
|
27 |
|
|
147
|
6 |
|
default: |
148
|
6 |
|
throw new UnknownBytesFormat($bytes->toString()); |
149
|
|
|
} |
150
|
27 |
|
|
151
|
24 |
|
return new self( |
152
|
6 |
|
(int) (((float) $bytes->toString()) * $multiplier), |
153
|
6 |
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|