-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProgressCounter.php
59 lines (48 loc) · 1.32 KB
/
ProgressCounter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace Concise\Console\ResultPrinter\Utilities;
use DomainException;
use Concise\Validation\ArgumentChecker;
class ProgressCounter
{
/**
* Total value
* @var integer
*/
protected $total;
/**
* @var boolean
*/
protected $showPercentage;
protected function atLeastZero($value, $name)
{
if ($value < 0) {
throw new DomainException("$name must be at least zero.");
}
}
/**
* @param integer $total
* @param boolean $showPercentage
*/
public function __construct($total, $showPercentage = false)
{
ArgumentChecker::check($total, 'integer', 1);
ArgumentChecker::check($showPercentage, 'boolean', 2);
$this->atLeastZero($total, 'Total');
$this->total = $total;
$this->showPercentage = $showPercentage;
}
public function render($value = 0)
{
ArgumentChecker::check($value, 'integer');
$this->atLeastZero($value, 'Value');
$r = $value . ' / ' . max($value, $this->total);
if ($this->showPercentage) {
$r .= sprintf(' (%3s%%)', min(100, $this->getPercentage($value)));
}
return $r;
}
public function getPercentage($value)
{
return (0 === $this->total) ? 0 : floor($value / $this->total * 100);
}
}