1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WS\Utils\Collections\Functions\Group; |
4
|
|
|
|
5
|
|
|
use WS\Utils\Collections\Collection; |
6
|
|
|
use WS\Utils\Collections\CollectionFactory; |
7
|
|
|
use WS\Utils\Collections\Functions\ObjectFunctions; |
8
|
|
|
|
9
|
|
|
class Group |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
private $key; |
13
|
|
|
private $aggregators; |
14
|
|
|
|
15
|
7 |
|
public function __construct(string $key) |
16
|
|
|
{ |
17
|
7 |
|
$this->key = $key; |
18
|
7 |
|
} |
19
|
|
|
|
20
|
7 |
|
public function __invoke(Collection $collection) |
21
|
|
|
{ |
22
|
7 |
|
$groupedResult = []; |
23
|
7 |
|
foreach ($collection as $element) { |
24
|
7 |
|
if (!$groupKey = ObjectFunctions::getPropertyValue($element, $this->key)) { |
25
|
1 |
|
continue; |
26
|
|
|
} |
27
|
7 |
|
if (!isset($groupedResult[$groupKey])) { |
28
|
7 |
|
$groupedResult[$groupKey] = CollectionFactory::empty(); |
29
|
|
|
} |
30
|
7 |
|
$groupedResult[$groupKey]->add($element); |
31
|
|
|
} |
32
|
7 |
|
if (!$this->aggregators) { |
33
|
5 |
|
return $groupedResult; |
34
|
|
|
} |
35
|
2 |
|
$aggregatedResult = []; |
36
|
2 |
|
foreach ($groupedResult as $groupKey => $items) { |
37
|
2 |
|
foreach ($this->aggregators as $item) { |
38
|
2 |
|
[$destKey, $aggregator] = $item; |
39
|
2 |
|
$aggregatedResult[$groupKey][$destKey] = $aggregator($items); |
40
|
|
|
} |
41
|
|
|
} |
42
|
2 |
|
return $aggregatedResult; |
43
|
|
|
} |
44
|
|
|
|
45
|
7 |
|
public static function by(string $key): self |
46
|
|
|
{ |
47
|
7 |
|
return new self($key); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function sum(string $sourceKey, string $destKey = null): self |
51
|
|
|
{ |
52
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Sum($sourceKey)); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
public function min(string $sourceKey, string $destKey = null): self |
56
|
|
|
{ |
57
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Min($sourceKey)); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function max(string $sourceKey, string $destKey = null): self |
61
|
|
|
{ |
62
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Max($sourceKey)); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function avg(string $sourceKey, string $destKey = null): self |
66
|
|
|
{ |
67
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Avg($sourceKey)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function addToSet(string $sourceKey, string $destKey = null): self |
71
|
|
|
{ |
72
|
|
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\AddToSet($sourceKey)); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function first(string $sourceKey, string $destKey = null): self |
76
|
|
|
{ |
77
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\First($sourceKey)); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function last(string $sourceKey, string $destKey = null): self |
81
|
|
|
{ |
82
|
2 |
|
return $this->addAggregator($destKey ?? $sourceKey, new Aggregator\Last($sourceKey)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function count(string $destKey): self |
86
|
|
|
{ |
87
|
|
|
return $this->addAggregator($destKey, new Aggregator\Count()); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function addAggregator(string $destKey, callable $aggregator): self |
91
|
|
|
{ |
92
|
2 |
|
$this->aggregators[] = [$destKey, $aggregator]; |
93
|
2 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|