Completed
Pull Request — 3.x (#140)
by jake
02:04
created

Route::namePrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Router;
10
11
/**
12
 *
13
 * An individual route with a name, path, attributes, defaults, etc.
14
 *
15
 * @package Aura.Router
16
 *
17
 * @property-read string $name The route name.
18
 *
19
 * @property-read string $path The route path.
20
 *
21
 * @property-read string $namePrefix
22
 *
23
 * @property-read string $pathPrefix
24
 *
25
 * @property-read string $host
26
 *
27
 * @property-read array $defaults Default values for attributes.
28
 *
29
 * @property-read array $attributes Attribute values added by the rules.
30
 *
31
 * @property-read array $tokens Placeholder token names and regexes.
32
 *
33
 * @property-read string $wildcard The name of the wildcard token.
34
 *
35
 * @property-read array $accept
36
 *
37
 * @property-read mixed $auth The auth value.
38
 *
39
 * @property-read array $extras
40
 *
41
 * @property-read bool $secure
42
 *
43
 * @property-read array $allows
44
 *
45
 * @property-read bool $routable
46
 *
47
 * @property-read string $failedRule
48
 *
49
 * @property-read mixed $handler
50
 *
51
 */
52
class Route
53
{
54
    /**
55
     *
56
     * Accepts these content types.
57
     *
58
     * @var array
59
     *
60
     */
61
    protected $accepts = [];
62
63
    /**
64
     *
65
     * Allows these HTTP methods.
66
     *
67
     * @var array
68
     *
69
     */
70
    protected $allows = [];
71
72
    /**
73
     *
74
     * Attribute values added by the rules.
75
     *
76
     * @var array
77
     *
78
     */
79
    protected $attributes = [];
80
81
    /**
82
     *
83
     * Authentication/authorization values.
84
     *
85
     * @var mixed
86
     *
87
     */
88
    protected $auth;
89
90
    /**
91
     *
92
     * Default attribute values.
93
     *
94
     * @var array
95
     *
96
     */
97
    protected $defaults = [];
98
99
    /**
100
     *
101
     * Extra key-value pairs to attach to the route; intended for use by
102
     * custom matching rules.
103
     *
104
     * @var array
105
     *
106
     */
107
    protected $extras = [];
108
109
    /**
110
     *
111
     * The rule that failed, if any, during matching.
112
     *
113
     * @var string
114
     *
115
     */
116
    protected $failedRule;
117
118
    /**
119
     *
120
     * The action, controller, callable, closure, etc. this route points to.
121
     *
122
     * @var mixed
123
     *
124
     */
125
    protected $handler;
126
127
    /**
128
     *
129
     * The host string this route responds to.
130
     *
131
     * @var string
132
     *
133
     */
134
    protected $host;
135
136
    /**
137
     *
138
     * The name for this route.
139
     *
140
     * @var string
141
     *
142
     */
143
    protected $name;
144
145
    /**
146
     *
147
     * Prefix the route name with this string.
148
     *
149
     * @var string
150
     *
151
     */
152
    protected $namePrefix;
153
154
    /**
155
     *
156
     * The path for this route.
157
     *
158
     * @var string
159
     *
160
     */
161
    protected $path;
162
163
    /**
164
     *
165
     * Prefix the route path with this string.
166
     *
167
     * @var string
168
     *
169
     */
170
    protected $pathPrefix;
171
172
    /**
173
     *
174
     * Should this route be used for matching?
175
     *
176
     * @var bool
177
     *
178
     */
179
    protected $isRoutable = true;
180
181
    /**
182
     *
183
     * Should this route respond on a secure protocol?
184
     *
185
     * @var bool
186
     *
187
     */
188
    protected $secure = null;
189
190
    /**
191
     *
192
     * Placeholder token names and regexes.
193
     *
194
     * @var array
195
     *
196
     */
197
    protected $tokens = [];
198
199
    /**
200
     *
201
     * Wildcard token name, if any.
202
     *
203
     * @var string
204
     *
205
     */
206
    protected $wildcard = null;
207
208
    /**
209
     *
210
     * When cloning the Route, reset the `$attributes` to an empty array, and
211
     * clear the `$failedRule`.
212
     *
213
     */
214 32
    public function __clone()
215
    {
216
        // $this is the cloned instance, not the original
217 32
        $this->attributes = $this->defaults;
218 32
        $this->failedRule = null;
219 32
    }
220
221
    /**
222
     *
223
     * Magic read-only for all properties.
224
     *
225
     * @param string $key The property to read from.
226
     *
227
     * @return mixed
228
     *
229
     */
230 41
    public function __get($key)
231
    {
232 41
        return $this->$key;
233
    }
234
235
    /**
236
     *
237
     * Merges with the existing content types.
238
     *
239
     * @param string|array $accepts The content types.
240
     *
241
     * @return $this
242
     *
243
     */
244 1
    public function accepts($accepts)
245
    {
246 1
        $this->accepts = array_merge($this->accepts, (array) $accepts);
247 1
        return $this;
248
    }
249
250
    /**
251
     *
252
     * Merges with the existing allowed methods.
253
     *
254
     * @param string|array $allows The allowed HTTP methods.
255
     *
256
     * @return $this
257
     *
258
     */
259 5
    public function allows($allows)
260
    {
261 5
        $this->allows = array_merge($this->allows, (array) $allows);
262 5
        return $this;
263
    }
264
265
    /**
266
     *
267
     * Merges with the existing attributes.
268
     *
269
     * @param array $attributes The attributes to add.
270
     *
271
     * @return $this
272
     *
273
     */
274 17
    public function attributes(array $attributes)
275
    {
276 17
        $this->attributes = array_merge($this->attributes, $attributes);
277 17
        return $this;
278
    }
279
280
    /**
281
     *
282
     * Sets the auth value.
283
     *
284
     * @param mixed $auth The auth value to set.
285
     *
286
     * @return $this
287
     *
288
     */
289 1
    public function auth($auth)
290
    {
291 1
        $this->auth = $auth;
292 1
        return $this;
293
    }
294
295
    /**
296
     *
297
     * Merges with the existing default values for attributes.
298
     *
299
     * @param array $defaults Default values for attributes.
300
     *
301
     * @return $this
302
     *
303
     */
304 3
    public function defaults(array $defaults)
305
    {
306 3
        $this->defaults = array_merge($this->defaults, $defaults);
307 3
        return $this;
308
    }
309
310
    /**
311
     *
312
     * Merges with the existing extra key-value pairs; this merge is recursive,
313
     * so the values can be arbitrarily deep.
314
     *
315
     * @param array $extras The extra key-value pairs.
316
     *
317
     * @return $this
318
     *
319
     */
320 1
    public function extras(array $extras)
321
    {
322 1
        $this->extras = array_merge_recursive($this->extras, $extras);
323 1
        return $this;
324
    }
325
326
    /**
327
     *
328
     * Sets the failed rule.
329
     *
330
     * @param mixed $failedRule The failed rule.
331
     *
332
     * @return $this
333
     *
334
     */
335 4
    public function failedRule($failedRule)
336
    {
337 4
        $this->failedRule = $failedRule;
338 4
        return $this;
339
    }
340
341
    /**
342
     *
343
     * The route leads to this handler.
344
     *
345
     * @param mixed $handler The handler for this route; if null, uses the
346
     * route name.
347
     *
348
     * @return $this
349
     *
350
     */
351 23
    public function handler($handler)
352
    {
353 23
        if ($handler === null) {
354 21
            $handler = $this->name;
355 21
        }
356 23
        $this->handler = $handler;
357 23
        return $this;
358
    }
359
360
    /**
361
     *
362
     * Sets the host.
363
     *
364
     * @param mixed $host The host.
365
     *
366
     * @return $this
367
     *
368
     */
369 5
    public function host($host)
370
    {
371 5
        $this->host = $host;
372 5
        return $this;
373
    }
374
375
    /**
376
     *
377
     * Sets whether or not this route should be used for matching.
378
     *
379
     * @param bool $isRoutable If true, this route can be matched; if not, it
380
     * can be used only to generate a path.
381
     *
382
     * @return $this
383
     *
384
     */
385 4
    public function isRoutable($isRoutable = true)
386
    {
387 4
        $this->isRoutable = (bool) $isRoutable;
388 4
        return $this;
389
    }
390
391
    /**
392
     *
393
     * Sets the route name; immutable once set.
394
     *
395
     * @param string $name The route name.
396
     *
397
     * @return $this
398
     * 
399
     * @throws Exception\ImmutableProperty when the name has already been set.
400
     *
401
     */
402 25
    public function name($name)
403
    {
404 25
        if ($this->name !== null) {
405 1
            $message = __CLASS__ . '::$name is immutable once set';
406 1
            throw new Exception\ImmutableProperty($message);
407
        }
408 25
        $this->name = $this->namePrefix . $name;
409 25
        return $this;
410
    }
411
412
    /**
413
     *
414
     * Appends to the existing name prefix; immutable once $name is set.
415
     *
416
     * @param string $namePrefix The name prefix to append.
417
     *
418
     * @return $this
419
     *
420
     * @throws Exception\ImmutableProperty when the name has already been set.
421
     *
422
     */
423 5
    public function namePrefix($namePrefix)
424
    {
425 5
        if ($this->name !== null) {
426 1
            $message = __CLASS__ . '::$namePrefix is immutable once $name is set';
427 1
            throw new Exception\ImmutableProperty($message);
428
        }
429 4
        $this->namePrefix = $namePrefix;
430 4
        return $this;
431
    }
432
433
    /**
434
     *
435
     * Sets the route path; immutable once set.
436
     *
437
     * @param string $path The route path.
438
     *
439
     * @return $this
440
     *
441
     * @throws Exception\ImmutableProperty when the name has already been set.
442
     *
443
     */
444 42
    public function path($path)
445
    {
446 42
        if ($this->path !== null) {
447 1
            $message = __CLASS__ . '::$path is immutable once set';
448 1
            throw new Exception\ImmutableProperty($message);
449
        }
450 42
        $this->path = $this->pathPrefix . $path;
451 42
        return $this;
452
    }
453
454
    /**
455
     *
456
     * Appends to the existing path prefix; immutable once $path is set.
457
     *
458
     * @param string $pathPrefix The path prefix to append.
459
     *
460
     * @return $this
461
     *
462
     * @throws Exception\ImmutableProperty when the path has already been set.
463
     *
464
     */
465 5
    public function pathPrefix($pathPrefix)
466
    {
467 5
        if ($this->path !== null) {
468 1
            $message = __CLASS__ . '::$pathPrefix is immutable once $path is set';
469 1
            throw new Exception\ImmutableProperty($message);
470
        }
471 4
        $this->pathPrefix = $pathPrefix;
472 4
        return $this;
473
    }
474
475
    /**
476
     *
477
     * Sets whether or not the route must be secure.
478
     *
479
     * @param bool|null $secure If true, the server must indicate an HTTPS request;
480
     * if false, it must *not* be HTTPS; if null, it doesn't matter.
481
     *
482
     * @return $this
483
     *
484
     */
485 5
    public function secure($secure = true)
486
    {
487 5
        $this->secure = ($secure === null) ? null : (bool) $secure;
488 5
        return $this;
489
    }
490
491
    /**
492
     *
493
     * Merges with the existing tokens.
494
     *
495
     * @param array $tokens The tokens.
496
     *
497
     * @return $this
498
     *
499
     */
500 12
    public function tokens(array $tokens)
501
    {
502 12
        $this->tokens = array_merge($this->tokens, $tokens);
503 12
        return $this;
504
    }
505
506
    /**
507
     *
508
     * Sets the name of the wildcard token, if any.
509
     *
510
     * @param string $wildcard The name of the wildcard token, if any.
511
     *
512
     * @return $this
513
     *
514
     */
515 3
    public function wildcard($wildcard)
516
    {
517 3
        $this->wildcard = $wildcard;
518 3
        return $this;
519
    }
520
}
521