helpers.php ➔ build_batch_query_string()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
if (! function_exists('build_query_string')) {
4
    /**
5
     * Generate a query string.
6
     *
7
     * @param  array $params
8
     * @param  int   $encoding
9
     * @return string
10
     */
11
    function build_query_string($params = [], $encoding = PHP_QUERY_RFC3986)
12
    {
13
        if (empty($params)) {
14
            return '';
15
        }
16
17
        $query = '';
18
        foreach ($params as $key => $value) {
19
            if (is_array($value)) {
20
                $query .= build_batch_query_string($key, $value, $encoding);
21
            } elseif (!empty($value)) {
22
                $query .= '&' . url_encode($key, $encoding) . '=' . url_encode($value, $encoding);
23
            }
24
        }
25
26
        return $query ?: '';
27
    }
28
}
29
30
if (! function_exists('build_batch_query_string')) {
31
    /**
32
     * Generate a query string for batch requests.
33
     *
34
     * @param  string $key   The name of the query variable.
35
     * @param  array  $items An array of item values for the variable.
36
     * @param  int    $encoding
37
     * @return string
38
     */
39
    function build_batch_query_string($key, $items, $encoding = PHP_QUERY_RFC3986)
40
    {
41
        return array_reduce($items, function ($query, $item) use ($key, $encoding) {
42
            return $query . "&" . url_encode($key, $encoding) . '=' . url_encode($item, $encoding);
43
        }, '');
44
    }
45
}
46
47
if (! function_exists('url_encode')) {
48
    /**
49
     * Url encode a string.
50
     *
51
     * @param  string $value
52
     * @param  int    $encoding
53
     * @return string
54
     */
55
    function url_encode($value, $encoding = PHP_QUERY_RFC3986)
56
    {
57
        switch ($encoding) {
58
            case false:
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $encoding of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
59
                return $value;
60
            case PHP_QUERY_RFC3986:
61
                return rawurlencode($value);
62
            case PHP_QUERY_RFC1738:
63
                return urlencode($value);
64
            default:
65
                throw new \InvalidArgumentException('Invalid type');
66
        }
67
    }
68
}
69
70
if (! function_exists('ms_timestamp')) {
71
    /**
72
     * Get a millisecond timestamp from a date or time.
73
     *
74
     * @param  mixed $time
75
     * @return int
76
     */
77
    function ms_timestamp($time)
78
    {
79
        switch (true) {
80
            case $time instanceof \DateTime:
81
                return $time->getTimestamp() * 1000;
82
            case is_numeric($time) && strlen((string) $time) === 10:
83
                return $time * 1000;
84
            case is_string($time):
85
                return strtotime($time) * 1000;
86
            default:
87
                return $time;
88
        }
89
    }
90
}
91