1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChinLeung\LaravelMultilingualRoutes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\RouteCollection; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
class MultilingualRoutePendingRegistration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The handle of the routes. |
12
|
|
|
* |
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
protected $handle; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The translation key of the routes. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The list of locales for the route. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $locales = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The options of the routes. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $options = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The resource's registration status. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $registered = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The resource registrar. |
47
|
|
|
* |
48
|
|
|
* @var \Illuminate\Routing\ResourceRegistrar |
49
|
|
|
*/ |
50
|
|
|
protected $registrar; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor of the class. |
54
|
|
|
* |
55
|
|
|
* @param \ChinLeung\LaravelMultilingualRoutes\MultilingualRegistrar $registrar |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $handle |
58
|
|
|
* @param array $locales |
59
|
|
|
* @return void |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
public function __construct(MultilingualRegistrar $registrar, string $key, $handle, array $locales = []) |
62
|
|
|
{ |
63
|
|
|
$this->key = $key; |
64
|
|
|
$this->registrar = $registrar; |
|
|
|
|
65
|
|
|
$this->handle = $handle; |
66
|
|
|
$this->locales = $locales; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register the resource route. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Routing\RouteCollection |
73
|
|
|
*/ |
74
|
|
|
public function register() : RouteCollection |
75
|
|
|
{ |
76
|
|
|
$this->registered = true; |
77
|
|
|
|
78
|
|
|
return $this->registrar->register( |
79
|
|
|
$this->key, |
80
|
|
|
$this->handle, |
81
|
|
|
$this->options['locales'] ?? $this->locales, |
82
|
|
|
$this->options |
|
|
|
|
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add one or many locale to the exception. |
88
|
|
|
* |
89
|
|
|
* @param string|array $locales |
90
|
|
|
* @return self |
91
|
|
|
*/ |
92
|
|
|
public function except($locales) : self |
93
|
|
|
{ |
94
|
|
|
$this->options['locales'] = array_diff( |
95
|
|
|
$this->locales, |
96
|
|
|
Arr::wrap($locales) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the name of the routes. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
|
|
public function name(string $name) : self |
109
|
|
|
{ |
110
|
|
|
$this->options['name'] = $name; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the method of the routes. |
117
|
|
|
* |
118
|
|
|
* @param string $method |
119
|
|
|
* @return self |
120
|
|
|
*/ |
121
|
|
|
public function method(string $method) : self |
122
|
|
|
{ |
123
|
|
|
$this->options['method'] = $method; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the name of each locale for the routes. |
130
|
|
|
* |
131
|
|
|
* @param array $names |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
public function names(array $names) : self |
135
|
|
|
{ |
136
|
|
|
$this->options['names'] = $names; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the route for a list of locales only. |
143
|
|
|
* |
144
|
|
|
* @param string|array $locales |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
|
|
public function only($locales) : self |
148
|
|
|
{ |
149
|
|
|
$this->options['locales'] = array_intersect( |
150
|
|
|
$this->locales, |
151
|
|
|
Arr::wrap($locales) |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Handle the object's destruction. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function __destruct() |
163
|
|
|
{ |
164
|
|
|
if (! $this->registered) { |
165
|
|
|
$this->register(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.