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 |
||
125 | |||
126 | /** |
||
127 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
128 | * |
||
129 | * Possible names are: |
||
130 | * |
||
131 | * - The classname, is no explicit name is provided |
||
132 | * - A name provided in the state class as a public static property: |
||
133 | * `public static $name = 'dummy'` |
||
134 | * |
||
135 | * @param $state |
||
136 | * |
||
137 | * @return string|null |
||
138 | */ |
||
139 | public static function resolveStateName($state): ?string |
||
157 | |||
158 | /** |
||
159 | * Determine if the current state is one of an arbitrary number of other states. |
||
160 | * This can be either a classname or a name. |
||
161 | * |
||
162 | * @param string|array ...$stateClasses |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | public function isOneOf(...$statesNames): bool |
||
178 | |||
179 | /** |
||
180 | * Determine if the current state equals another. |
||
181 | * This can be either a classname or a name. |
||
182 | * |
||
183 | * @param string|\Spatie\ModelStates\State $state |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function equals($state): bool |
||
192 | |||
193 | /** |
||
194 | * Determine if the current state equals another. |
||
195 | * This can be either a classname or a name. |
||
196 | * |
||
197 | * @param string|\Spatie\ModelStates\State $state |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function is($state): bool |
||
205 | |||
206 | public function __toString(): string |
||
210 | |||
211 | /** |
||
212 | * @param string|\Spatie\ModelStates\Transition $transitionClass |
||
213 | * @param mixed ...$args |
||
214 | * |
||
215 | * @return \Illuminate\Database\Eloquent\Model |
||
216 | */ |
||
217 | public function transition($transition, ...$args): Model |
||
235 | |||
236 | /** |
||
237 | * @param string|\Spatie\ModelStates\State $state |
||
238 | * @param mixed ...$args |
||
239 | * |
||
240 | * @return \Illuminate\Database\Eloquent\Model |
||
241 | */ |
||
242 | public function transitionTo($state, ...$args): Model |
||
255 | |||
256 | /** |
||
257 | * This method is used to find all available implementations of a given abstract state class. |
||
258 | * Finding all implementations can be done in two ways:. |
||
259 | * |
||
260 | * - The developer can define his own mapping directly in abstract state classes |
||
261 | * via the `protected $states = []` property |
||
262 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
263 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | private static function resolveStateMapping(): array |
||
305 | |||
306 | public function jsonSerialize() { |
||
309 | } |
||
310 |
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: