1 | <?php |
||
13 | trait HasStates |
||
14 | { |
||
15 | /** @var \Spatie\ModelStates\StateConfig[]|null */ |
||
16 | protected static $stateFields = null; |
||
17 | |||
18 | abstract protected function registerStates(): void; |
||
19 | |||
20 | public static function bootHasStates(): void |
||
78 | |||
79 | public function initializeHasStates(): void |
||
89 | |||
90 | public function scopeWhereState(Builder $builder, string $field, $states): Builder |
||
91 | { |
||
92 | self::getStateConfig(); |
||
93 | |||
94 | /** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
||
95 | $stateConfig = self::getStateConfig()[$field] ?? null; |
||
96 | |||
97 | if (! $stateConfig) { |
||
98 | throw InvalidConfig::unknownState($field, $this); |
||
99 | } |
||
100 | |||
101 | $abstractStateClass = $stateConfig->stateClass; |
||
102 | |||
103 | $stateNames = collect((array) $states)->map(function ($state) use ($abstractStateClass) { |
||
104 | return $abstractStateClass::resolveStateName($state); |
||
105 | }); |
||
106 | |||
107 | return $builder->whereIn($field, $stateNames); |
||
108 | } |
||
109 | |||
110 | public function scopeWhereNotState(Builder $builder, string $field, $states): Builder |
||
111 | { |
||
112 | /** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
||
113 | $stateConfig = self::getStateConfig()[$field] ?? null; |
||
114 | |||
115 | if (! $stateConfig) { |
||
116 | throw InvalidConfig::unknownState($field, $this); |
||
117 | } |
||
118 | |||
119 | $stateNames = collect((array) $states)->map(function ($state) use ($stateConfig) { |
||
120 | return $stateConfig->stateClass::resolveStateName($state); |
||
121 | }); |
||
122 | |||
123 | return $builder->whereNotIn($field, $stateNames); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param \Spatie\ModelStates\State|string $state |
||
128 | * @param string|null $field |
||
129 | */ |
||
130 | public function transitionTo($state, string $field = null) |
||
142 | |||
143 | /** |
||
144 | * @param string $fromClass |
||
145 | * @param string $toClass |
||
146 | * |
||
147 | * @return \Spatie\ModelStates\Transition|string|null |
||
148 | */ |
||
149 | public function resolveTransitionClass(string $fromClass, string $toClass) |
||
161 | |||
162 | protected function addState(string $field, string $stateClass): StateConfig |
||
170 | |||
171 | /** |
||
172 | * @return \Spatie\ModelStates\StateConfig[] |
||
173 | */ |
||
174 | private static function getStateConfig(): array |
||
184 | |||
185 | /** |
||
186 | * Get the registered states for the model. |
||
187 | * |
||
188 | * @return \Illuminate\Support\Collection |
||
189 | */ |
||
190 | public static function getStates(): \Illuminate\Support\Collection |
||
200 | |||
201 | /** |
||
202 | * Get registered states for a specfic model column. |
||
203 | * |
||
204 | * @param string $column |
||
205 | * @return \Illuminate\Support\Collection |
||
206 | */ |
||
207 | public static function getStatesFor(string $column): \Illuminate\Support\Collection |
||
211 | |||
212 | /** |
||
213 | * Get default states for the model. |
||
214 | * |
||
215 | * @return \Illuminate\Support\Collection |
||
216 | */ |
||
217 | public static function getDefaultStates(): \Illuminate\Support\Collection |
||
226 | |||
227 | /** |
||
228 | * Get the default state for a specfic model column. |
||
229 | * |
||
230 | * @param string $column |
||
231 | * @return \Illuminate\Support\Collection| |
||
232 | */ |
||
233 | public static function getDefaultStateFor(string $column): ?\Spatie\ModelStates\State |
||
237 | } |
||
238 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: