• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14131982970

28 Mar 2025 03:08PM UTC coverage: 8.983% (+0.5%) from 8.517%
14131982970

push

github

soyuka
docs: changelog 4.1.4

13389 of 149040 relevant lines covered (8.98%)

24.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/src/State/Util/RequestParser.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\State\Util;
15

16
use Symfony\Component\HttpFoundation\Request;
17

18
/**
19
 * Utility functions for working with Symfony's HttpFoundation request.
20
 *
21
 * @internal
22
 *
23
 * @author Teoh Han Hui <teohhanhui@gmail.com>
24
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
25
 */
26
final class RequestParser
27
{
28
    private function __construct()
29
    {
30
    }
×
31

32
    /**
33
     * Parses request parameters from the specified source.
34
     *
35
     * @author Rok Kralj
36
     *
37
     * @see https://stackoverflow.com/a/18209799/1529493
38
     */
39
    public static function parseRequestParams(string $source): array
40
    {
41
        // '[' is urlencoded ('%5B') in the input, but we must urldecode it in order
42
        // to find it when replacing names with the regexp below.
43
        $source = str_replace('%5B', '[', $source);
620✔
44

45
        $source = preg_replace_callback(
620✔
46
            '/(^|(?<=&))[^=[&]+/',
620✔
47
            static fn ($key): string => bin2hex(urldecode($key[0])),
620✔
48
            $source
620✔
49
        );
620✔
50

51
        // parse_str urldecodes both keys and values in resulting array.
52
        parse_str($source, $params);
620✔
53

54
        return array_combine(array_map('hex2bin', array_keys($params)), $params);
620✔
55
    }
56

57
    /**
58
     * Generates the normalized query string for the Request.
59
     *
60
     * It builds a normalized query string, where keys/value pairs are alphabetized
61
     * and have consistent escaping.
62
     */
63
    public static function getQueryString(Request $request): ?string
64
    {
65
        $qs = $request->server->get('QUERY_STRING', '');
1,700✔
66
        if ('' === $qs) {
1,700✔
67
            return null;
1,086✔
68
        }
69

70
        $parts = [];
620✔
71

72
        foreach (explode('&', (string) $qs) as $param) {
620✔
73
            if ('' === $param || '=' === $param[0]) {
620✔
74
                // Ignore useless delimiters, e.g. "x=y&".
75
                // Also ignore pairs with empty key, even if there was a value, e.g. "=value", as such nameless values cannot be retrieved anyway.
76
                // PHP also does not include them when building _GET.
77
                continue;
×
78
            }
79

80
            $keyValuePair = explode('=', $param, 2);
620✔
81

82
            // GET parameters, that are submitted from a HTML form, encode spaces as "+" by default (as defined in enctype application/x-www-form-urlencoded).
83
            // PHP also converts "+" to spaces when filling the global _GET or when using the function parse_str. This is why we use urldecode and then normalize to
84
            // RFC 3986 with rawurlencode.
85
            $parts[] = isset($keyValuePair[1]) ?
620✔
86
                rawurlencode(urldecode($keyValuePair[0])).'='.rawurlencode(urldecode($keyValuePair[1])) :
612✔
87
                rawurlencode(urldecode($keyValuePair[0]));
26✔
88
        }
89

90
        return implode('&', $parts);
620✔
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc