|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Router; |
|
4
|
|
|
|
|
5
|
|
|
trait WhereTrait |
|
6
|
|
|
{ |
|
7
|
|
|
use Helper; |
|
8
|
|
|
|
|
9
|
|
|
private array $parameters = []; |
|
10
|
|
|
|
|
11
|
|
|
public static function where(array $wheres): Router |
|
12
|
|
|
{ |
|
13
|
|
|
$route = self::getInstance()->inSave(); |
|
14
|
|
|
$route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
15
|
|
|
self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
16
|
|
|
return self::getInstance(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function checkData(array $route, string $uriPath): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->checkCount($route['uri']->getPath(), $uriPath); |
|
22
|
|
|
|
|
23
|
|
|
$this->parameters = []; |
|
24
|
|
|
|
|
25
|
|
|
$uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
26
|
|
|
|
|
27
|
|
|
$routePath = explode('/',urldecode($route['uri']->getPath())); |
|
28
|
|
|
unset($routePath[0]); |
|
29
|
|
|
$uriPath = explode('/',urldecode($uriPath)); |
|
30
|
|
|
unset($uriPath[0]); |
|
31
|
|
|
|
|
32
|
|
|
$corretRoute = true; |
|
33
|
|
|
foreach($routePath as $r => $routeFrag){ |
|
34
|
|
|
$where = is_array($route['where']) ? $route['where'] : []; |
|
35
|
|
|
$routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
|
36
|
|
|
|
|
37
|
|
|
if($routeFrag !== $uriPath[$r]){ |
|
38
|
|
|
$corretRoute = false; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if(!$corretRoute){ |
|
43
|
|
|
throw new \Exception('continue'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function replaceParam(array $where, string $ref, string $value): string |
|
50
|
|
|
{ |
|
51
|
|
|
if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
52
|
|
|
$this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
53
|
|
|
|
|
54
|
|
|
$this->checkValueRequire($ref,$value); |
|
55
|
|
|
|
|
56
|
|
|
if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
57
|
|
|
$this->matchParam($where, $ref, $value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $value; |
|
61
|
|
|
} |
|
62
|
|
|
return $ref; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function checkValueRequire(string $ref, string $value): void |
|
66
|
|
|
{ |
|
67
|
|
|
if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
68
|
|
|
throw new \Exception('continue'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function checkCount(string $routePath, string $uriPath): void |
|
73
|
|
|
{ |
|
74
|
|
|
$countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
75
|
|
|
$countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
76
|
|
|
|
|
77
|
|
|
if($countRequest !== $countRoute){ |
|
78
|
|
|
throw new \Exception('continue'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function matchParam(array $where, string $ref, string $value): void |
|
83
|
|
|
{ |
|
84
|
|
|
echo 3; |
|
85
|
|
|
if(substr($ref,0,2) === '{' || $value !== ''){ |
|
86
|
|
|
echo 1; |
|
87
|
|
|
if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
88
|
|
|
echo 2; |
|
89
|
|
|
throw new \Exception('continue'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|