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

api-platform / core / 17054069864

18 Aug 2025 10:27PM UTC coverage: 21.952% (+0.2%) from 21.769%
17054069864

Pull #7151

github

web-flow
Merge 0da010d8d into 6491bfc7a
Pull Request #7151: fix: 7119 parameter array shape uses invalid syntax

11524 of 52497 relevant lines covered (21.95%)

11.86 hits per line

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

51.43
/src/Metadata/Util/IriHelper.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\Metadata\Util;
15

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\UrlGeneratorInterface;
18
use ApiPlatform\State\Util\RequestParser;
19

20
/**
21
 * Parses and creates IRIs.
22
 *
23
 * @author Kévin Dunglas <dunglas@gmail.com>
24
 *
25
 * @internal
26
 */
27
final class IriHelper
28
{
29
    private function __construct()
30
    {
31
    }
×
32

33
    /**
34
     * Parses and standardizes the request IRI.
35
     *
36
     * @throws InvalidArgumentException
37
     */
38
    public static function parseIri(string $iri, string $pageParameterName): array
39
    {
40
        $parts = parse_url($iri);
132✔
41
        if (false === $parts) {
132✔
42
            throw new InvalidArgumentException(\sprintf('The request URI "%s" is malformed.', $iri));
×
43
        }
44

45
        $parameters = [];
132✔
46
        if (isset($parts['query'])) {
132✔
47
            $parameters = RequestParser::parseRequestParams($parts['query']);
101✔
48

49
            // Remove existing page parameter
50
            unset($parameters[$pageParameterName]);
101✔
51
        }
52

53
        return ['parts' => $parts, 'parameters' => $parameters];
132✔
54
    }
55

56
    /**
57
     * Gets a collection IRI for the given parameters.
58
     *
59
     * @param int $urlGenerationStrategy
60
     */
61
    public static function createIri(array $parts, array $parameters, ?string $pageParameterName = null, ?float $page = null, $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH): string
62
    {
63
        if (null !== $page && null !== $pageParameterName) {
113✔
64
            $parameters[$pageParameterName] = $page;
8✔
65
        }
66

67
        $query = http_build_query($parameters, '', '&', \PHP_QUERY_RFC3986);
113✔
68
        $parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query);
113✔
69

70
        $url = '';
113✔
71
        if ((UrlGeneratorInterface::ABS_URL === $urlGenerationStrategy || UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy) && isset($parts['host'])) {
113✔
72
            if (isset($parts['scheme'])) {
×
73
                $scheme = $parts['scheme'];
×
74
            } elseif (isset($parts['port']) && 443 === $parts['port']) {
×
75
                $scheme = 'https';
×
76
            } else {
77
                $scheme = 'http';
×
78
            }
79
            $url .= UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy ? '//' : "$scheme://";
×
80

81
            if (isset($parts['user'])) {
×
82
                $url .= $parts['user'];
×
83

84
                if (isset($parts['pass'])) {
×
85
                    $url .= ':'.$parts['pass'];
×
86
                }
87

88
                $url .= '@';
×
89
            }
90

91
            $url .= $parts['host'];
×
92

93
            if (isset($parts['port'])) {
×
94
                $url .= ':'.$parts['port'];
×
95
            }
96
        }
97

98
        $url .= $parts['path'];
113✔
99

100
        if ('' !== $parts['query']) {
113✔
101
            $url .= '?'.$parts['query'];
103✔
102
        }
103

104
        if (isset($parts['fragment'])) {
113✔
105
            $url .= '#'.$parts['fragment'];
×
106
        }
107

108
        return $url;
113✔
109
    }
110
}
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