Passed
Push — master ( 5de612...863f0a )
by Darío
01:40
created

ArrayDimension::objectToArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Util;
12
13
/**
14
 * ArrayDimension class
15
 *
16
 * Utils functions to operate arrays
17
 */
18
class ArrayDimension
19
{
20
    /**
21
     * Converts a multidimensional array in a dimensional array
22
     *
23
     * @param array  $array
24
     * @param string $glue
25
     *
26
     * @return array
27
     */
28
    public static function toUnidimensional(Array $array, $glue)
29
    {
30
        $new_config = [];
31
        $again = false;
32
33
        foreach ($array as $param => $configure)
34
        {
35
            if (is_array($configure))
36
            {
37
                foreach ($configure as $key => $value)
38
                {
39
                    $again = true;
40
                    $new_config[$param . $glue . $key] = $value;
41
                }
42
            }
43
            else
44
                $new_config[$param] = $configure;
45
        }
46
47
        return (!$again) ? $new_config : self::toUnidimensional($new_config, $glue);
48
    }
49
50
    /**
51
     * Search inside a multi-dimensional array a value by nested keys
52
     *
53
     * Default value will be returned if any key in the haystack array does not exists
54
     *
55
     * @param array $needle
56
     * @param array $haystack
57
     * @param mixed $value
58
     *
59
     * @return mixed
60
     */
61
    public static function ifdef(array $needle, array $haystack, $value)
62
    {
63
        $key = array_shift($needle);
64
65
        do
66
        {
67
            if (array_key_exists($key, $haystack))
68
                $haystack = $haystack[$key];
69
            else
70
                return $value;
71
72
            $key = count($needle) ? array_shift($needle) : NULL;
73
74
            if (is_null($key))
75
                return $haystack;
76
77
        } while (!is_null($key));
78
    }
79
80
    /**
81
     * Transforms an object to an array
82
     *
83
     * @param mixed $obj
84
     *
85
     * @return array
86
     */
87
    public static function objectToArray($obj)
88
    {
89
        if (is_object($obj))
90
            $obj = (array) $obj;
91
92
        if (is_array($obj))
93
        {
94
            $new = array();
95
96
            foreach($obj as $key => $val)
97
            {
98
                $new[$key] = self::objectToArray($val);
99
            }
100
        }
101
        else
102
            $new = $obj;
103
104
        return $new;
105
    }
106
}