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( |
|
101 | Builder $builder, |
||
102 | string $key, |
||
103 | $enumerables |
||
104 | ): void { |
||
105 | 12 | $this->buildEnumScope( |
|
106 | 12 | $builder, |
|
107 | 12 | 'orWhereNotIn', |
|
108 | 6 | $key, |
|
109 | 6 | $enumerables |
|
110 | ); |
||
111 | 8 | } |
|
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) |
|
135 | |||
136 | /** |
||
137 | * @param string $key |
||
138 | * @param \Spatie\Enum\Enumerable $enum |
||
139 | * |
||
140 | * @return int|string |
||
141 | */ |
||
142 | 72 | protected function getStoredValue(string $key, Enumerable $enum) |
|
148 | |||
149 | /** |
||
150 | * @param string $key |
||
151 | * @param int|string $value |
||
152 | * |
||
153 | * @return \Spatie\Enum\Enumerable |
||
154 | */ |
||
155 | 72 | protected function getEnumAttribute(string $key, $value): Enumerable |
|
159 | |||
160 | 96 | protected function isEnumAttribute(string $key): bool |
|
164 | |||
165 | 80 | protected function getEnumClass(string $key): string |
|
177 | |||
178 | /** |
||
179 | * @param string $class |
||
180 | * @param int|string $value |
||
181 | * |
||
182 | * @return \Spatie\Enum\Enumerable |
||
183 | */ |
||
184 | 72 | protected function asEnum(string $class, $value): Enumerable |
|
195 | |||
196 | /** |
||
197 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
198 | * @param string $method |
||
199 | * @param string $key |
||
200 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
201 | */ |
||
202 | 60 | protected function buildEnumScope( |
|
221 | } |
||
222 |
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: