Passed
Push — main ( 43c69a...a0bf98 )
by Sergey
02:27
created

Utils::EnsureArray()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 8.4444
cc 8
nc 6
nop 2
1
<?php
2
3
namespace Gurucomkz\EagerLoading;
4
5
class Utils {
6
7
    public static function EnsureArray($arr, $kfield = null)
8
    {
9
        if (is_array($arr)) {
10
            return $arr;
11
        }
12
        $result = [];
13
        foreach ($arr as $k => $v){
14
            $key = $k;
15
            if ($kfield!==null) {
16
                if (is_array($v) && isset($v[$kfield])) $key = $v[$kfield];
17
                elseif (is_object($v) && isset($v->$kfield)) $key = $v->$kfield;
18
            }
19
            $result[$key] = $v;
20
        }
21
        return $result;
22
    }
23
24
    public static function extractField($arr,$field)
25
    {
26
        $result = [];
27
        foreach ($arr as $record){
28
            $result[is_object($record) ? $record->ID : $record['ID']] = is_object($record) ? $record->$field : $record[$field];
29
        }
30
        return $result;
31
    }
32
33
}
34