-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathView.php
More file actions
91 lines (71 loc) · 2.03 KB
/
View.php
File metadata and controls
91 lines (71 loc) · 2.03 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark\Services;
use DragonCode\Benchmark\View\ProgressBar;
use DragonCode\Benchmark\View\Table;
use Symfony\Component\Console\Style\SymfonyStyle;
use function is_array;
use function is_numeric;
use function round;
use function sprintf;
class View
{
protected Table $table;
protected ProgressBar $progressBar;
protected ?int $roundPrecision = null;
public function __construct(
protected SymfonyStyle $io,
protected Memory $memory = new Memory
) {
$this->table = new Table($this->io);
$this->progressBar = new ProgressBar($this->io);
}
public function setRound(?int $precision): void
{
$this->roundPrecision = $precision;
}
public function table(array $data): void
{
$this->table->show(
$this->appendMs($data)
);
}
public function progressBar(): ProgressBar
{
return $this->progressBar;
}
public function emptyLine(int $count = 1): void
{
$this->io->newLine($count);
}
protected function appendMs(array $data): array
{
foreach ($data as &$values) {
foreach ($values as $key => &$value) {
if ($key === '#' || ! is_array($value)) {
continue;
}
$time = $this->roundTime($value['time']);
$ram = $this->roundRam($value['ram'] ?? null);
$value = ! empty($ram)
? sprintf('%s ms - %s', $time, $ram)
: sprintf('%s ms', $time);
}
}
return $data;
}
protected function roundTime(float $value): float
{
if (is_numeric($this->roundPrecision)) {
return round($value, $this->roundPrecision);
}
return $value;
}
protected function roundRam(float|int|null $bytes): ?string
{
if ($bytes !== null) {
return $this->memory->format((int) $bytes);
}
return null;
}
}