|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Routing\Route; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Routing\Router; |
|
28
|
|
|
use Brickoo\Component\Routing\Route\Exception\PathNotValidException; |
|
29
|
|
|
use Brickoo\Component\Routing\Route\Exception\RouteRequiredParametersMissingException; |
|
30
|
|
|
use Brickoo\Component\Common\Assert; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* RouteUriBuilder |
|
34
|
|
|
* |
|
35
|
|
|
* Implements an uri builder to create a route matching uri. |
|
36
|
|
|
* @author Celestino Diaz <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class RouteUriBuilder { |
|
39
|
|
|
|
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $baseUrl; |
|
42
|
|
|
|
|
43
|
|
|
/** @var \Brickoo\Component\Routing\Router */ |
|
44
|
|
|
private $router; |
|
45
|
|
|
|
|
46
|
|
|
/** @var \Brickoo\Component\Routing\Route\RoutePathRegexGenerator */ |
|
47
|
|
|
private $regexGenerator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Class constructor. |
|
51
|
|
|
* @param string $baseUrl the base url e.g. http://localhost:8080 |
|
52
|
|
|
* @param \Brickoo\Component\Routing\Router $router |
|
53
|
|
|
* @param \Brickoo\Component\Routing\Route\RoutePathRegexGenerator $regexGenerator |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function __construct($baseUrl, Router $router, RoutePathRegexGenerator $regexGenerator) { |
|
56
|
2 |
|
Assert::isString($baseUrl); |
|
57
|
1 |
|
$this->baseUrl = $baseUrl; |
|
58
|
1 |
|
$this->router = $router; |
|
59
|
1 |
|
$this->regexGenerator = $regexGenerator; |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Builds an uri string based on the parameters provided. |
|
64
|
|
|
* @param string $routeName the route to use for the build |
|
65
|
|
|
* @param array $pathParameters the path parameters as key/value pairs |
|
66
|
|
|
* @param string $queryString |
|
67
|
|
|
* @throws \Brickoo\Component\Routing\Route\Exception\PathNotValidException |
|
68
|
|
|
* @internal param string $queryParameters the query parameters |
|
69
|
|
|
* @return string the built uri |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function build($routeName, array $pathParameters = [], $queryString = "") { |
|
72
|
4 |
|
Assert::isString($routeName); |
|
73
|
4 |
|
Assert::isString($queryString); |
|
74
|
|
|
|
|
75
|
4 |
|
$route = $this->router->getRoute($routeName); |
|
76
|
3 |
|
$expectedPath = $this->getExpectedRoutePath($route, $pathParameters); |
|
77
|
|
|
|
|
78
|
2 |
|
$matches = []; |
|
79
|
2 |
|
if (preg_match_all($this->regexGenerator->generate($route), $expectedPath, $matches) === 0) { |
|
80
|
1 |
|
throw new PathNotValidException($routeName, $expectedPath); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $this->createUriString($expectedPath, $queryString); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the expected uri path to validate against the route path. |
|
88
|
|
|
* @param \Brickoo\Component\Routing\Route\Route $route |
|
89
|
|
|
* @param array $pathParameters the path parameters to use |
|
90
|
|
|
* @throws \Brickoo\Component\Routing\Route\Exception\RouteRequiredParametersMissingException |
|
91
|
|
|
* @return string the uri path expected |
|
92
|
|
|
*/ |
|
93
|
3 |
|
private function getExpectedRoutePath(Route $route, $pathParameters) { |
|
94
|
3 |
|
$routePath = $route->getPath(); |
|
95
|
3 |
|
$pathParameters = array_merge($route->getDefaultValues(), $pathParameters); |
|
96
|
|
|
|
|
97
|
3 |
|
foreach ($pathParameters as $parameter => $value) { |
|
98
|
3 |
|
$routePath = str_replace("{".$parameter."}", $value, $routePath); |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
$matches = []; |
|
102
|
3 |
|
if (preg_match_all("~(\\{(?<missingParameters>[\\w]+)\\})~", $routePath, $matches) > 0) { |
|
103
|
1 |
|
throw new RouteRequiredParametersMissingException($route->getName(), $matches["missingParameters"]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return $routePath; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the created uri string. |
|
111
|
|
|
* @param string $uriPath the uri path |
|
112
|
|
|
* @param string $queryString the query string |
|
113
|
|
|
* @return string the created uri string |
|
114
|
|
|
*/ |
|
115
|
1 |
|
private function createUriString($uriPath, $queryString) { |
|
116
|
1 |
|
return rtrim($this->baseUrl, "/").$uriPath.(empty($queryString) ? "" : "?".ltrim($queryString, "?")); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|