Passed
Pull Request — 2.1 (#1729)
by Antoine
04:12 queued 01:10
created

IriHelper::parseIri()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Util;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
18
/**
19
 * Parses and creates IRIs.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 *
23
 * @internal
24
 */
25
final class IriHelper
26
{
27
    private function __construct()
28
    {
29
    }
30
31
    /**
32
     * Parses and standardizes the request IRI.
33
     *
34
     * @param string $iri
35
     * @param string $pageParameterName
36
     *
37
     * @throws InvalidArgumentException
38
     *
39
     * @return array
40
     */
41
    public static function parseIri(string $iri, string $pageParameterName): array
42
    {
43
        $parts = parse_url($iri);
44
        if (false === $parts) {
0 ignored issues
show
introduced by
The condition false === $parts can never be true.
Loading history...
45
            throw new InvalidArgumentException(sprintf('The request URI "%s" is malformed.', $iri));
46
        }
47
48
        $parameters = [];
49
        if (isset($parts['query'])) {
50
            $parameters = RequestParser::parseRequestParams($parts['query']);
51
52
            // Remove existing page parameter
53
            unset($parameters[$pageParameterName]);
54
        }
55
56
        return ['parts' => $parts, 'parameters' => $parameters];
57
    }
58
59
    /**
60
     * Gets a collection IRI for the given parameters.
61
     *
62
     * @param array  $parts
63
     * @param array  $parameters
64
     * @param string $pageParameterName
65
     * @param float  $page
66
     * @param bool   $absoluteUrl
67
     *
68
     * @return string
69
     */
70
    public static function createIri(array $parts, array $parameters, string $pageParameterName, float $page = null, bool $absoluteUrl = false): string
71
    {
72
        if (null !== $page) {
73
            $parameters[$pageParameterName] = $page;
74
        }
75
76
        $query = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
77
        $parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query);
78
79
        $url = '';
80
81
        if ($absoluteUrl && isset($parts['host'])) {
82
            if (isset($parts['scheme'])) {
83
                $url .= $parts['scheme'];
84
            } elseif (isset($parts['port']) && 443 === $parts['port']) {
85
                $url .= 'https';
86
            } else {
87
                $url .= 'http';
88
            }
89
90
            $url .= '://';
91
92
            if (isset($parts['user'])) {
93
                $url .= $parts['user'];
94
95
                if (isset($parts['pass'])) {
96
                    $url .= ':'.$parts['pass'];
97
                }
98
99
                $url .= '@';
100
            }
101
102
            $url .= $parts['host'];
103
104
            if (isset($parts['port'])) {
105
                $url .= ':'.$parts['port'];
106
            }
107
        }
108
109
        $url .= $parts['path'];
110
111
        if ('' !== $parts['query']) {
112
            $url .= '?'.$parts['query'];
113
        }
114
115
        if (isset($parts['fragment'])) {
116
            $url .= '#'.$parts['fragment'];
117
        }
118
119
        return $url;
120
    }
121
}
122