Collectors::average()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections\Functions;
7
8
use Closure;
9
use WS\Utils\Collections\Collection;
10
use WS\Utils\Collections\HashMap;
11
use WS\Utils\Collections\Map;
12
13
/**
14
 * Class Aggregators
15
 * This class consist of static methods with < Fn(...$args: mixed): <Fn($c: Collection): mixed> >
16
 * @package WS\Utils\Collections\Functions
17
 */
18
class Collectors
19
{
20
    /**
21
     * Returns function with interface <Fn($c: Collection): string> for concatenating collection strings
22
     * @param string $delimiter
23
     * @return Closure
24
     */
25 7
    public static function concat(string $delimiter = ''): Closure
26
    {
27
        return static function (Collection $collection) use ($delimiter) {
28 6
            return implode($delimiter, $collection->toArray());
29 7
        };
30
    }
31
32
    /**
33
     * Returns closure <Fn($c: Collection): float> for getting average collection value
34
     * @return Closure
35
     */
36 2
    public static function average(): Closure
37
    {
38
        /**
39
         * @param Collection $collection
40
         * @return float|int
41
         */
42
        return static function (Collection $collection) {
43 2
            $array = $collection->toArray();
44
45 2
            return array_sum($array) / count($array);
46 2
        };
47
    }
48
49
    /**
50
     * Returns closure for getting <Fn($c: Collection): Map<value: mixed, repeats: int>>. keys - collection uniq values, values - count of repeats
51
     * @return Closure
52
     */
53 3
    public static function group(): Closure
54
    {
55
        return static function (Collection $collection): Map {
56
            $groupBy = self::groupBy(static function ($el) {
57 3
                return $el;
58 3
            });
59 3
            return $groupBy($collection);
60 3
        };
61
    }
62
63
    /**
64
     * Returns closure for getting map <Fn($c: Collection): Map<value: mixed, repeats: int>>. keys - value uniq fieldName values, values - count of repeats
65
     * @param string $property
66
     * @return Closure
67
     */
68 1
    public static function groupByProperty(string $property): Closure
69
    {
70
        return static function (Collection $collection) use ($property): Map {
71
            $fGetValue = static function ($obj) use ($property) {
72 1
                return ObjectFunctions::getPropertyValue($obj, $property);
73 1
            };
74
75 1
            $groupBy = self::groupBy($fGetValue);
76 1
            return $groupBy($collection);
77 1
        };
78
    }
79
80
    /**
81
     * Returns closure for getting map <Fn($c: Collection): Map<value: mixed, repeats: int>>. keys - value uniq fieldName values, values - count of repeats
82
     * @param callable $f
83
     * @return Closure
84
     */
85 5
    public static function groupBy(callable $f): Closure
86
    {
87
        return static function (Collection $collection) use ($f): Map {
88 5
            $group = new HashMap();
89
            $collection
90 5
                ->stream()
91
                ->each(static function ($el) use ($group, $f) {
92 5
                    $value = $f($el);
93 5
                    $count = 0;
94 5
                    if (($gCount = $group->get($value)) !== null) {
95 5
                        $count = $gCount;
96
                    }
97 5
                    $group->put($value, $count + 1);
98 5
                });
99 5
            return $group;
100 5
        };
101
    }
102
}
103