ArrayUtils::createArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 8
cp 0
crap 12
rs 9.9666
1
<?php
2
3
namespace App\Utils;
4
5
use Closure;
6
use InvalidArgumentException;
7
class ArrayUtils {
8
9
    public static function apply(array &$items, Closure $closure): void {
10
        foreach($items as $item) {
11
            $closure($item);
12
        }
13
    }
14
15
    public static function createArray(array $keys, array $values): array {
16
        $array = [ ];
17
        $count = count($keys);
18
19
        $keys = array_values($keys);
20
        $values = array_values($values);
21
22
        if(count($keys) !== count($values)) {
23
            throw new InvalidArgumentException('$keys and $items parameter need to have the same length.');
24
        }
25
26
        for($i = 0; $i < $count; $i++) {
27
            $array[$keys[$i]] = $values[$i];
28
        }
29
30
        return $array;
31
    }
32
33
    public static function createArrayWithKeys(array $items, Closure $keyFunc): array {
34
        $array = [ ];
35
36
        foreach($items as $item) {
37
            $key = $keyFunc($item);
38
            $array[$key] = $item;
39
        }
40
41
        return $array;
42
    }
43
}