standardize_booleans()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9332
c 1
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
if (! function_exists('response_to_collect')) {
4
    /**
5
     * Return PhpIPAM response content into a collect of the specified class.
6
     *
7
     * @param $response
8
     * @param $class
9
     * @param string $key
10
     * @return \Illuminate\Support\Collection
11
     */
12
    function response_to_collect($response, $class = null, $key = 'data')
13
    {
14
        $collection = collect();
15
16
        if (array_key_exists($key, $response)) {
17
            foreach ($response[$key] as $item) {
18
                $collection->push($class ? new $class($item) : $item);
19
            }
20
        }
21
22
        return $collection;
23
    }
24
}
25
26
if (! function_exists('get_key_or_null')) {
27
    function get_key_or_null($response, $key = 'data')
28
    {
29
        return $response['success'] ? $response[$key] : null;
30
    }
31
}
32
33
if (! function_exists('standardize_booleans')) {
34
    function standardize_booleans(array $data)
35
    {
36
        $result = [];
37
38
        foreach (array_keys($data) as $key) {
39
            switch ($data[$key]) {
40
                case "\x00":
41
                case "\0":
42
                    $result[$key] = 0;
43
                    break;
44
                default:
45
                    $result[$key] = $data[$key];
46
            }
47
        }
48
49
        return $result;
50
    }
51
}
52
53
if (! function_exists('unset_null_values')) {
54
    function unset_null_values(array $data)
55
    {
56
        foreach (array_keys($data) as $key) {
57
            if ($data[$key] === null) {
58
                unset($data[$key]);
59
            }
60
        }
61
62
        return $data;
63
    }
64
}
65
66
if (! function_exists('standarize_request_body')) {
67
    function standarize_request_body(array $data)
68
    {
69
        return standardize_booleans(unset_null_values($data));
70
    }
71
}
72
73
if (! function_exists('get_id_from_variable')) {
74
    function get_id_from_variable($data)
75
    {
76
        if (is_object($data)) {
77
            return $data->id;
78
        } elseif (is_array($data)) {
79
            return $data['id'];
80
        } else {
81
            return $data;
82
        }
83
    }
84
}
85