-
Notifications
You must be signed in to change notification settings - Fork 1
/
RouteGenerator.php
121 lines (106 loc) · 3.36 KB
/
RouteGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Created by PhpStorm.
* User: Babacar Mbengue
* Date: 01/04/2019
* Time: 18:21
*/
namespace Babacar\Router;
use Babacar\Router\Exceptions\RouteNotFoundException;
use Babacar\Router\Exceptions\RouteParameterNameException;
use Babacar\Router\Exceptions\RouteParameterNotMatchException;
use Babacar\Router\Exceptions\RouteParametersMissingException;
class RouteGenerator
{
/**
* @param $name
* @param array $prams
* @param array $query_args
* @return array|string
* @throws RouteNotFoundException
* @throws RouteParametersMissingException
* @throws RouteParameterNotMatchException
* @throws RouteParameterNameException
*/
public static function generate($name, array $prams = [], array $query_args = [])
{
$route = RouteCollector::getByName($name);
if(is_null($route)){
throw new RouteNotFoundException($name);
}
/**
* @var $expectedParameters Parameter[]
*/
$expectedParameters = RouteMatcher::getExpectedParameters($route->getPath());
$actualParameters = RouteMatcher::getActualParameters($prams);
$expected = [];
$uri = [];
$posUri = 0;
$postExpected = 0;
foreach ($expectedParameters as $parameter){
if($parameter instanceof RouteRegexParameter || $parameter instanceof RouteParameter){
$expected[$postExpected++]=$parameter;
$posUri++;
}
else if($parameter instanceof RoutePrefixParameter){
$uri[$posUri]=$parameter->getValue();
$posUri++;
}
}
if(empty($expectedParameters)){
return $route->getPath();
}
$actualParameters = self::reorganise($expected,$actualParameters);
for ($i = 0;$i < $postExpected ;$i++)
{
$ex = $expected[$i];
$ac = $actualParameters[$i] ?? null;
if($ac === null && !$ex->isOptional())
throw new RouteParametersMissingException($ex->getName());
else if($ex instanceof RouteRegexParameter && $ac !== null && !$ex->match($ac->getValue()))
{
throw new RouteParameterNotMatchException($ex->getName(),$ex->getPattern(),$ac->getValue());
}
else
{
if($ac !== null )
{
if($ex->getName() !== $ac->getKey())
{
throw new RouteParametersMissingException($ex->getName());
}
$j = 0;
while (isset($uri[$j]))
$j++;
$uri[$j]=$ac->getValue();
}
}
}
ksort($uri);
$uri = join("/",$uri);
if(!empty($query_args)){
$uri.="?".http_build_query($query_args);
}
return "/".$uri;
}
/**
* @param Parameter[] $expecteds
* @param ActualParameter[] $actualParameters
*/
private static function reorganise($expecteds, $actualParameters)
{
$p = [];
foreach ($expecteds as $expected)
{
foreach ($actualParameters as $parameter)
{
if($expected->getName() === $parameter->getKey())
{
$p[]=$parameter;
break;
}
}
}
return $p;
}
}