Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 61... Línea 61...
61
     *
61
     *
62
     * This function can use the return value of `parse()` to build a query
62
     * This function can use the return value of `parse()` to build a query
63
     * string. This function does not modify the provided keys when an array is
63
     * string. This function does not modify the provided keys when an array is
64
     * encountered (like `http_build_query()` would).
64
     * encountered (like `http_build_query()` would).
65
     *
65
     *
66
     * @param array     $params   Query string parameters.
66
     * @param array     $params           Query string parameters.
67
     * @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
67
     * @param int|false $encoding         Set to false to not encode,
-
 
68
     *                                    PHP_QUERY_RFC3986 to encode using
68
     *                            to encode using RFC3986, or PHP_QUERY_RFC1738
69
     *                                    RFC3986, or PHP_QUERY_RFC1738 to
69
     *                            to encode using RFC1738.
70
     *                                    encode using RFC1738.
-
 
71
     * @param bool      $treatBoolsAsInts Set to true to encode as 0/1, and
-
 
72
     *                                    false as false/true.
70
     */
73
     */
71
    public static function build(array $params, $encoding = PHP_QUERY_RFC3986): string
74
    public static function build(array $params, $encoding = PHP_QUERY_RFC3986, bool $treatBoolsAsInts = true): string
72
    {
75
    {
73
        if (!$params) {
76
        if (!$params) {
74
            return '';
77
            return '';
75
        }
78
        }
Línea 84... Línea 87...
84
            $encoder = 'urlencode';
87
            $encoder = 'urlencode';
85
        } else {
88
        } else {
86
            throw new \InvalidArgumentException('Invalid type');
89
            throw new \InvalidArgumentException('Invalid type');
87
        }
90
        }
Línea -... Línea 91...
-
 
91
 
-
 
92
        $castBool = $treatBoolsAsInts ? static function ($v) { return (int) $v; } : static function ($v) { return $v ? 'true' : 'false'; };
88
 
93
 
89
        $qs = '';
94
        $qs = '';
90
        foreach ($params as $k => $v) {
95
        foreach ($params as $k => $v) {
91
            $k = $encoder((string) $k);
96
            $k = $encoder((string) $k);
92
            if (!is_array($v)) {
97
            if (!is_array($v)) {
93
                $qs .= $k;
98
                $qs .= $k;
94
                $v = is_bool($v) ? (int) $v : $v;
99
                $v = is_bool($v) ? $castBool($v) : $v;
95
                if ($v !== null) {
100
                if ($v !== null) {
96
                    $qs .= '=' . $encoder((string) $v);
101
                    $qs .= '='.$encoder((string) $v);
97
                }
102
                }
98
                $qs .= '&';
103
                $qs .= '&';
99
            } else {
104
            } else {
100
                foreach ($v as $vv) {
105
                foreach ($v as $vv) {
101
                    $qs .= $k;
106
                    $qs .= $k;
102
                    $vv = is_bool($vv) ? (int) $vv : $vv;
107
                    $vv = is_bool($vv) ? $castBool($vv) : $vv;
103
                    if ($vv !== null) {
108
                    if ($vv !== null) {
104
                        $qs .= '=' . $encoder((string) $vv);
109
                        $qs .= '='.$encoder((string) $vv);
105
                    }
110
                    }
106
                    $qs .= '&';
111
                    $qs .= '&';
107
                }
112
                }
108
            }
113
            }