-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathCreatesRegularExpressionRouteConstraints.php
96 lines (86 loc) · 2.54 KB
/
CreatesRegularExpressionRouteConstraints.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Collection;
use function Illuminate\Support\enum_value;
trait CreatesRegularExpressionRouteConstraints
{
/**
* Specify that the given route parameters must be alphabetic.
*
* @param array|string $parameters
* @return $this
*/
public function whereAlpha($parameters)
{
return $this->assignExpressionToParameters($parameters, '[a-zA-Z]+');
}
/**
* Specify that the given route parameters must be alphanumeric.
*
* @param array|string $parameters
* @return $this
*/
public function whereAlphaNumeric($parameters)
{
return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+');
}
/**
* Specify that the given route parameters must be numeric.
*
* @param array|string $parameters
* @return $this
*/
public function whereNumber($parameters)
{
return $this->assignExpressionToParameters($parameters, '[0-9]+');
}
/**
* Specify that the given route parameters must be ULIDs.
*
* @param array|string $parameters
* @return $this
*/
public function whereUlid($parameters)
{
return $this->assignExpressionToParameters($parameters, '[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}');
}
/**
* Specify that the given route parameters must be UUIDs.
*
* @param array|string $parameters
* @return $this
*/
public function whereUuid($parameters)
{
return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}');
}
/**
* Specify that the given route parameters must be one of the given values.
*
* @param array|string $parameters
* @param array $values
* @return $this
*/
public function whereIn($parameters, array $values)
{
return $this->assignExpressionToParameters(
$parameters,
(new Collection($values))
->map(fn ($value) => enum_value($value))
->implode('|')
);
}
/**
* Apply the given regular expression to the given parameters.
*
* @param array|string $parameters
* @param string $expression
* @return $this
*/
protected function assignExpressionToParameters($parameters, $expression)
{
return $this->where(Collection::wrap($parameters)
->mapWithKeys(fn ($parameter) => [$parameter => $expression])
->all());
}
}