Issues (7)

src/helpers.php (1 issue)

Severity
1
<?php
2
3
    if (!function_exists('env')) {
4
        /**
5
         * Get an environment variable
6
         * or a default if that variable is not set
7
         *
8
         * @param string $var_name
9
         * @param mixed  $default
10
         *
11
         * @return mixed
12
         */
13
        function env($var_name, $default = null) {
14
            $value = getenv($var_name);
15
16
            if (is_null($value)) {
0 ignored issues
show
The condition is_null($value) is always false.
Loading history...
17
                return $default;
18
            }
19
20
            return $value;
21
        }
22
    }
23
24
    if (!function_exists('is_dev')) {
25
        /**
26
         * Returns true if the current environment is development
27
         *
28
         * @return bool
29
         */
30
        function is_dev() {
31
            $env = env('ENVIRONMENT', 'development');
32
33
            return $env === 'development';
34
        }
35
    }
36
37
    if (!function_exists('pr')) {
38
        /**
39
         * PR
40
         * Print data for debugging IFF in Development Environment
41
         *
42
         * @param mixed  $data
43
         * @param string $identifier [optional]
44
         */
45
        function pr($data, $identifier = '') {
46
            if (is_dev()) {
47
                \rAPId\Debug\Output::variable($data, $identifier);
48
            }
49
        }
50
    }
51
52
    if (!function_exists('array_get')) {
53
        /**
54
         * Get value from array without boilerplate error checking
55
         *
56
         * @param array  $array
57
         * @param string $key
58
         * @param mixed  $default
59
         *
60
         * @return mixed|null
61
         */
62
        function array_get($array, $key, $default = null) {
63
            if (is_array($array) && isset($array[ $key ])) {
64
                return $array[ $key ];
65
            }
66
67
            return $default;
68
        }
69
    }
70
71
    if (!function_exists('studly_case')) {
72
        /**
73
         * Convert string to StudlyCase
74
         *
75
         * @param string $str
76
         *
77
         * @return string
78
         */
79
        function studly_case($str) {
80
            $words = str_replace(['_', '-'], ' ', $str);
81
82
            return str_replace(' ', '', ucwords($words));
83
        }
84
    }
85
86
    if (!function_exists('camel_case')) {
87
        /**
88
         * convert string to camelCase
89
         *
90
         * @param string $str
91
         *
92
         * @return string
93
         */
94
        function camel_case($str) {
95
            return lcfirst(studly_case($str));
96
        }
97
    }
98
99
    if (!function_exists('snake_case')) {
100
        /**
101
         * @param $str
102
         *
103
         * @return null|string|string[]
104
         */
105
        function snake_case($str) {
106
107
            $str = str_replace(['-', ' '], '_', $str);
108
            $str = preg_replace('/(.)(?=[A-Z])/u', '$1_', $str);
109
110
            return strtolower($str);
111
        }
112
    }
113
114
    if (!function_exists('merge')) {
115
        /**
116
         * merge n vars into an array without endless boilerplate error checking
117
         *
118
         * example:
119
         *
120
         *  merge([1,2],['a' => 'b'],'just a string', null);
121
         *
122
         *  [
123
         *    0 => 1,
124
         *    1 => 2,
125
         *   'a' => 'b',
126
         *    2 => 'just a string
127
         *  ]
128
         *
129
         *
130
         * @param array ...$vars
131
         *
132
         * @return array
133
         */
134
        function merge(...$vars) {
135
            if (empty($vars)) {
136
                return [];
137
            }
138
            $result = [];
139
            foreach ($vars as $arr) {
140
                if (is_null($arr)) continue;
141
142
                $arr = array_wrap($arr);
143
                $result = array_merge($result, $arr);
144
            }
145
146
            return $result;
147
        }
148
    }
149
150
    if (!function_exists('merge_non_empty')) {
151
        /**
152
         * merge() and discard all empty() values
153
         *
154
         * @param array ...$vars
155
         *
156
         * @return array
157
         */
158
        function merge_non_empty(...$vars) {
159
            if (empty($vars)) {
160
                return [];
161
            }
162
            $result = [];
163
            foreach ($vars as $arr) {
164
                if (!empty($arr)) {
165
                    $result = merge($result, $arr);
166
                }
167
            }
168
169
            return $result;
170
        }
171
172
        if (!function_exists('get_object_as_array')) {
173
            /**
174
             * Return an array of all public properties of an object
175
             *
176
             * @param object $obj
177
             *
178
             * @return array
179
             */
180
            function get_object_as_array($obj) {
181
                $result = [];
182
                foreach ($obj as $key => $value) {
183
                    if (is_object($value)) {
184
                        $result[ $key ] = get_object_as_array($value);
185
                    } else {
186
                        $result[ $key ] = $value;
187
                    }
188
                }
189
190
                return $result;
191
            }
192
        }
193
194
        if (!function_exists('array_wrap')) {
195
            /**
196
             * If the given value is not an array
197
             * it will be returned wrapped in an array
198
             *
199
             * If the value is already an array it will
200
             * be returned as is
201
             *
202
             * If the value is null, an empty array will
203
             * be returned
204
             *
205
             * @param mixed $value
206
             *
207
             * @return array
208
             */
209
            function array_wrap($value) {
210
                if (is_null($value)) {
211
                    return [];
212
                }
213
214
                if (is_array($value)) {
215
                    return $value;
216
                }
217
218
                return [$value];
219
            }
220
        }
221
222
        if (!function_exists('db')) {
223
            /**
224
             * Get an EasyDB instance for the given key
225
             *
226
             * @param string $key
227
             *        this is the key used in your database config
228
             *
229
             * @return \EasyDb\Core\DB
230
             */
231
            function db($key = 'primary') {
232
                return \rAPId\Data\DB::getDB($key);
233
            }
234
        }
235
236
        function rAPId_bootstrap()
237
        {
238
            require 'bootstrap.php';
239
        }
240
    }