1: <?php
2:
3: namespace InfoContact\IcFwk\Router
4: ;
5:
6: class Route {
7:
8:
9: private $name;
10:
11: private $url;
12:
13: private $module;
14:
15: private $action;
16:
17: private $varsNames;
18:
19: private $vars = [];
20:
21: 22: 23: 24: 25: 26: 27: 28:
29: public function __construct($name, $url, $module, $action, $varsNames) {
30: $this->setName($name);
31: $this->setUrl($url);
32: $this->setModule($module);
33: $this->setAction($action);
34: $this->setVarsNames($varsNames);
35: }
36:
37: 38: 39:
40: public function getName() {
41: return $this->name;
42: }
43:
44: 45: 46:
47: public function getUrl() {
48: return $this->url;
49: }
50:
51: 52: 53:
54: public function getModule() {
55: return $this->module;
56: }
57:
58: 59: 60:
61: public function getAction() {
62: return $this->action;
63: }
64:
65: 66: 67:
68: public function getVarsNames() {
69: return $this->varsNames;
70: }
71:
72: 73: 74:
75: public function getVars() {
76: return $this->vars;
77: }
78:
79: 80: 81: 82:
83: private function setName($name) {
84: if (is_string($name)) {
85: $this->name = $name;
86: } else {
87: throw new \InvalidArgumentException("Le nom de route doit être une chaîne de caractères");
88: }
89: }
90:
91: 92: 93: 94:
95: private function setUrl($url) {
96: if (is_string($url)) {
97: $this->url = $url;
98: } else {
99: throw new \InvalidArgumentException("L'url de la route doit être une chaîne de caractères");
100: }
101: }
102:
103: 104: 105: 106:
107: private function setModule($module) {
108: if (is_string($module)) {
109: $this->module = $module;
110: } else {
111: throw new \InvalidArgumentException("Le module de la route doit être une chaîne de caractères");
112: }
113: }
114:
115: 116: 117: 118:
119: private function setAction($action) {
120: if (is_string($action)) {
121: $this->action = $action;
122: } else {
123: throw new \InvalidArgumentException("Le'action de la route doit être une chaîne de caractères");
124: }
125: }
126:
127: 128: 129: 130:
131: private function setVarsNames(array $varsNames) {
132: if(is_array($varsNames)) {
133: $this->varsNames = $varsNames;
134: } else {
135: throw new \InvalidArgumentException("Les noms de variable de la route doivent être passés sous forme de tableau");
136: }
137: }
138:
139: 140: 141: 142: 143:
144: public function setVars(array $vars) {
145: if(is_array($vars) && empty($this->vars)) {
146: if($this->hasVars()) {
147: $this->assocVars($vars);
148: }
149: } else {
150: throw new \RuntimeException("Conflit sur les variables de route");
151: }
152: }
153:
154: 155: 156: 157:
158: private function assocVars($vars) {
159: foreach ($vars as $key => $match) {
160:
161: if ($key !== 0) {
162: $this->vars[$this->varsNames[$key - 1]] = $match;
163: }
164: }
165: }
166:
167: 168: 169:
170: public function hasVars() {
171: return !empty($this->varsNames);
172: }
173:
174: 175: 176: 177: 178:
179: public function match($url) {
180: $matches = [];
181: if (preg_match('`^' . $this->url . '$`', $url, $matches)) {
182: return $matches;
183: } else {
184: return false;
185: }
186: }
187:
188: 189: 190: 191: 192:
193: public function generateUrl($params = []) {
194: $url = $this->url;
195: if($this->hasVars()) {
196: $matches = [];
197: \preg_match_all('/\((.*?)?\)/', $url, $matches);
198: foreach ($this->varsNames as $k => $v) {
199: str_replace_first($matches[0][$k], $params[$v], $url);
200: }
201: }
202: $finalurl = \str_replace("\\", "", $url);
203: return $finalurl;
204: }
205:
206: }
207: