@@ -17,15 +17,21 @@ class ByteFormatter
17
17
/** @var string */
18
18
private $ format ;
19
19
20
+ /** @var string */
21
+ private $ sprintfFormat ;
22
+
20
23
/** @var int */
21
24
private $ precision = 0 ;
22
25
23
- /** @var string */
24
- private $ normalizedFormat ;
25
-
26
26
/** @var UnitDecorator */
27
27
private $ unitDecorator ;
28
28
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
+ */
29
35
public function __construct (UnitDecorator $ unitDecorator = null )
30
36
{
31
37
$ this
@@ -34,51 +40,105 @@ public function __construct(UnitDecorator $unitDecorator = null)
34
40
;
35
41
}
36
42
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
+ */
37
51
public function format ($ bytes , $ precision = null )
38
52
{
39
- $ precision = $ precision === null ? $ this ->precision : $ precision ;
53
+ // Use default precision when not specified.
54
+ $ precision === null && $ precision = $ this ->precision ;
55
+
40
56
$ log = log ($ bytes , $ this ->base );
41
57
$ exponent = max (0 , $ log |0 );
42
58
$ value = round (pow ($ this ->base , $ log - $ exponent ), $ precision );
43
59
$ units = $ this ->getUnitDecorator ()->decorate ($ exponent , $ this ->base , $ value );
44
60
45
- return trim (sprintf ($ this ->normalizedFormat , $ value , $ units ));
61
+ return trim (sprintf ($ this ->sprintfFormat , $ value , $ units ));
46
62
}
47
63
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 )
49
72
{
50
73
return str_replace (['%v ' , '%u ' ], ['%1$s ' , '%2$s ' ], $ format );
51
74
}
52
75
76
+ /**
77
+ * Gets the exponentiation base.
78
+ *
79
+ * @return int Exponentiation base.
80
+ */
53
81
public function getBase ()
54
82
{
55
83
return $ this ->base ;
56
84
}
57
85
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
+ */
58
93
public function setBase ($ base )
59
94
{
60
95
$ this ->base = $ base |0 ;
61
96
62
97
return $ this ;
63
98
}
64
99
100
+ /**
101
+ * Gets the format specifier.
102
+ *
103
+ * @return string Format specifier.
104
+ */
65
105
public function getFormat ()
66
106
{
67
107
return $ this ->format ;
68
108
}
69
109
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
+ */
70
118
public function setFormat ($ format )
71
119
{
72
- $ this ->normalizedFormat = $ this ->normalizeFormat ($ this ->format = "$ format " );
120
+ $ this ->sprintfFormat = $ this ->convertFormat ($ this ->format = "$ format " );
73
121
74
122
return $ this ;
75
123
}
76
124
125
+ /**
126
+ * Gets the maximum number of fractional digits.
127
+ *
128
+ * @return int Fractional digits.
129
+ */
77
130
public function getPrecision ()
78
131
{
79
132
return $ this ->precision ;
80
133
}
81
134
135
+ /**
136
+ * Sets the maximum number of fractional digits.
137
+ *
138
+ * @param $precision
139
+ *
140
+ * @return $this
141
+ */
82
142
public function setPrecision ($ precision )
83
143
{
84
144
$ this ->precision = $ precision |0 ;
@@ -87,16 +147,25 @@ public function setPrecision($precision)
87
147
}
88
148
89
149
/**
150
+ * Gets the unit decorator.
151
+ *
90
152
* @return UnitDecorator
91
153
*/
92
154
public function getUnitDecorator ()
93
155
{
94
156
return $ this ->unitDecorator ;
95
157
}
96
158
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 )
98
167
{
99
- $ this ->unitDecorator = $ collection ;
168
+ $ this ->unitDecorator = $ decorator ;
100
169
101
170
return $ this ;
102
171
}
0 commit comments