|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\MissingUrlsRedirector\Entities; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Routing\Route; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Redirection |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanedev\MissingUrlsRedirector\Entities |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Redirection |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Properties |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $from; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $to; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
protected $status; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \Illuminate\Routing\Route|null */ |
|
33
|
|
|
protected $route; |
|
34
|
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
|
36
|
|
|
| Constructor |
|
37
|
|
|
| ----------------------------------------------------------------- |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Redirection constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $from |
|
44
|
|
|
* @param string $to |
|
45
|
|
|
* @param int $status |
|
46
|
|
|
*/ |
|
47
|
84 |
|
public function __construct(string $from, string $to, int $status) |
|
48
|
|
|
{ |
|
49
|
84 |
|
$this->setFrom($from); |
|
50
|
84 |
|
$this->setTo($to); |
|
51
|
84 |
|
$this->setStatus($status); |
|
52
|
84 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Make a new redirection. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $from |
|
58
|
|
|
* @param string $to |
|
59
|
|
|
* @param int $status |
|
60
|
|
|
* |
|
61
|
|
|
* @return static |
|
62
|
|
|
*/ |
|
63
|
12 |
|
public static function make(string $from, string $to, int $status = Response::HTTP_MOVED_PERMANENTLY) |
|
64
|
|
|
{ |
|
65
|
12 |
|
return new static($from, $to, $status); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* ----------------------------------------------------------------- |
|
69
|
|
|
| Getters & Setters |
|
70
|
|
|
| ----------------------------------------------------------------- |
|
71
|
|
|
*/ |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the old URL. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
84 |
|
public function getFrom(): string |
|
79
|
|
|
{ |
|
80
|
84 |
|
return $this->from; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the old URL. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $from |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
84 |
|
public function setFrom(string $from): self |
|
91
|
|
|
{ |
|
92
|
84 |
|
$this->from = $from; |
|
93
|
|
|
|
|
94
|
84 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the new URL. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
84 |
|
public function getTo(): string |
|
103
|
|
|
{ |
|
104
|
84 |
|
return $this->to; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set the new URL. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $to |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
84 |
|
public function setTo(string $to): self |
|
115
|
|
|
{ |
|
116
|
84 |
|
$this->to = $to; |
|
117
|
|
|
|
|
118
|
84 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get the status code. |
|
123
|
|
|
* |
|
124
|
|
|
* @return int |
|
125
|
|
|
*/ |
|
126
|
84 |
|
public function getStatus(): int |
|
127
|
|
|
{ |
|
128
|
84 |
|
return $this->status; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Set the status. |
|
133
|
|
|
* |
|
134
|
|
|
* @param int $status |
|
135
|
|
|
* |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
84 |
|
public function setStatus(int $status): self |
|
139
|
|
|
{ |
|
140
|
84 |
|
$this->status = $status; |
|
141
|
|
|
|
|
142
|
84 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get the route. |
|
147
|
|
|
* |
|
148
|
|
|
* @return \Illuminate\Routing\Route|null |
|
149
|
|
|
*/ |
|
150
|
60 |
|
public function getRoute(): ?Route |
|
151
|
|
|
{ |
|
152
|
60 |
|
return $this->route; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set the route. |
|
157
|
|
|
* |
|
158
|
|
|
* @param \Illuminate\Routing\Route $route |
|
159
|
|
|
* |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
60 |
|
public function setRoute(Route $route) |
|
163
|
|
|
{ |
|
164
|
60 |
|
$this->route = $route; |
|
165
|
|
|
|
|
166
|
60 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the resolved url. |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
60 |
|
public function getResolvedUrl(): string |
|
175
|
|
|
{ |
|
176
|
60 |
|
$url = $this->getTo(); |
|
177
|
|
|
|
|
178
|
60 |
|
foreach ($this->getRoute()->parameters() as $key => $value) { |
|
179
|
18 |
|
$url = str_replace("{{$key}}", $value, $url); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
60 |
|
return preg_replace('/\/{[\w-]+}/', '', $url); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|