Completed
Push — master ( 941f7e...fd5c1f )
by Chin
01:17 queued 11s
created

MultilingualRoutePendingRegistration::middleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ChinLeung\MultilingualRoutes;
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 \ChinLeung\MultilingualRoutes\MultilingualRegistrar
49
     */
50
    protected $registrar;
51
52
    /**
53
     * Constructor of the class.
54
     *
55
     * @param  \ChinLeung\MultilingualRoutes\MultilingualRegistrar  $registrar
56
     * @param  string  $key
57
     * @param  mixed  $handle
58
     * @param  array  $locales
59
     */
60
    public function __construct(MultilingualRegistrar $registrar, string $key, $handle, array $locales = [])
61
    {
62
        $this->key = $key;
63
        $this->registrar = $registrar;
64
        $this->handle = $handle;
65
        $this->locales = $locales;
66
    }
67
68
    /**
69
     * Register the resource route.
70
     *
71
     * @return \Illuminate\Routing\RouteCollection
72
     */
73
    public function register(): RouteCollection
74
    {
75
        $this->registered = true;
76
77
        return $this->registrar->register(
78
            $this->key,
79
            $this->handle,
80
            $this->options['locales'] ?? $this->locales,
81
            $this->options
82
        );
83
    }
84
85
    /**
86
     * Set the default view data of the route.
87
     *
88
     * @param  array  $data
89
     * @return self
90
     */
91
    public function data(array $data): self
92
    {
93
        $this->options['data'] = $data;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Add one or many locale to the exception.
100
     *
101
     * @param  string|array  $locales
102
     * @return self
103
     */
104
    public function except($locales): self
105
    {
106
        $this->options['locales'] = array_diff(
107
            $this->locales,
108
            Arr::wrap($locales)
109
        );
110
111
        return $this;
112
    }
113
114
    /**
115
     * Set the name of the routes.
116
     *
117
     * @param  string  $name
118
     * @return self
119
     */
120
    public function name(string $name): self
121
    {
122
        $this->options['name'] = $name;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the method of the routes.
129
     *
130
     * @param  string  $method
131
     * @return self
132
     */
133
    public function method(string $method): self
134
    {
135
        $this->options['method'] = $method;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Set the middleware of the route.
142
     *
143
     * @param  string|array  $middleware
144
     * @return self
145
     */
146
    public function middleware($middleware): self
147
    {
148
        $this->options['middleware'] = $middleware;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set the name of each locale for the routes.
155
     *
156
     * @param  array  $names
157
     * @return self
158
     */
159
    public function names(array $names): self
160
    {
161
        $this->options['names'] = $names;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set the route for a list of locales only.
168
     *
169
     * @param  string|array  $locales
170
     * @return self
171
     */
172
    public function only($locales): self
173
    {
174
        $this->options['locales'] = array_intersect(
175
            $this->locales,
176
            Arr::wrap($locales)
177
        );
178
179
        return $this;
180
    }
181
182
    /**
183
     * Set a regular expression requirement on the route.
184
     *
185
     * @param  array|string  $name
186
     * @param  string|null  $expression
187
     * @return $this
188
     */
189
    public function where($name, $expression = null): self
190
    {
191
        if (! is_array(Arr::get($this->options, 'constraints'))) {
192
            Arr::set($this->options, 'constraints', []);
193
        }
194
195
        Arr::set($this->options, "constraints.$name", $expression);
196
197
        return $this;
198
    }
199
200
    /**
201
     * Set the view to render.
202
     *
203
     * @param  string  $view
204
     * @param  array  $data
205
     * @return self
206
     */
207
    public function view(string $view, array $data = []): self
208
    {
209
        $this->options['view'] = $view;
210
211
        if (filled($data)) {
212
            $this->options['data'] = $data;
213
        }
214
215
        return $this;
216
    }
217
218
    /**
219
     * Set default parameters values of the routes.
220
     *
221
     * @param  array  $defaults
222
     * @return self
223
     */
224
    public function defaults(array $defaults): self
225
    {
226
        $this->options['defaults'] = $defaults;
227
228
        return $this;
229
    }
230
231
    /**
232
     * Handle the object's destruction.
233
     *
234
     * @return void
235
     */
236
    public function __destruct()
237
    {
238
        if (! $this->registered) {
239
            $this->register();
240
        }
241
    }
242
}
243