Completed
Push — master ( 1d2e98...a07a70 )
by Chris
01:19
created

array_wrap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
3
    if (!function_exists('pr')) {
4
        /**
5
         * PR
6
         * Prints data wrapped in pre tags. Perfect for reading arrays
7
         *
8
         * @param mixed $data
9
         */
10
        function pr($data) {
11
            if (php_sapi_name() === 'cli') {
12
                echo "\n";
13
                print_r($data);
14
                echo "\n";
15
            } else {
16
                echo "<pre style='font-size: 8pt; text-align: left; background-color: #ffffff;'>";
17
                print_r($data);
18
                echo "</pre>";
19
            }
20
        }
21
    }
22
23
    if (!function_exists('array_get')) {
24
        /**
25
         * Get value from array without boilerplate error checking
26
         *
27
         * @param array  $array
28
         * @param string $key
29
         * @param mixed  $default
30
         *
31
         * @return mixed|null
32
         */
33
        function array_get($array, $key, $default = null) {
34
            if (is_array($array) && isset($array[ $key ])) {
35
                return $array[ $key ];
36
            }
37
38
            return $default;
39
        }
40
    }
41
42
    if (!function_exists('studly_case')) {
43
        /**
44
         * Convert string to StudlyCase
45
         *
46
         * @param string $str
47
         *
48
         * @return string
49
         */
50
        function studly_case($str) {
51
            $words = str_replace(['_', '-'], ' ', $str);
52
53
            return str_replace(' ', '', ucwords($words));
54
        }
55
    }
56
57
    if (!function_exists('camel_case')) {
58
        /**
59
         * convert string to camelCase
60
         *
61
         * @param string $str
62
         *
63
         * @return string
64
         */
65
        function camel_case($str) {
66
            return lcfirst(studly_case($str));
67
        }
68
    }
69
70
    if (!function_exists('snake_case')) {
71
        /**
72
         * @param $str
73
         *
74
         * @return null|string|string[]
75
         */
76
        function snake_case($str) {
77
78
            $str = str_replace(['-', ' '], '_', $str);
79
            $str = preg_replace('/(.)(?=[A-Z])/u', '$1_', $str);
80
81
            return strtolower($str);
82
        }
83
    }
84
85
    if (!function_exists('merge')) {
86
        /**
87
         * merge n vars into an array without endless boilerplate error checking
88
         *
89
         * example:
90
         *
91
         *  merge([1,2],['a' => 'b'],'just a string', null);
92
         *
93
         *  [
94
         *    0 => 1,
95
         *    1 => 2,
96
         *   'a' => 'b',
97
         *    2 => 'just a string
98
         *  ]
99
         *
100
         *
101
         * @param array ...$vars
102
         *
103
         * @return array
104
         */
105
        function merge(...$vars) {
106
            if (empty($vars)) {
107
                return [];
108
            }
109
            $result = [];
110
            foreach ($vars as $arr) {
111
                if (is_null($arr)) continue;
112
                if (!is_array($arr)) {
113
                    $arr = [$arr];
114
                }
115
                $result = array_merge($result, $arr);
116
            }
117
118
            return $result;
119
        }
120
    }
121
122
    if (!function_exists('merge_non_empty')) {
123
        /**
124
         * merge() and discard all empty() values
125
         *
126
         * @param array ...$vars
127
         *
128
         * @return array
129
         */
130
        function merge_non_empty(...$vars) {
131
            if (empty($vars)) {
132
                return [];
133
            }
134
            $result = [];
135
            foreach ($vars as $arr) {
136
                if (!empty($arr)) {
137
                    $result = merge($result, $arr);
138
                }
139
            }
140
141
            return $result;
142
        }
143
144
        if (!function_exists('get_object_as_array')) {
145
            /**
146
             * Return an array of all public properties of an object
147
             *
148
             * @param object $obj
149
             *
150
             * @return array
151
             */
152
            function get_object_as_array($obj) {
153
                $array = (array)$obj;
154
                foreach ($array as $key => $value) {
155
                    // Private and protected property names contain "\0"
156
                    // when cast to array. Discard them here
157
                    if (strpos($key, "\0") !== false) {
158
                        unset($array[ $key ]);
159
                    }
160
                }
161
162
                return $array;
163
            }
164
        }
165
166
        if (!function_exists('array_wrap')) {
167
            /**
168
             * If the given value is not an array
169
             * it will be returned wrapped in an array
170
             *
171
             * If the value is already an array it will
172
             * be returned as is
173
             *
174
             * If the value is null, an empty array will
175
             * be returned
176
             *
177
             * @param mixed $value
178
             *
179
             * @return array
180
             */
181
            function array_wrap($value) {
182
                if (is_null($value)) {
183
                    return [];
184
                }
185
186
                if (is_array($value)) {
187
                    return $value;
188
                }
189
190
                return [$value];
191
            }
192
        }
193
    }