|
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
|
|
|
namespace ApiPlatform\Core\Routing; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
15
|
|
|
use ApiPlatform\Core\Util\RequestParser; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* URL generator for collections. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class CollectionRoutingHelper |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Parses and standardizes the request URI. |
|
26
|
|
|
* |
|
27
|
|
|
* @throws InvalidArgumentException |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function parseRequestUri(string $requestUri, string $pageParameterName) : array |
|
30
|
|
|
{ |
|
31
|
|
|
$parts = parse_url($requestUri); |
|
32
|
|
|
if (false === $parts) { |
|
33
|
|
|
throw new InvalidArgumentException(sprintf('The request URI "%s" is malformed.', $requestUri)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$parameters = []; |
|
37
|
|
|
if (isset($parts['query'])) { |
|
38
|
|
|
$parameters = RequestParser::parseRequestParams($parts['query']); |
|
39
|
|
|
|
|
40
|
|
|
// Remove existing page parameter |
|
41
|
|
|
unset($parameters[$pageParameterName]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return [$parts, $parameters]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gets a collection IRI for the given parameters. |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function generateUrl(array $parts, array $parameters, string $pageParameterName, float $page = null) : string |
|
51
|
|
|
{ |
|
52
|
|
|
if (null !== $page) { |
|
53
|
|
|
$parameters[$pageParameterName] = $page; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$query = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986); |
|
57
|
|
|
$parts['query'] = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $query); |
|
58
|
|
|
|
|
59
|
|
|
$url = $parts['path']; |
|
60
|
|
|
|
|
61
|
|
|
if ('' !== $parts['query']) { |
|
62
|
|
|
$url .= '?'.$parts['query']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $url; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|