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