Passed
Push — master ( c312a7...6efd67 )
by Arnold
01:53
created

objectify()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 12
nop 1
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 7
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Jasny;
6
7
/**
8
 * Check if variable is an associative array.
9
 *
10
 * @param array|mixed $var
11
 * @return bool
12
 */
13
function is_associative_array($var): bool
14
{
15 8
    if (!is_array($var)) {
16 2
        return false;
17
    }
18
    
19 6
    $keys = array_keys($var);
20 6
    return ($keys !== array_keys($keys));
21
}
22
23
/**
24
 * Check if variable is a numeric array.
25
 *
26
 * @param array|mixed $var
27
 * @return bool
28
 */
29
function is_numeric_array($var): bool
30
{
31 8
    if (!is_array($var)) {
32 2
        return false;
33
    }
34
    
35 6
    $keys = array_keys($var);
36 6
    return ($keys === array_keys($keys));
37
}
38
39
/**
40
 * Turn associated array into stdClass object recursively.
41
 *
42
 * @param array|mixed $var
43
 * @return \stdClass|mixed
44
 */
45
function objectify($var)
46
{
47 9
    $i = func_num_args() > 1 ? func_get_arg(1) : 100;
48
    
49 9
    if ($i <= 0) {
50 1
        throw new \OverflowException("Maximum recursion depth reached. Possible circular reference.");
51
    }
52
    
53 9
    if (!is_array($var) && !is_object($var)) {
54 8
        return $var;
55
    }
56
    
57 8
    if (is_associative_array($var)) {
58 5
        $var = (object)$var;
59
    }
60
    
61 8
    foreach ($var as &$value) {
62 7
        $value = objectify($value, $i - 1);
63
    }
64
    
65 7
    return $var;
66
}
67
68
/**
69
 * Turn stdClass object into associated array recursively.
70
 *
71
 * @param \stdClass|mixed $var
72
 * @return array|mixed
73
 */
74
function arrayify($var)
75
{
76 7
    $i = func_num_args() > 1 ? func_get_arg(1) : 100;
77
    
78 7
    if ($i <= 0) {
79 1
        throw new \OverflowException("Maximum recursion depth reached. Possible circular reference.");
80
    }
81
    
82 7
    if (!is_array($var) && !is_object($var)) {
83 6
        return $var;
84
    }
85
    
86 6
    if ($var instanceof \stdClass) {
87 6
        $var = (array)$var;
88
    }
89
    
90 6
    foreach ($var as &$value) {
91 5
        $value = arrayify($value, $i - 1);
92
    }
93
    
94 5
    return $var;
95
}
96
97
/**
98
 * Check that an argument has a specific type, otherwise throw an exception.
99
 *
100
 * @param mixed           $var
101
 * @param string|string[] $type
102
 * @param string          $throwable  Class name
103
 * @param string          $message
104
 * @return void
105
 * @throws \InvalidArgumentException
106
 */
107
function expect_type($var, $type, string $throwable = \TypeError::class, string $message = null): void
108
{
109 9
    $strTypes = [];
110 9
    $types = is_scalar($type) ? [$type] : $type;
111
    
112 9
    foreach ($types as $curtype) {
113 9
        $fn = $curtype === 'boolean' ? 'is_bool' : 'is_' . $curtype;
114 9
        $internal = function_exists($fn);
115
        
116 9
        if ($internal ? $fn($var) : is_a($var, $curtype)) {
117 5
            return; // Valid type
118
        }
119
        
120 4
        $strTypes[] = $curtype . ($internal ? '' : ' object');
121
    }
122
    
123 4
    $message = $message ?: "Expected " . array_join_pretty(', ', ' or ', $strTypes) . ", %s given";
124 4
    $varType = (is_object($var) ? get_class($var) . " " : "") . gettype($var);
125
    
126 4
    throw new $throwable(sprintf($message, $varType));
127
}
128