Passed
Push — main ( 54d11e...e3fc5a )
by Sergey
11:08 queued 06:53
created

Utils::EnsureArray()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

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