1 | <?php |
||
15 | trait HasEnums |
||
16 | { |
||
17 | 80 | public function setAttribute($key, $value) |
|
23 | |||
24 | 28 | public function getAttribute($key) |
|
32 | |||
33 | /** |
||
34 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
35 | * @param string $key |
||
36 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
37 | * |
||
38 | * @see \Illuminate\Database\Eloquent\Builder::whereIn() |
||
39 | */ |
||
40 | 32 | public function scopeWhereEnum( |
|
52 | |||
53 | /** |
||
54 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
55 | * @param string $key |
||
56 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
57 | * |
||
58 | * @see \Illuminate\Database\Eloquent\Builder::orWhereIn() |
||
59 | */ |
||
60 | 12 | public function scopeOrWhereEnum( |
|
61 | Builder $builder, |
||
62 | string $key, |
||
63 | $enumerables |
||
64 | ): void { |
||
65 | 12 | $this->buildEnumScope( |
|
66 | 12 | $builder, |
|
67 | 12 | 'orWhereIn', |
|
68 | 6 | $key, |
|
69 | 6 | $enumerables |
|
70 | ); |
||
71 | 8 | } |
|
72 | |||
73 | /** |
||
74 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
75 | * @param string $key |
||
76 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
77 | * |
||
78 | * @see \Illuminate\Database\Eloquent\Builder::whereNotIn() |
||
79 | */ |
||
80 | 20 | public function scopeWhereNotEnum( |
|
92 | |||
93 | /** |
||
94 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
95 | * @param string $key |
||
96 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
97 | * |
||
98 | * @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn() |
||
99 | */ |
||
100 | 12 | public function scopeOrWhereNotEnum( |
|
112 | |||
113 | /** |
||
114 | * @param string $key |
||
115 | * @param int|string|\Spatie\Enum\Enumerable $value |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | 80 | protected function setEnumAttribute(string $key, $value) |
|
144 | 72 | ||
145 | 4 | /** |
|
146 | 72 | * @param string $key |
|
147 | * @param \Spatie\Enum\Enumerable $enum |
||
148 | * |
||
149 | * @return int|string |
||
150 | */ |
||
151 | protected function getStoredValue(string $key, Enumerable $enum) |
||
157 | 72 | ||
158 | /** |
||
159 | * @param string $key |
||
160 | 96 | * @param int|string|null $value |
|
161 | * |
||
162 | 96 | * @return \Spatie\Enum\Enumerable|int|string|null |
|
163 | */ |
||
164 | protected function getEnumAttribute(string $key, $value) |
||
172 | 4 | ||
173 | protected function isEnumAttribute(string $key): bool |
||
177 | |||
178 | protected function isNullableEnum(string $key, $value): bool |
||
194 | |||
195 | protected function getEnumClass(string $key): string |
||
213 | |||
214 | 44 | /** |
|
215 | 44 | * @param string $class |
|
216 | * @param int|string $value |
||
217 | 44 | * |
|
218 | 44 | * @return \Spatie\Enum\Enumerable |
|
219 | */ |
||
220 | 44 | protected function asEnum(string $class, $value): Enumerable |
|
231 | |||
232 | /** |
||
233 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
234 | * @param string $method |
||
235 | * @param string $key |
||
236 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
237 | */ |
||
238 | protected function buildEnumScope( |
||
257 | } |
||
258 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: