99 */
1010class ByteFormatter
1111{
12- private const DEFAULT_BASE = Base::BINARY ;
12+ private int $ base = Base::BINARY ;
1313
14- /** @var int */
15- private $ base = self ::DEFAULT_BASE ;
14+ private string $ format ;
1615
17- /** @var string */
18- private $ format ;
16+ private string $ sprintfFormat ;
1917
20- /** @var string */
21- private $ sprintfFormat ;
18+ private int $ precision = 0 ;
2219
23- /** @var int */
24- private $ precision = 0 ;
20+ private bool $ automaticPrecision = true ;
2521
26- /** @var bool */
27- private $ automaticPrecision = true ;
22+ private ?int $ exponent = null ;
2823
29- /** @var int */
30- private $ exponent ;
31-
32- /** @var UnitDecorator */
33- private $ unitDecorator ;
24+ private UnitDecorator $ unitDecorator ;
3425
3526 /**
3627 * Initializes this instance, optionally with a specific unit decorator.
@@ -54,13 +45,13 @@ public function __construct(UnitDecorator $unitDecorator = null)
5445 *
5546 * @return string Formatted bytes.
5647 */
57- public function format ($ bytes , int $ precision = null ): string
48+ public function format (int | float $ bytes , int $ precision = null ): string
5849 {
5950 // Use default precision when not specified.
6051 $ precision === null && $ precision = $ this ->getPrecision ();
6152
6253 $ log = log ($ bytes , $ this ->getBase ());
63- $ exponent = $ this ->hasFixedExponent () ? $ this ->getFixedExponent () : max (0 , $ log| 0 );
54+ $ exponent = $ this ->hasFixedExponent () ? $ this ->getFixedExponent () : max (0 , ( int ) $ log );
6455 $ value = round ($ this ->getBase () ** ($ log - $ exponent ), $ precision );
6556 $ units = $ this ->getUnitDecorator ()->decorate ($ exponent , $ this ->getBase (), $ value );
6657
@@ -89,12 +80,12 @@ private function formatValue(float $value, int $precision): string
8980
9081 if (isset ($ formattedParts [1 ])) {
9182 // Strip trailing 0s in fractional part.
92- if (!$ formattedParts [1 ] = chop ($ formattedParts [1 ], '0 ' )) {
83+ if (!$ formattedParts [1 ] = rtrim ($ formattedParts [1 ], '0 ' )) {
9384 // Remove fractional part.
9485 unset($ formattedParts [1 ]);
9586 }
9687
97- $ formatted = join ('. ' , $ formattedParts );
88+ $ formatted = implode ('. ' , $ formattedParts );
9889 }
9990 }
10091
0 commit comments