1 | <?php |
||
13 | abstract class State implements JsonSerializable |
||
14 | { |
||
15 | /** |
||
16 | * Static cache for generated state maps. |
||
17 | * |
||
18 | * @var array |
||
19 | * |
||
20 | * @see State::resolveStateMapping |
||
21 | */ |
||
22 | protected static $generatedMapping = []; |
||
23 | |||
24 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
25 | protected $model; |
||
26 | |||
27 | public function __construct(Model $model) |
||
31 | |||
32 | /** |
||
33 | * Create a state object based on a value (classname or name), |
||
34 | * and optionally provide its constructor arguments. |
||
35 | * |
||
36 | * @param string $name |
||
37 | * @param mixed ...$args |
||
38 | * |
||
39 | * @return \Spatie\ModelStates\State |
||
40 | */ |
||
41 | public static function make(string $name, Model $model): State |
||
51 | |||
52 | /** |
||
53 | * Create a state object based on a value (classname or name), |
||
54 | * and optionally provide its constructor arguments. |
||
55 | * |
||
56 | * @param string $name |
||
57 | * @param mixed ...$args |
||
58 | * |
||
59 | * @return \Spatie\ModelStates\State |
||
60 | */ |
||
61 | public static function find(string $name, Model $model): State |
||
65 | |||
66 | /** |
||
67 | * Get all registered state classes. |
||
68 | * |
||
69 | * @return \Illuminate\Support\Collection|string[]|static[] A list of class names. |
||
70 | */ |
||
71 | public static function all(): Collection |
||
75 | |||
76 | /** |
||
77 | * The value that will be saved in the database. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public static function getMorphClass(): string |
||
85 | |||
86 | /** |
||
87 | * The value that will be saved in the database. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getValue(): string |
||
95 | |||
96 | /** |
||
97 | * Resolve the state class based on a value, for example a stored value in the database. |
||
98 | * |
||
99 | * @param string|\Spatie\ModelStates\State $state |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public static function resolveStateClass($state): ?string |
||
127 | |||
128 | /** |
||
129 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
130 | * |
||
131 | * Possible names are: |
||
132 | * |
||
133 | * - The classname, is no explicit name is provided |
||
134 | * - A name provided in the state class as a public static property: |
||
135 | * `public static $name = 'dummy'` |
||
136 | * |
||
137 | * @param $state |
||
138 | * |
||
139 | * @return string|null |
||
140 | */ |
||
141 | public static function resolveStateName($state): ?string |
||
159 | |||
160 | /** |
||
161 | * Determine if the current state is one of an arbitrary number of other states. |
||
162 | * This can be either a classname or a name. |
||
163 | * |
||
164 | * @param string|array ...$stateClasses |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function isOneOf(...$statesNames): bool |
||
180 | |||
181 | /** |
||
182 | * Determine if the current state equals another. |
||
183 | * This can be either a classname or a name. |
||
184 | * |
||
185 | * @param string|\Spatie\ModelStates\State $state |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function equals($state): bool |
||
194 | |||
195 | /** |
||
196 | * Determine if the current state equals another. |
||
197 | * This can be either a classname or a name. |
||
198 | * |
||
199 | * @param string|\Spatie\ModelStates\State $state |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function is($state): bool |
||
207 | |||
208 | public function __toString(): string |
||
212 | |||
213 | /** |
||
214 | * @param string|\Spatie\ModelStates\Transition $transitionClass |
||
215 | * @param mixed ...$args |
||
216 | * |
||
217 | * @return \Illuminate\Database\Eloquent\Model |
||
218 | */ |
||
219 | public function transition($transition, ...$args): Model |
||
237 | |||
238 | /** |
||
239 | * @param string|\Spatie\ModelStates\State $state |
||
240 | * @param mixed ...$args |
||
241 | * |
||
242 | * @return \Illuminate\Database\Eloquent\Model |
||
243 | */ |
||
244 | public function transitionTo($state, ...$args): Model |
||
257 | |||
258 | /** |
||
259 | * Get the transitionable states from this state. |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | public function transitionableStates($field = null): array |
||
267 | |||
268 | /** |
||
269 | * This method is used to find all available implementations of a given abstract state class. |
||
270 | * Finding all implementations can be done in two ways:. |
||
271 | * |
||
272 | * - The developer can define his own mapping directly in abstract state classes |
||
273 | * via the `protected $states = []` property |
||
274 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
275 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | private static function resolveStateMapping(): array |
||
317 | |||
318 | public function jsonSerialize() |
||
322 | } |
||
323 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: