helpers.php ➔ str_url_queries()   B
last analyzed

Complexity

Conditions 8
Paths 13

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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