1 | <?php |
||
14 | trait HasStates |
||
15 | { |
||
16 | /** @var \Spatie\ModelStates\StateConfig[]|null */ |
||
17 | protected static $stateFields = null; |
||
18 | |||
19 | abstract protected function registerStates(): void; |
||
20 | |||
21 | public static function bootHasStates(): void |
||
79 | |||
80 | public function initializeHasStates(): void |
||
90 | |||
91 | public function scopeWhereState(Builder $builder, string $field, $states): Builder |
||
92 | { |
||
93 | self::getStateConfig(); |
||
94 | |||
95 | /** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
||
96 | $stateConfig = self::getStateConfig()[$field] ?? null; |
||
97 | |||
98 | if (! $stateConfig) { |
||
99 | throw InvalidConfig::unknownState($field, $this); |
||
100 | } |
||
101 | |||
102 | $abstractStateClass = $stateConfig->stateClass; |
||
103 | |||
104 | $stateNames = collect((array) $states)->map(function ($state) use ($abstractStateClass) { |
||
105 | return $abstractStateClass::resolveStateName($state); |
||
106 | }); |
||
107 | |||
108 | return $builder->whereIn($field, $stateNames); |
||
109 | } |
||
110 | |||
111 | public function scopeWhereNotState(Builder $builder, string $field, $states): Builder |
||
112 | { |
||
113 | /** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
||
114 | $stateConfig = self::getStateConfig()[$field] ?? null; |
||
115 | |||
116 | if (! $stateConfig) { |
||
117 | throw InvalidConfig::unknownState($field, $this); |
||
118 | } |
||
119 | |||
120 | $stateNames = collect((array) $states)->map(function ($state) use ($stateConfig) { |
||
121 | return $stateConfig->stateClass::resolveStateName($state); |
||
122 | }); |
||
123 | |||
124 | return $builder->whereNotIn($field, $stateNames); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param \Spatie\ModelStates\State|string $state |
||
129 | * @param string|null $field |
||
130 | */ |
||
131 | public function transitionTo($state, string $field = null) |
||
143 | |||
144 | public function transitionableStates(string $fromClass, ?string $field = null): array |
||
145 | { |
||
160 | |||
161 | /** |
||
162 | * @param string $fromClass |
||
163 | * @param string $toClass |
||
164 | * |
||
165 | * @return \Spatie\ModelStates\Transition|string|null |
||
166 | */ |
||
167 | public function resolveTransitionClass(string $fromClass, string $toClass) |
||
179 | |||
180 | protected function addState(string $field, string $stateClass): StateConfig |
||
188 | |||
189 | /** |
||
190 | * @return \Spatie\ModelStates\StateConfig[] |
||
191 | */ |
||
192 | private static function getStateConfig(): array |
||
202 | |||
203 | public static function getStates(): Collection |
||
210 | |||
211 | public static function getStatesFor(string $column): Collection |
||
215 | |||
216 | public static function getDefaultStates(): Collection |
||
223 | |||
224 | public static function getDefaultStateFor(string $column): string |
||
228 | } |
||
229 |
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: