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