Multisort   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 24
cts 24
cp 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B sort() 0 39 10
1
<?php
2
3
namespace UniMan\Core\Utils;
4
5
class Multisort
6
{
7 10
    public static function sort($data, array $sorting = [])
8
    {
9 10
        if (empty($sorting)) {
10 2
            return $data;
11
        }
12
13 8
        foreach ($data as $key => $row) {
14 8
            foreach ($row as $column => $value) {
15 8
                $col = '__' . $column . '__';
16 8
                if (!isset($$col)) {
17 8
                    $$col = [];
18
                }
19 8
                ${$col}[$key] = $value;
20
            }
21
        }
22
23 8
        $args = [];
24 8
        foreach ($sorting as $sort) {
25 8
            foreach ($sort as $key => $direction) {
26 8
                $col = '__' . $key . '__';
27 8
                if (!isset($$col)) {
28 4
                    continue;
29
                }
30 6
                $args[] = $$col;
31 7
                $args[] = strtolower($direction) == 'asc' ? SORT_ASC : SORT_DESC;
32
            }
33
        }
34
35 8
        if (empty($args)) {
36 2
            return $data;
37
        }
38
39 6
        $keys = array_keys($data);
40 6
        $args[] = &$data;
41 6
        $args[] = &$keys;
42
43 6
        call_user_func_array('array_multisort', $args);
44 6
        return array_combine($keys, $data);
45
    }
46
}
47