Passed
Push — master ( aed439...4d7571 )
by Stephen
01:06 queued 29s
created

ArrayHelpers::fromMerge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Helpers\Arrays;
4
5
use Sfneal\Helpers\Arrays\Utils\ArrayUtility;
6
7
class ArrayHelpers
8
{
9
    /**
10
     * Instantiate a `ArrayUtility` instance by passing an $array to the constructor.
11
     *
12
     * @param array $array
13
     * @return ArrayUtility
14
     */
15
    public static function from(array $array): ArrayUtility
16
    {
17
        return new ArrayUtility($array);
18
    }
19
20
    /**
21
     * Instantiate a `ArrayUtility` instance by passing multiple $arrays to the constructor to be merged.
22
     *
23
     * @param mixed ...$arrays
24
     * @return ArrayUtility
25
     */
26
    public static function fromMerge(...$arrays): ArrayUtility
27
    {
28
        return new ArrayUtility(array_merge(...$arrays));
29
    }
30
31
    /**
32
     * Sum the values of two arrays.
33
     *
34
     * @param array $arrays
35
     * @return array
36
     */
37
    public static function sum(...$arrays): array
38
    {
39
        $sum = [];
40
41
        foreach ($arrays as $array) {
42
            foreach ($array as $index => $value) {
43
                if (! array_key_exists($index, $sum)) {
44
                    $sum[$index] = $value;
45
                } else {
46
                    $sum[$index] += $value;
47
                }
48
            }
49
        }
50
51
        return $sum;
52
    }
53
}
54