1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Enum\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Spatie\Enum\Enumerable; |
10
|
|
|
use Spatie\Enum\Laravel\Exceptions\ExpectsArrayOfEnumsField; |
11
|
|
|
use Spatie\Enum\Laravel\Exceptions\InvalidEnumError; |
12
|
|
|
use Spatie\Enum\Laravel\Exceptions\NoSuchEnumField; |
13
|
|
|
use Spatie\Enum\Laravel\Exceptions\NotNullableEnumField; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @mixin Model |
17
|
80 |
|
*/ |
18
|
|
|
trait HasEnums |
19
|
80 |
|
{ |
20
|
80 |
|
/** |
21
|
72 |
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
22
|
|
|
* @param string $key |
23
|
|
|
* @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
24
|
28 |
|
* |
25
|
|
|
* @see \Illuminate\Database\Eloquent\Builder::whereIn() |
26
|
28 |
|
*/ |
27
|
|
|
public function scopeWhereEnum( |
28
|
28 |
|
Builder $builder, |
29
|
28 |
|
string $key, |
30
|
28 |
|
$enumerables |
31
|
|
|
): void { |
32
|
|
|
$this->buildEnumScope( |
33
|
|
|
$builder, |
34
|
|
|
'whereIn', |
35
|
|
|
$key, |
36
|
|
|
$enumerables |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
32 |
|
/** |
41
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
42
|
|
|
* @param string $key |
43
|
|
|
* @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
44
|
|
|
* |
45
|
32 |
|
* @see \Illuminate\Database\Eloquent\Builder::orWhereIn() |
46
|
32 |
|
*/ |
47
|
32 |
|
public function scopeOrWhereEnum( |
48
|
16 |
|
Builder $builder, |
49
|
16 |
|
string $key, |
50
|
|
|
$enumerables |
51
|
28 |
|
): void { |
52
|
|
|
$this->buildEnumScope( |
53
|
|
|
$builder, |
54
|
|
|
'orWhereIn', |
55
|
|
|
$key, |
56
|
|
|
$enumerables |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
12 |
|
/** |
61
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
62
|
|
|
* @param string $key |
63
|
|
|
* @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
64
|
|
|
* |
65
|
12 |
|
* @see \Illuminate\Database\Eloquent\Builder::whereNotIn() |
66
|
12 |
|
*/ |
67
|
12 |
|
public function scopeWhereNotEnum( |
68
|
6 |
|
Builder $builder, |
69
|
6 |
|
string $key, |
70
|
|
|
$enumerables |
71
|
8 |
|
): void { |
72
|
|
|
$this->buildEnumScope( |
73
|
|
|
$builder, |
74
|
|
|
'whereNotIn', |
75
|
|
|
$key, |
76
|
|
|
$enumerables |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
20 |
|
/** |
81
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
82
|
|
|
* @param string $key |
83
|
|
|
* @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
84
|
|
|
* |
85
|
20 |
|
* @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn() |
86
|
20 |
|
*/ |
87
|
20 |
|
public function scopeOrWhereNotEnum( |
88
|
10 |
|
Builder $builder, |
89
|
10 |
|
string $key, |
90
|
|
|
$enumerables |
91
|
16 |
|
): void { |
92
|
|
|
$this->buildEnumScope( |
93
|
|
|
$builder, |
94
|
|
|
'orWhereNotIn', |
95
|
|
|
$key, |
96
|
|
|
$enumerables |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
/** |
101
|
|
|
* @param string $key |
102
|
|
|
* @param \Spatie\Enum\Enumerable $enum |
103
|
|
|
* |
104
|
|
|
* @return int|string |
105
|
12 |
|
*/ |
106
|
12 |
|
protected function getStoredValue(string $key, Enumerable $enum) |
107
|
12 |
|
{ |
108
|
6 |
|
return $this->hasCast($key, ['int', 'integer']) |
|
|
|
|
109
|
6 |
|
? $enum->getIndex() |
110
|
|
|
: $enum->getValue(); |
111
|
8 |
|
} |
112
|
|
|
|
113
|
|
|
protected function isEnumAttribute(string $key): bool |
114
|
|
|
{ |
115
|
|
|
return isset($this->enums[$key]); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
80 |
|
* @param string $class |
120
|
|
|
* @param int|string $value |
121
|
80 |
|
* |
122
|
|
|
* @return \Spatie\Enum\Enumerable |
123
|
76 |
|
*/ |
124
|
16 |
|
protected function asEnum(string $class, $value): Enumerable |
125
|
|
|
{ |
126
|
|
|
if ($value instanceof Enumerable) { |
127
|
76 |
|
return $value; |
128
|
4 |
|
} |
129
|
|
|
|
130
|
|
|
return forward_static_call( |
131
|
72 |
|
$class.'::make', |
132
|
|
|
$value |
133
|
72 |
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
138
|
|
|
* @param string $method |
139
|
|
|
* @param string $key |
140
|
|
|
* @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
141
|
|
|
*/ |
142
|
72 |
|
protected function buildEnumScope( |
143
|
|
|
Builder $builder, |
144
|
72 |
|
string $method, |
145
|
4 |
|
string $key, |
146
|
72 |
|
$enumerables |
147
|
|
|
): void { |
148
|
|
|
if (! $this->isEnumAttribute($key)) { |
149
|
|
|
throw NoSuchEnumField::make($key, static::class); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$enumerables = is_array($enumerables) ? $enumerables : [$enumerables]; |
153
|
|
|
|
154
|
|
|
$builder->$method( |
155
|
72 |
|
$key, |
156
|
|
|
array_map(function ($value) use ($key) { |
157
|
72 |
|
return $this->getStoredValue($key, $this->asEnum($key, $value)); |
158
|
|
|
}, $enumerables) |
159
|
|
|
); |
160
|
96 |
|
} |
161
|
|
|
} |
162
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.