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

Utils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 15
c 1
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractField() 0 7 4
B EnsureArray() 0 15 8
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