@@ -22,9 +22,28 @@ class ProgressBar
2222 public const string RIGHT = "\e[%dC " ;
2323 public const string LEFT = "\e[%dD " ;
2424
25- /** Number of required iterations to add a character */
26- public int $ numberOfEachIterations {
27- get => intval (ceil ($ this ->max / $ this ->numberOfSymbols ));
25+ public int $ max {
26+ set {
27+ if ($ value <= 0 ) {
28+ throw new UnexpectedValueException ('The max value must be positive ' );
29+ }
30+
31+ $ this ->max = $ value ;
32+ }
33+ get => $ this ->max ;
34+ }
35+
36+ public int $ numberOfSymbols {
37+ set {
38+ if ($ value <= 0 ) {
39+ throw new UnexpectedValueException ('The number of symbols must be positive ' );
40+ }
41+
42+ $ this ->numberOfSymbols = $ value ;
43+ }
44+ get {
45+ return $ this ->max <= $ this ->numberOfSymbols ? $ this ->max : $ this ->numberOfSymbols ;
46+ }
2847 }
2948
3049 /** Current iteration */
@@ -84,29 +103,11 @@ public static function getFormattedTime(float $time): string
84103 *
85104 * @throws UnexpectedValueException If `$max` or `$numberOfSymbols` are negative values
86105 */
87- public function __construct (
88- public int $ max {
89- set {
90- if ($ value <= 0 ) {
91- throw new UnexpectedValueException ('The max value must be positive ' );
92- }
93-
94- $ this ->max = $ value ;
95- }
96- },
97- public int $ numberOfSymbols = 50 {
98- set {
99- if ($ value <= 0 ) {
100- throw new UnexpectedValueException ('The number of symbols must be positive ' );
101- }
102-
103- $ this ->numberOfSymbols = $ value ;
104- }
105- get {
106- return $ this ->max <= $ this ->numberOfSymbols ? $ this ->max : $ this ->numberOfSymbols ;
107- }
108- }
109- ) {}
106+ public function __construct (int $ max , int $ numberOfSymbols = 50 )
107+ {
108+ $ this ->max = $ max ;
109+ $ this ->numberOfSymbols = $ numberOfSymbols ;
110+ }
110111
111112 /**
112113 * Starts the progress bar (or restarts it, if not breaks two lines)
@@ -168,7 +169,7 @@ public function advance(int $toAdvance = 1): void
168169 } elseif ($ this ->max === $ this ->numberOfSymbols ) {
169170 print str_repeat ('# ' , $ this ->iteration ) . str_repeat (' ' , $ this ->max - $ this ->iteration );
170171 } else {
171- $ actualDiezes = intval (floor ($ this ->iteration / $ this ->numberOfEachIterations ));
172+ $ actualDiezes = intval (floor ($ this ->iteration / intval ( ceil ( $ this ->max / $ this -> numberOfSymbols )) ));
172173
173174 print str_repeat ('# ' , $ actualDiezes ) . str_repeat (' ' , ($ this ->numberOfSymbols - $ actualDiezes ));
174175 }
0 commit comments