-
Notifications
You must be signed in to change notification settings - Fork 0
/
acsmti-route.class.php
165 lines (126 loc) · 5.39 KB
/
acsmti-route.class.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
if( !class_exists( 'ascmtiRoute' ) ){
class acsmtiRoute{
function gerarRegex( $rota ){
$rota = str_replace( ["{{","}}"], ["©", "®"], $rota );
$regex_parametros = "/©(?'chamada'((((((?'parametro'([a-z0-9\_,]+))\:)?(?'valor'([^©®]+))))|(?R))*))®/";
$regex_final = '';
$regex_final = preg_replace_callback( $regex_parametros, array( $this, 'trataParametros' ), $rota );
while( preg_match( "/\[\/(.*)\/\]/", $regex_final, $match ) ){
$novo = preg_replace( ["/^\[\//","/\/\]$/"], ["(\/",")?"], $match[0] );
$regex_final = str_replace( $match[0], $novo, $regex_final );
}
$regex_final = str_replace( "__ENCLOUSURADO__", "[^\/]", $regex_final );
$regex_final = preg_replace( "/^\//" , "\/" , $regex_final );
$regex_final = preg_replace( "/([^\\\])\//" , "$1\/" , $regex_final );
$regex_final = '/^' . $regex_final . '(\/)?$/';
return $regex_final;
}
function pegarParametrosDaRota( $rota, $padrao ){
preg_match( $padrao, $rota, $resultado );
foreach( $resultado as $k => $v ){
if( is_numeric( $k ) ){
unset( $resultado[ $k ] );
}
else {
if( preg_match( "/___/", $k ) ){
$parametro = explode( "___", $k );
unset( $resultado[ $k ] );
$resultado[ $parametro[0] ] = $this->setParam( $parametro[1], $v );
}
}
}
return (object)$resultado;
}
private function trataParametros( $match ){
$novo = $match[0];
$novo = str_replace( ["©","®"], ["(",")"], $novo );
if( isset( $match['parametro'] ) && !empty( $match['parametro'] ) ){
$novo = str_replace( $match['chamada'], "(?'" . str_replace( ",", "___", $match['parametro'] ) . "'(" . $match['valor'] . "))", $novo );
} else {
$novo = str_replace( $match['chamada'], "(?'" . str_replace( ",", "___", $match['valor'] ) . "'__ENCLOUSURADO__+)", $novo );
}
return $novo;
}
private function setParam( $param, $val ){
if( preg_match( "/(int|float|mobject|object|array|boolean)/", $param, $match ) ){
$method = 'set' . ucfirst( $match[0] );
$new_param = preg_replace( "/^" . $match[0] . "/", "", $param );
if( is_array( $val ) ){
foreach( $val as $k => $v ){
$val[ $k ] = $this->setParam(
$new_param,
$this->$method( $v )
);
}
return $val;
}
else if( is_object( $val ) ){
foreach( $val as $k => $v ){
$val->{$k} = $this->setParam(
$new_param,
$this->$method( $v )
);
}
return $val;
}
else {
return $this->setParam(
$new_param,
$this->$method( $val )
);
}
}
return $val;
}
private function setInt( $val ){
return intval( $val );
}
private function setFloat( $val ){
return floatval( $val );
}
private function setObject( $val ){
$array = explode( "|", $val );
$object = [];
foreach( $array as $a ){
$key = preg_replace( "/\[(.*)\]/", "", $a );
$value = preg_replace( ["/^[^\[]+\[/", "/\]/"], "", $a );
$object[ $key ] = $value;
}
return (object)$object;
}
private function setMobject( $val ){
$array = explode( "|", $val );
$object = [];
foreach( $array as $a ){
$key = preg_replace( "/\[(.*)\]/", "", $a );
$value = preg_replace( ["/^[^\[]+\[/", "/\]/"], "", $a );
$object[ $key ][] = $value;
}
return (object)$object;
}
private function setArray( $val ){
if( preg_match( "/(\[|\]|\|)/", $val ) ){
$itens = [];
foreach( explode( "|", $val ) as $item ){
$key = preg_replace( "/\[(.*)\]/", "", $item );
$value = preg_replace( ["/^[^\[]+\[/", "/\]/"], "", $item );
$itens[ $key ] = explode( ',', $value );
}
return $itens;
}
else {
return explode( ",", $val );
}
}
private function setBoolean( $val ){
return boolval( $val );
}
}
}
$teste = new acsmtiRoute();
$regex = $teste->gerarRegex( '/pasta1/{{pesquisa,array}}' );
echo "<pre>";
var_dump( $teste->pegarParametrosDaRota( '/pasta1/ids[10,20,30]|variaveis[aaa,bbb]|blablabla[1,2,3]', $regex ) );
echo "</pre>";
exit;