Completed
Push — master ( 6efa89...d59b5b )
by ARCANEDEV
9s
created

helpers.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/* ------------------------------------------------------------------------------------------------
4
 |  SYSTEM
5
 | ------------------------------------------------------------------------------------------------
6
 */
7
if ( ! function_exists('is_testing')) {
8
    /**
9
     * Check if testing environment.
10
     *
11
     * @return bool
12
     */
13
    function is_testing()
14
    {
15
        return getenv('APP_ENV') === 'testing';
16
    }
17
}
18
19
if ( ! function_exists('is_hhvm')) {
20
    /**
21
     * Check if running on HHVM env.
22
     *
23
     * @return bool
24
     */
25
    function is_hhvm()
26
    {
27
        return defined('HHVM_VERSION');
28
    }
29
}
30
31
/* ------------------------------------------------------------------------------------------------
32
 |  STRINGS
33
 | ------------------------------------------------------------------------------------------------
34
 */
35
if ( ! function_exists('str_utf8')) {
36
    /**
37
     * Encoding string to UTF-8.
38
     *
39
     * @param  string|mixed  $string
40
     *
41
     * @return string
42
     */
43
    function str_utf8($string)
44
    {
45
        if (is_string($string) && mb_detect_encoding($string, 'UTF-8', true) !== 'UTF-8') {
46
            $string = utf8_encode($string);
0 ignored issues
show
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
47
        }
48
49
        return $string;
50
    }
51
}
52
53
if ( ! function_exists('str_parse_url')) {
54
    /**
55
     * Parse url with queries.
56
     *
57
     * @param  string       $baseUrl
58
     * @param  array|mixed  $queries
59
     *
60
     * @return string
61
     */
62
    function str_parse_url($baseUrl, $queries = [])
63
    {
64
        if ( ! is_string($baseUrl) || empty($queries)) {
65
            return $baseUrl;
66
        }
67
68
        return $baseUrl . '?'. str_url_queries($queries);
69
    }
70
}
71
72
if ( ! function_exists('str_url_queries')) {
73
    /**
74
     *  A query string, essentially.
75
     *
76
     * @param  array|mixed  $queries  An map of param keys to values.
77
     * @param  string|null  $prefix
78
     *
79
     * @return string
80
     */
81
    function str_url_queries($queries, $prefix = null)
82
    {
83
        if ( ! is_array($queries)) return $queries;
84
85
        $output = [];
86
87
        foreach ($queries as $key => $value) {
88
            if (is_null($value)) continue;
89
90
            if ($prefix) {
91
                $key = $prefix . (($key && ! is_int($key)) ? "[$key]" : '[]');
92
            }
93
94
            $output[] = is_array($value)
95
                ? str_url_queries($value, $key)
96
                : urlencode($key) . '=' . urlencode($value);
97
        }
98
99
        return implode('&', $output);
100
    }
101
}
102
103
if ( ! function_exists('str_split_camelcase')) {
104
    /**
105
     * Split Camel Case String.
106
     *
107
     * @param  string $string
108
     * @param  string $glue
109
     *
110
     * @return string
111
     */
112
    function str_split_camelcase($string, $glue = ' ')
113
    {
114
        if ( ! is_string($string)) return $string;
115
116
        $string = preg_split('/(?<=\\w)(?=[A-Z])/', trim($string));
0 ignored issues
show
Consider using a different name than the parameter $string. This often makes code more readable.
Loading history...
117
118
        return implode($glue, $string);
119
    }
120
}
121
122
/* ------------------------------------------------------------------------------------------------
123
 |  Array
124
 | ------------------------------------------------------------------------------------------------
125
 */
126
if ( ! function_exists('is_multi_dim_array')) {
127
    /**
128
     * Check if array is a multidimensional array.
129
     *
130
     * @param  array  $array
131
     *
132
     * @return bool
133
     */
134
    function is_multi_dim_array($array)
135
    {
136
        if ( ! is_array($array)) return false;
137
138
        $array = array_filter($array, 'is_array');
0 ignored issues
show
Consider using a different name than the parameter $array. This often makes code more readable.
Loading history...
139
140
        return (bool) count($array);
141
    }
142
}
143
144
if ( ! function_exists('is_assoc_array')) {
145
    /**
146
     * Check if array is an associative array.
147
     *
148
     * @param  array  $array
149
     *
150
     * @return bool
151
     */
152
    function is_assoc_array($array)
153
    {
154
        if ( ! is_array($array)) return false;
155
156
        $array = array_filter(array_keys($array), 'is_string');
0 ignored issues
show
Consider using a different name than the parameter $array. This often makes code more readable.
Loading history...
157
158
        return (bool) count($array);
159
    }
160
}
161
162
/* ------------------------------------------------------------------------------------------------
163
 |  Validations
164
 | ------------------------------------------------------------------------------------------------
165
 */
166
if ( ! function_exists('validate_url')) {
167
    /**
168
     * Check if url is valid.
169
     *
170
     * @param  string  $url
171
     *
172
     * @return bool
173
     */
174
    function validate_url($url)
175
    {
176
        return (bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
177
    }
178
}
179
180
if ( ! function_exists('validate_version')) {
181
    /**
182
     * Check if version is valid.
183
     *
184
     * @param  string  $version
185
     *
186
     * @return bool
187
     */
188
    function validate_version($version)
189
    {
190
        if ( ! is_string($version)) return false;
191
192
        // Format [x.x.x] - no beta & no release candidate
193
        preg_match('/(\d+.){2}\d+/', $version, $matches);
194
195
        return isset($matches[0]);
196
    }
197
}
198
199
if ( ! function_exists('validate_bool')) {
200
    /**
201
     * Validate boolean.
202
     *
203
     * @param  bool|mixed  $value
204
     *
205
     * @return bool
206
     */
207
    function validate_bool($value)
208
    {
209
        if ( ! is_bool($value)) {
210
            $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
0 ignored issues
show
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
211
        }
212
213
        return (bool) $value;
214
    }
215
}
216