Completed
Push — master ( 237036...650d4b )
by Dominik
02:47
created

ArrayHelpers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssoc() 0 9 1
A objectToArray() 0 6 1
A indexedValuesToAssocKeys() 0 12 3
1
<?php
2
3
namespace Flynt\Utils;
4
5
class ArrayHelpers
6
{
7
    public static function isAssoc(array $array)
8
    {
9
        // Keys of the array
10
        $keys = array_keys($array);
11
12
        // If the array keys of the keys match the keys, then the array must
13
        // not be associative (e.g. the keys array looked like {0:0, 1:1...}).
14
        return array_keys($keys) !== $keys;
15
    }
16
17
    // only converts first dimension of object
18
    public static function objectToArray($obj)
19
    {
20
        return array_map(function ($val) {
21
            return (array) $val;
22
        }, $obj);
23
    }
24
25
    public static function indexedValuesToAssocKeys(array $array)
26
    {
27
        $values = array_map(function ($value) {
28
            return is_array($value) ? $value : [];
29
        }, $array);
30
31
        $keys = array_map(function ($key) use ($array) {
32
            return is_int($key) ? $array[$key] : $key;
33
        }, array_keys($array));
34
35
        return array_combine($keys, $values);
36
    }
37
}
38