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 | public function transitionableStates(string $fromClass, ?string $field = null): array |
||
144 | { |
||
159 | |||
160 | /** |
||
161 | * @param string $fromClass |
||
162 | * @param string $toClass |
||
163 | * |
||
164 | * @return \Spatie\ModelStates\Transition|string|null |
||
165 | */ |
||
166 | public function resolveTransitionClass(string $fromClass, string $toClass) |
||
178 | |||
179 | protected function addState(string $field, string $stateClass): StateConfig |
||
187 | |||
188 | /** |
||
189 | * @return \Spatie\ModelStates\StateConfig[] |
||
190 | */ |
||
191 | private static function getStateConfig(): array |
||
201 | } |
||
202 |
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: