@@ -17,15 +17,21 @@ class ByteFormatter
1717 /** @var string */
1818 private $ format ;
1919
20+ /** @var string */
21+ private $ sprintfFormat ;
22+
2023 /** @var int */
2124 private $ precision = 0 ;
2225
23- /** @var string */
24- private $ normalizedFormat ;
25-
2626 /** @var UnitDecorator */
2727 private $ unitDecorator ;
2828
29+ /**
30+ * Initializes this instance, optionally with a specific unit decorator.
31+ * If no unit decorator is specified, SymbolDecorator will be used.
32+ *
33+ * @param UnitDecorator|null $unitDecorator Optional. Unit decorator.
34+ */
2935 public function __construct (UnitDecorator $ unitDecorator = null )
3036 {
3137 $ this
@@ -34,51 +40,105 @@ public function __construct(UnitDecorator $unitDecorator = null)
3440 ;
3541 }
3642
43+ /**
44+ * Formats the specified number of bytes as a human-readable string.
45+ *
46+ * @param int $bytes Number of bytes.
47+ * @param int|null $precision Optional. Number of fractional digits.
48+ *
49+ * @return string Formatted bytes.
50+ */
3751 public function format ($ bytes , $ precision = null )
3852 {
39- $ precision = $ precision === null ? $ this ->precision : $ precision ;
53+ // Use default precision when not specified.
54+ $ precision === null && $ precision = $ this ->precision ;
55+
4056 $ log = log ($ bytes , $ this ->base );
4157 $ exponent = max (0 , $ log |0 );
4258 $ value = round (pow ($ this ->base , $ log - $ exponent ), $ precision );
4359 $ units = $ this ->getUnitDecorator ()->decorate ($ exponent , $ this ->base , $ value );
4460
45- return trim (sprintf ($ this ->normalizedFormat , $ value , $ units ));
61+ return trim (sprintf ($ this ->sprintfFormat , $ value , $ units ));
4662 }
4763
48- private function normalizeFormat ($ format )
64+ /**
65+ * Coverts a format specifier into a sprintf() compatible format.
66+ *
67+ * @param string $format Format specifier.
68+ *
69+ * @return string sprintf() format.
70+ */
71+ private function convertFormat ($ format )
4972 {
5073 return str_replace (['%v ' , '%u ' ], ['%1$s ' , '%2$s ' ], $ format );
5174 }
5275
76+ /**
77+ * Gets the exponentiation base.
78+ *
79+ * @return int Exponentiation base.
80+ */
5381 public function getBase ()
5482 {
5583 return $ this ->base ;
5684 }
5785
86+ /**
87+ * Sets the exponentiation base which should usually be a Base constant.
88+ *
89+ * @param int $base Exponentiation base.
90+ *
91+ * @return $this
92+ */
5893 public function setBase ($ base )
5994 {
6095 $ this ->base = $ base |0 ;
6196
6297 return $ this ;
6398 }
6499
100+ /**
101+ * Gets the format specifier.
102+ *
103+ * @return string Format specifier.
104+ */
65105 public function getFormat ()
66106 {
67107 return $ this ->format ;
68108 }
69109
110+ /**
111+ * Sets the format specifier. Occurrences of %v and %u will be replaced
112+ * with formatted byte values and units, respectively.
113+ *
114+ * @param string $format Format specifier.
115+ *
116+ * @return $this
117+ */
70118 public function setFormat ($ format )
71119 {
72- $ this ->normalizedFormat = $ this ->normalizeFormat ($ this ->format = "$ format " );
120+ $ this ->sprintfFormat = $ this ->convertFormat ($ this ->format = "$ format " );
73121
74122 return $ this ;
75123 }
76124
125+ /**
126+ * Gets the maximum number of fractional digits.
127+ *
128+ * @return int Fractional digits.
129+ */
77130 public function getPrecision ()
78131 {
79132 return $ this ->precision ;
80133 }
81134
135+ /**
136+ * Sets the maximum number of fractional digits.
137+ *
138+ * @param $precision
139+ *
140+ * @return $this
141+ */
82142 public function setPrecision ($ precision )
83143 {
84144 $ this ->precision = $ precision |0 ;
@@ -87,16 +147,25 @@ public function setPrecision($precision)
87147 }
88148
89149 /**
150+ * Gets the unit decorator.
151+ *
90152 * @return UnitDecorator
91153 */
92154 public function getUnitDecorator ()
93155 {
94156 return $ this ->unitDecorator ;
95157 }
96158
97- public function setUnitDecorator (UnitDecorator $ collection )
159+ /**
160+ * Sets the unit decorator.
161+ *
162+ * @param UnitDecorator $decorator Unit decorator.
163+ *
164+ * @return $this
165+ */
166+ public function setUnitDecorator (UnitDecorator $ decorator )
98167 {
99- $ this ->unitDecorator = $ collection ;
168+ $ this ->unitDecorator = $ decorator ;
100169
101170 return $ this ;
102171 }
0 commit comments