Passed
Pull Request — master (#20)
by
unknown
03:43
created

Group::addToSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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