Type::arrayWalkRecursive()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php /** MicroType */
2
3
namespace Micro\File;
4
5
/**
6
 * Class Type
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/linpax/microphp-framework
10
 * @copyright Copyright (c) 2013 Oleg Lunegov
11
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
12
 * @package Micro
13
 * @subpackage File
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Type
18
{
19
    /**
20
     * Return concrete object type
21
     *
22
     * @access public
23
     *
24
     * @param mixed $var object to scan
25
     *
26
     * @return string
27
     * @static
28
     */
29
    public static function getType($var)
30
    {
31
        $type = gettype($var);
32
        switch ($type) {
33
            case 'object':
34
                return get_class($var);
35
36
            case 'double':
37
                return is_float($var) ? 'float' : 'double';
38
39
            default:
40
                return strtolower($type);
41
        }
42
    }
43
44
    /**
45
     * Get public vars into object
46
     *
47
     * @access public
48
     *
49
     * @param mixed $object
50
     *
51
     * @return array
52
     * @static
53
     */
54
    public static function getVars($object)
55
    {
56
        return get_object_vars($object);
57
    }
58
59
    /**
60
     * Array walk recursive
61
     *
62
     * @access public
63
     *
64
     * @param array $data array to walk
65
     * @param callable $function callable function
66
     *
67
     * @return array|mixed
68
     * @static
69
     */
70
    public static function arrayWalkRecursive(array $data, callable $function)
71
    {
72
        if (!is_array($data)) {
73
            return call_user_func($function, $data);
74
        }
75
76
        foreach ($data as $k => &$item) {
77
            $item = self::arrayWalkRecursive($data[$k], $function);
78
        }
79
80
        return $data;
81
    }
82
83
    /**
84
     * Get SubArray from array by keys
85
     *
86
     * @access public
87
     *
88
     * @param array $array
89
     * @param array $keys
90
     *
91
     * @return array
92
     * @static
93
     */
94
    public static function getSubArrayFromArrayByKeys(array $array, array $keys)
95
    {
96
        $result = [];
97
98
        foreach ($keys as $key) {
99
            if (array_key_exists($key, $array)) {
100
                $result[$key] = $array[$key];
101
            }
102
        }
103
104
        return $result;
105
    }
106
}
107