|
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\Api\UrlGeneratorInterface; |
|
17
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parses and creates IRIs. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @internal |
|
25
|
|
|
*/ |
|
26
|
|
|
final class IriHelper |
|
27
|
|
|
{ |
|
28
|
|
|
private function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parses and standardizes the request IRI. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws InvalidArgumentException |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function parseIri(string $iri, string $pageParameterName): array |
|
38
|
|
|
{ |
|
39
|
|
|
$parts = parse_url($iri); |
|
40
|
|
|
if (false === $parts) { |
|
41
|
|
|
throw new InvalidArgumentException(sprintf('The request URI "%s" is malformed.', $iri)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$parameters = []; |
|
45
|
|
|
if (isset($parts['query'])) { |
|
46
|
|
|
$parameters = RequestParser::parseRequestParams($parts['query']); |
|
47
|
|
|
|
|
48
|
|
|
// Remove existing page parameter |
|
49
|
|
|
unset($parameters[$pageParameterName]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return ['parts' => $parts, 'parameters' => $parameters]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Gets a collection IRI for the given parameters. |
|
57
|
|
|
* |
|
58
|
|
|
* @param float $page |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function createIri(array $parts, array $parameters, string $pageParameterName = null, float $page = null, $urlGenerationStrategy = UrlGeneratorInterface::ABS_PATH): string |
|
61
|
|
|
{ |
|
62
|
|
|
if (null !== $page && null !== $pageParameterName) { |
|
63
|
|
|
$parameters[$pageParameterName] = $page; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (\is_bool($urlGenerationStrategy)) { |
|
67
|
|
|
@trigger_error(sprintf('Passing a bool as 5th parameter to "%s::createIri()" is deprecated since API Platform 2.6. Pass an "%s" constant (int) instead.', __CLASS__, UrlGeneratorInterface::class), E_USER_DEPRECATED); |
|
68
|
|
|
$urlGenerationStrategy = $urlGenerationStrategy ? UrlGeneratorInterface::ABS_URL : UrlGeneratorInterface::ABS_PATH; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$query = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986); |
|
72
|
|
|
$parts['query'] = preg_replace('/%5B\d+%5D/', '%5B%5D', $query); |
|
73
|
|
|
|
|
74
|
|
|
$url = ''; |
|
75
|
|
|
if ((UrlGeneratorInterface::ABS_URL === $urlGenerationStrategy || UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy) && isset($parts['host'])) { |
|
76
|
|
|
if (isset($parts['scheme'])) { |
|
77
|
|
|
$scheme = $parts['scheme']; |
|
78
|
|
|
} elseif (isset($parts['port']) && 443 === $parts['port']) { |
|
79
|
|
|
$scheme = 'https'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$scheme = 'http'; |
|
82
|
|
|
} |
|
83
|
|
|
$url .= UrlGeneratorInterface::NET_PATH === $urlGenerationStrategy ? '//' : "$scheme://"; |
|
84
|
|
|
|
|
85
|
|
|
if (isset($parts['user'])) { |
|
86
|
|
|
$url .= $parts['user']; |
|
87
|
|
|
|
|
88
|
|
|
if (isset($parts['pass'])) { |
|
89
|
|
|
$url .= ':'.$parts['pass']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$url .= '@'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$url .= $parts['host']; |
|
96
|
|
|
|
|
97
|
|
|
if (isset($parts['port'])) { |
|
98
|
|
|
$url .= ':'.$parts['port']; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$url .= $parts['path']; |
|
103
|
|
|
|
|
104
|
|
|
if ('' !== $parts['query']) { |
|
105
|
|
|
$url .= '?'.$parts['query']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (isset($parts['fragment'])) { |
|
109
|
|
|
$url .= '#'.$parts['fragment']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $url; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|