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
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public static function parseIri(string $iri, string $pageParameterName): array |
37
|
|
|
{ |
38
|
|
|
$parts = parse_url($iri); |
39
|
|
|
if (false === $parts) { |
40
|
|
|
throw new InvalidArgumentException(sprintf('The request URI "%s" is malformed.', $iri)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$parameters = []; |
44
|
|
|
if (isset($parts['query'])) { |
45
|
|
|
$parameters = RequestParser::parseRequestParams($parts['query']); |
46
|
|
|
|
47
|
|
|
// Remove existing page parameter |
48
|
|
|
unset($parameters[$pageParameterName]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return ['parts' => $parts, 'parameters' => $parameters]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets a collection IRI for the given parameters. |
56
|
|
|
* |
57
|
|
|
* @param float $page |
58
|
|
|
*/ |
59
|
|
|
public static function createIri(array $parts, array $parameters, string $pageParameterName = null, float $page = null, bool $absoluteUrl = false): string |
60
|
|
|
{ |
61
|
|
|
if (null !== $page && null !== $pageParameterName) { |
62
|
|
|
$parameters[$pageParameterName] = $page; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$query = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986); |
66
|
|
|
$parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query); |
67
|
|
|
|
68
|
|
|
$url = ''; |
69
|
|
|
|
70
|
|
|
if ($absoluteUrl && isset($parts['host'])) { |
71
|
|
|
if (isset($parts['scheme'])) { |
72
|
|
|
$url .= $parts['scheme']; |
73
|
|
|
} elseif (isset($parts['port']) && 443 === $parts['port']) { |
74
|
|
|
$url .= 'https'; |
75
|
|
|
} else { |
76
|
|
|
$url .= 'http'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$url .= '://'; |
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']; |
99
|
|
|
|
100
|
|
|
if ('' !== $parts['query']) { |
101
|
|
|
$url .= '?'.$parts['query']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($parts['fragment'])) { |
105
|
|
|
$url .= '#'.$parts['fragment']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $url; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|