-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTransformer.php
More file actions
58 lines (44 loc) · 1.27 KB
/
Transformer.php
File metadata and controls
58 lines (44 loc) · 1.27 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
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark\Transformers;
use DragonCode\Benchmark\Contracts\Transformer as TransformerContract;
use function array_merge;
use function count;
class Transformer
{
public function forTime(array $data): array
{
return $this->resolve(Times::class, $data);
}
public function forStats(array $data): array
{
return $this->resolve(Stats::class, $data);
}
public function forWinners(array $data): array
{
return $this->resolve(Winner::class, $data);
}
public function separator(array $data): array
{
return $this->resolve(Separator::class, $data);
}
public function merge(array ...$arrays): array
{
$result = [];
$count = count($arrays);
$index = 1;
foreach ($arrays as $array) {
if (! empty($array)) {
$result = $index < $count
? array_merge($result, $array, $this->separator($arrays[0]))
: array_merge($result, $array);
}
++$index;
}
return $result;
}
protected function resolve(string|TransformerContract $transformer, array $data): array
{
return (new $transformer)->transform($data);
}
}