-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinner.php
More file actions
71 lines (52 loc) · 1.59 KB
/
Winner.php
File metadata and controls
71 lines (52 loc) · 1.59 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
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark\Transformers;
use DragonCode\Benchmark\Services\Arr;
use function array_keys;
use function compact;
use function sprintf;
class Winner extends Base
{
protected string $key = 'avg';
protected array $orders = [
1 => '<fg=green>- %d -</>',
2 => '<fg=yellow>- %d -</>',
3 => '<fg=blue>- %d -</>',
];
public function __construct(
protected Arr $arr = new Arr
) {}
public function transform(array $data): array
{
$values = $data[$this->key];
$names = $this->prepare($values);
return $this->order($values, $names);
}
protected function order(array $data, array $names): array
{
$items = [];
foreach (array_keys($data) as $key) {
if ($key === '#') {
continue;
}
$position = $names[$key] + 1;
$this->put($items, 'Order', $key, fn () => $this->color($position));
}
return $items;
}
protected function prepare(array $data): array
{
$data = $this->arr->forget($data, '#');
$data = $this->arr->map($data, fn (array $values, mixed $name) => compact('name', 'values'));
$data = $this->arr->sort($data, 'values.time');
$data = $this->arr->pluck($data, 'name');
return $this->arr->flip($data);
}
protected function color(int $order): string
{
if ($template = $this->orders[$order] ?? null) {
return sprintf($template, $order);
}
return sprintf('- %d -', $order);
}
}