Issues (4)

src/RouterTrait.php (4 issues)

1
<?php
2
3
namespace Stonks\Router;
4
5
/**
6
 * Trait RouterTrait
7
 *
8
 * @package Stonks\Router
9
 */
10
trait RouterTrait
11
{
12
13
	/**
14
	 * @var array
15
	 */
16
	private $routes;
17
18
	/**
19
	 * @var string
20
	 */
21
	private $patch;
22
23
	/**
24
	 * @var string
25
	 */
26
	private $httpMethod;
27
28
	/**
29
	 * @return string|null
30
	 */
31
	public function currentRoute(): ?string
32
	{
33
		if ($this->route) {
0 ignored issues
show
The property route does not exist on Stonks\Router\RouterTrait. Did you mean routes?
Loading history...
34
			return $this->treat($this->route, ($this->data ?? null));
35
		}
36
37
		return null;
38
	}
39
40
	/**
41
	 * @param array|string $name
42
	 * @return bool
43
	 */
44
	public function isCurrentRoute($name): bool
45
	{
46
		if ($this->route && $this->route['name'] != null) {
0 ignored issues
show
The property route does not exist on Stonks\Router\RouterTrait. Did you mean routes?
Loading history...
47
			return (is_array($name) ? in_array($this->route['name'], $name) : ($this->route['name'] === $name));
48
		}
49
50
		return false;
51
	}
52
53
	/**
54
	 * @param string $name
55
	 * @param array|null $data
56
	 * @return string|null
57
	 */
58
	public function route(string $name, array $data = null): ?string
59
	{
60
		foreach ($this->routes as $httpVerb) {
61
			foreach ($httpVerb as $routeItem) {
62
				if (!empty($routeItem['name']) && $routeItem['name'] === $name && $routeItem['route'] !== '') {
63
					return $this->treat($routeItem, $data);
64
				}
65
			}
66
		}
67
68
		return null;
69
	}
70
71
	/**
72
	 * @param string $route
73
	 * @param array|null $data
74
	 * @return void
75
	 */
76
	public function redirect(string $route, array $data = null): void
77
	{
78
		if ($url = $this->route($route, $data)) {
79
			header("Location: {$url}");
80
			exit;
81
		}
82
83
		if (filter_var($route, FILTER_VALIDATE_URL)) {
84
			header("Location: {$route}");
85
			exit;
86
		}
87
88
		$route = (substr($route, 0, 1) === '/' ? $route : "/{$route}");
89
90
		header("Location: {$this->projectUrl}{$route}");
91
		exit;
92
	}
93
94
	/**
95
	 * @param string $method
96
	 * @param string $route
97
	 * @param $handler
98
	 * @param string|null $name
99
	 * @return void
100
	 */
101
	protected function addRoute(string $method, string $route, $handler, string $name = null): void
102
	{
103
		if ($route === '/') {
104
			$this->addRoute($method, '', $handler, $name);
105
		}
106
107
		$this->formSpoofing();
0 ignored issues
show
It seems like formSpoofing() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
		$this->/** @scrutinizer ignore-call */ 
108
         formSpoofing();
Loading history...
108
109
		preg_match_all('~\{\s* ([a-zA-Z_][a-zA-Z0-9_-]*) \}~x', $route, $keys, PREG_SET_ORDER);
110
111
		$routeDiff = array_values(array_diff(explode('/', $this->patch), explode('/', $route)));
112
		$offset = ($this->group ? 1 : 0);
113
114
		foreach ($keys as $key) {
115
			$this->data[$key[1]] = ($routeDiff[$offset++] ?? null);
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
116
		}
117
118
		$route = (!$this->group ? $route : "/{$this->group}{$route}");
119
		$data = $this->data;
120
		$namespace = $this->namespace;
121
122
		$router = function () use ($method, $handler, $data, $route, $name, $namespace) {
123
			return [
124
				'route' => $route,
125
				'name' => $name,
126
				'method' => $method,
127
				'handler' => $this->handler($handler, $namespace),
128
				'action' => $this->action($handler),
129
				'data' => $data,
130
			];
131
		};
132
133
		$route = preg_replace('~{([^}]*)}~', '([^/]+)', $route);
134
		$this->routes[$method][$route] = $router();
135
	}
136
137
	/**
138
	 * @param $handler
139
	 * @param $namespace
140
	 * @return mixed|string
141
	 */
142
	private function handler($handler, $namespace)
143
	{
144
		return (!is_string($handler) ? $handler : "{$namespace}\\" . explode($this->separator, $handler)[0]);
145
	}
146
147
	/**
148
	 * @param $handler
149
	 * @return string|null
150
	 */
151
	private function action($handler): ?string
152
	{
153
		return (!is_string($handler) ?: (explode($this->separator, $handler)[1] ?? null));
154
	}
155
156
	/**
157
	 * @param array $currentRoute
158
	 * @param array|null $data
159
	 * @return string
160
	 */
161
	private function treat(array $currentRoute, array $data = null): string
162
	{
163
		$route = $currentRoute['route'];
164
165
		if (!empty($data)) {
166
			$arguments = [];
167
			$params = [];
168
169
			foreach ($data as $key => $value) {
170
				if (!strstr($route, "{{$key}}")) {
171
					$params[$key] = $value;
172
				}
173
174
				$arguments["{{$key}}"] = $value;
175
			}
176
177
			$route = $this->process($route, $arguments, $params);
178
		}
179
180
		return "{$this->projectUrl}{$route}";
181
	}
182
183
	/**
184
	 * @param string $route
185
	 * @param array $arguments
186
	 * @param array|null $params
187
	 * @return string
188
	 */
189
	private function process(string $route, array $arguments, array $params = null): string
190
	{
191
		$params = (!empty($params) ? '?' . http_build_query($params) : null);
192
		return str_replace(array_keys($arguments), array_values($arguments), $route) . $params;
193
	}
194
}
195